OrganizationSeries.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /* *
  2. *
  3. * Organization chart module
  4. *
  5. * (c) 2018-2020 Torstein Honsi
  6. *
  7. * License: www.highcharts.com/license
  8. *
  9. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  10. *
  11. * */
  12. import BaseSeries from '../Core/Series/Series.js';
  13. import H from '../Core/Globals.js';
  14. import U from '../Core/Utilities.js';
  15. var css = U.css, pick = U.pick, wrap = U.wrap;
  16. import './SankeySeries.js';
  17. var base = BaseSeries.seriesTypes.sankey.prototype;
  18. /**
  19. * Layout value for the child nodes in an organization chart. If `hanging`, this
  20. * node's children will hang below their parent, allowing a tighter packing of
  21. * nodes in the diagram.
  22. *
  23. * @typedef {"normal"|"hanging"} Highcharts.SeriesOrganizationNodesLayoutValue
  24. */
  25. ''; // detach doclets above
  26. /**
  27. * @private
  28. * @class
  29. * @name Highcharts.seriesTypes.organization
  30. *
  31. * @augments Highcharts.seriesTypes.sankey
  32. */
  33. BaseSeries.seriesType('organization', 'sankey',
  34. /**
  35. * An organization chart is a diagram that shows the structure of an
  36. * organization and the relationships and relative ranks of its parts and
  37. * positions.
  38. *
  39. * @sample highcharts/demo/organization-chart/
  40. * Organization chart
  41. * @sample highcharts/series-organization/horizontal/
  42. * Horizontal organization chart
  43. * @sample highcharts/series-organization/borderless
  44. * Borderless design
  45. * @sample highcharts/series-organization/center-layout
  46. * Centered layout
  47. *
  48. * @extends plotOptions.sankey
  49. * @excluding allowPointSelect, curveFactor, dataSorting
  50. * @since 7.1.0
  51. * @product highcharts
  52. * @requires modules/organization
  53. * @optionparent plotOptions.organization
  54. */
  55. {
  56. /**
  57. * The border color of the node cards.
  58. *
  59. * @type {Highcharts.ColorString}
  60. * @private
  61. */
  62. borderColor: '#666666',
  63. /**
  64. * The border radius of the node cards.
  65. *
  66. * @private
  67. */
  68. borderRadius: 3,
  69. /**
  70. * Radius for the rounded corners of the links between nodes.
  71. *
  72. * @sample highcharts/series-organization/link-options
  73. * Square links
  74. *
  75. * @private
  76. */
  77. linkRadius: 10,
  78. borderWidth: 1,
  79. /**
  80. * @declare Highcharts.SeriesOrganizationDataLabelsOptionsObject
  81. *
  82. * @private
  83. */
  84. dataLabels: {
  85. /* eslint-disable valid-jsdoc */
  86. /**
  87. * A callback for defining the format for _nodes_ in the
  88. * organization chart. The `nodeFormat` option takes precedence
  89. * over `nodeFormatter`.
  90. *
  91. * In an organization chart, the `nodeFormatter` is a quite complex
  92. * function of the available options, striving for a good default
  93. * layout of cards with or without images. In organization chart,
  94. * the data labels come with `useHTML` set to true, meaning they
  95. * will be rendered as true HTML above the SVG.
  96. *
  97. * @sample highcharts/series-organization/datalabels-nodeformatter
  98. * Modify the default label format output
  99. *
  100. * @type {Highcharts.SeriesSankeyDataLabelsFormatterCallbackFunction}
  101. * @since 6.0.2
  102. */
  103. nodeFormatter: function () {
  104. var outerStyle = {
  105. width: '100%',
  106. height: '100%',
  107. display: 'flex',
  108. 'flex-direction': 'row',
  109. 'align-items': 'center',
  110. 'justify-content': 'center'
  111. }, imageStyle = {
  112. 'max-height': '100%',
  113. 'border-radius': '50%'
  114. }, innerStyle = {
  115. width: '100%',
  116. padding: 0,
  117. 'text-align': 'center',
  118. 'white-space': 'normal'
  119. }, nameStyle = {
  120. margin: 0
  121. }, titleStyle = {
  122. margin: 0
  123. }, descriptionStyle = {
  124. opacity: 0.75,
  125. margin: '5px'
  126. };
  127. // eslint-disable-next-line valid-jsdoc
  128. /**
  129. * @private
  130. */
  131. function styleAttr(style) {
  132. return Object.keys(style).reduce(function (str, key) {
  133. return str + key + ':' + style[key] + ';';
  134. }, 'style="') + '"';
  135. }
  136. if (this.point.image) {
  137. imageStyle['max-width'] = '30%';
  138. innerStyle.width = '70%';
  139. }
  140. // PhantomJS doesn't support flex, roll back to absolute
  141. // positioning
  142. if (this.series.chart.renderer.forExport) {
  143. outerStyle.display = 'block';
  144. innerStyle.position = 'absolute';
  145. innerStyle.left = this.point.image ? '30%' : 0;
  146. innerStyle.top = 0;
  147. }
  148. var html = '<div ' + styleAttr(outerStyle) + '>';
  149. if (this.point.image) {
  150. html += '<img src="' + this.point.image + '" ' +
  151. styleAttr(imageStyle) + '>';
  152. }
  153. html += '<div ' + styleAttr(innerStyle) + '>';
  154. if (this.point.name) {
  155. html += '<h4 ' + styleAttr(nameStyle) + '>' +
  156. this.point.name + '</h4>';
  157. }
  158. if (this.point.title) {
  159. html += '<p ' + styleAttr(titleStyle) + '>' +
  160. (this.point.title || '') + '</p>';
  161. }
  162. if (this.point.description) {
  163. html += '<p ' + styleAttr(descriptionStyle) + '>' +
  164. this.point.description + '</p>';
  165. }
  166. html += '</div>' +
  167. '</div>';
  168. return html;
  169. },
  170. /* eslint-enable valid-jsdoc */
  171. style: {
  172. /** @internal */
  173. fontWeight: 'normal',
  174. /** @internal */
  175. fontSize: '13px'
  176. },
  177. useHTML: true
  178. },
  179. /**
  180. * The indentation in pixels of hanging nodes, nodes which parent has
  181. * [layout](#series.organization.nodes.layout) set to `hanging`.
  182. *
  183. * @private
  184. */
  185. hangingIndent: 20,
  186. /**
  187. * The color of the links between nodes.
  188. *
  189. * @type {Highcharts.ColorString}
  190. * @private
  191. */
  192. linkColor: '#666666',
  193. /**
  194. * The line width of the links connecting nodes, in pixels.
  195. *
  196. * @sample highcharts/series-organization/link-options
  197. * Square links
  198. *
  199. * @private
  200. */
  201. linkLineWidth: 1,
  202. /**
  203. * In a horizontal chart, the width of the nodes in pixels. Node that
  204. * most organization charts are vertical, so the name of this option
  205. * is counterintuitive.
  206. *
  207. * @private
  208. */
  209. nodeWidth: 50,
  210. tooltip: {
  211. nodeFormat: '{point.name}<br>{point.title}<br>{point.description}'
  212. }
  213. }, {
  214. pointAttribs: function (point, state) {
  215. var series = this, attribs = base.pointAttribs.call(series, point, state), level = point.isNode ? point.level : point.fromNode.level, levelOptions = series.mapOptionsToLevel[level || 0] || {}, options = point.options, stateOptions = (levelOptions.states && levelOptions.states[state]) || {}, values = ['borderRadius', 'linkColor', 'linkLineWidth']
  216. .reduce(function (obj, key) {
  217. obj[key] = pick(stateOptions[key], options[key], levelOptions[key], series.options[key]);
  218. return obj;
  219. }, {});
  220. if (!point.isNode) {
  221. attribs.stroke = values.linkColor;
  222. attribs['stroke-width'] = values.linkLineWidth;
  223. delete attribs.fill;
  224. }
  225. else {
  226. if (values.borderRadius) {
  227. attribs.r = values.borderRadius;
  228. }
  229. }
  230. return attribs;
  231. },
  232. createNode: function (id) {
  233. var node = base.createNode
  234. .call(this, id);
  235. // All nodes in an org chart are equal width
  236. node.getSum = function () {
  237. return 1;
  238. };
  239. return node;
  240. },
  241. createNodeColumn: function () {
  242. var column = base.createNodeColumn.call(this);
  243. // Wrap the offset function so that the hanging node's children are
  244. // aligned to their parent
  245. wrap(column, 'offset', function (proceed, node, factor) {
  246. var offset = proceed.call(this, node, factor); // eslint-disable-line no-invalid-this
  247. // Modify the default output if the parent's layout is 'hanging'
  248. if (node.hangsFrom) {
  249. return {
  250. absoluteTop: node.hangsFrom.nodeY
  251. };
  252. }
  253. return offset;
  254. });
  255. return column;
  256. },
  257. translateNode: function (node, column) {
  258. base.translateNode.call(this, node, column);
  259. if (node.hangsFrom) {
  260. node.shapeArgs.height -=
  261. this.options.hangingIndent;
  262. if (!this.chart.inverted) {
  263. node.shapeArgs.y += this.options.hangingIndent;
  264. }
  265. }
  266. node.nodeHeight = this.chart.inverted ?
  267. node.shapeArgs.width :
  268. node.shapeArgs.height;
  269. },
  270. // General function to apply corner radius to a path - can be lifted to
  271. // renderer or utilities if we need it elsewhere.
  272. curvedPath: function (path, r) {
  273. var d = [];
  274. for (var i = 0; i < path.length; i++) {
  275. var x = path[i][1];
  276. var y = path[i][2];
  277. if (typeof x === 'number' && typeof y === 'number') {
  278. // moveTo
  279. if (i === 0) {
  280. d.push(['M', x, y]);
  281. }
  282. else if (i === path.length - 1) {
  283. d.push(['L', x, y]);
  284. // curveTo
  285. }
  286. else if (r) {
  287. var prevSeg = path[i - 1];
  288. var nextSeg = path[i + 1];
  289. if (prevSeg && nextSeg) {
  290. var x1 = prevSeg[1], y1 = prevSeg[2], x2 = nextSeg[1], y2 = nextSeg[2];
  291. // Only apply to breaks
  292. if (typeof x1 === 'number' &&
  293. typeof x2 === 'number' &&
  294. typeof y1 === 'number' &&
  295. typeof y2 === 'number' &&
  296. x1 !== x2 &&
  297. y1 !== y2) {
  298. var directionX = x1 < x2 ? 1 : -1, directionY = y1 < y2 ? 1 : -1;
  299. d.push([
  300. 'L',
  301. x - directionX * Math.min(Math.abs(x - x1), r),
  302. y - directionY * Math.min(Math.abs(y - y1), r)
  303. ], [
  304. 'C',
  305. x,
  306. y,
  307. x,
  308. y,
  309. x + directionX * Math.min(Math.abs(x - x2), r),
  310. y + directionY * Math.min(Math.abs(y - y2), r)
  311. ]);
  312. }
  313. }
  314. // lineTo
  315. }
  316. else {
  317. d.push(['L', x, y]);
  318. }
  319. }
  320. }
  321. return d;
  322. },
  323. translateLink: function (point) {
  324. var fromNode = point.fromNode, toNode = point.toNode, crisp = Math.round(this.options.linkLineWidth) % 2 / 2, x1 = Math.floor(fromNode.shapeArgs.x +
  325. fromNode.shapeArgs.width) + crisp, y1 = Math.floor(fromNode.shapeArgs.y +
  326. fromNode.shapeArgs.height / 2) + crisp, x2 = Math.floor(toNode.shapeArgs.x) + crisp, y2 = Math.floor(toNode.shapeArgs.y +
  327. toNode.shapeArgs.height / 2) + crisp, xMiddle, hangingIndent = this.options.hangingIndent, toOffset = toNode.options.offset, percentOffset = /%$/.test(toOffset) && parseInt(toOffset, 10), inverted = this.chart.inverted;
  328. if (inverted) {
  329. x1 -= fromNode.shapeArgs.width;
  330. x2 += toNode.shapeArgs.width;
  331. }
  332. xMiddle = Math.floor(x2 +
  333. (inverted ? 1 : -1) *
  334. (this.colDistance - this.nodeWidth) / 2) + crisp;
  335. // Put the link on the side of the node when an offset is given. HR
  336. // node in the main demo.
  337. if (percentOffset &&
  338. (percentOffset >= 50 || percentOffset <= -50)) {
  339. xMiddle = x2 = Math.floor(x2 + (inverted ? -0.5 : 0.5) *
  340. toNode.shapeArgs.width) + crisp;
  341. y2 = toNode.shapeArgs.y;
  342. if (percentOffset > 0) {
  343. y2 += toNode.shapeArgs.height;
  344. }
  345. }
  346. if (toNode.hangsFrom === fromNode) {
  347. if (this.chart.inverted) {
  348. y1 = Math.floor(fromNode.shapeArgs.y +
  349. fromNode.shapeArgs.height -
  350. hangingIndent / 2) + crisp;
  351. y2 = (toNode.shapeArgs.y +
  352. toNode.shapeArgs.height);
  353. }
  354. else {
  355. y1 = Math.floor(fromNode.shapeArgs.y +
  356. hangingIndent / 2) + crisp;
  357. }
  358. xMiddle = x2 = Math.floor(toNode.shapeArgs.x +
  359. toNode.shapeArgs.width / 2) + crisp;
  360. }
  361. point.plotY = 1;
  362. point.shapeType = 'path';
  363. point.shapeArgs = {
  364. d: this.curvedPath([
  365. ['M', x1, y1],
  366. ['L', xMiddle, y1],
  367. ['L', xMiddle, y2],
  368. ['L', x2, y2]
  369. ], this.options.linkRadius)
  370. };
  371. },
  372. alignDataLabel: function (point, dataLabel, options) {
  373. // Align the data label to the point graphic
  374. if (options.useHTML) {
  375. var width = point.shapeArgs.width, height = point.shapeArgs.height, padjust = (this.options.borderWidth +
  376. 2 * this.options.dataLabels.padding);
  377. if (this.chart.inverted) {
  378. width = height;
  379. height = point.shapeArgs.width;
  380. }
  381. height -= padjust;
  382. width -= padjust;
  383. // Set the size of the surrounding div emulating `g`
  384. var text = dataLabel.text;
  385. if (text) {
  386. css(text.element.parentNode, {
  387. width: width + 'px',
  388. height: height + 'px'
  389. });
  390. // Set properties for the span emulating `text`
  391. css(text.element, {
  392. left: 0,
  393. top: 0,
  394. width: '100%',
  395. height: '100%',
  396. overflow: 'hidden'
  397. });
  398. }
  399. // The getBBox function is used in `alignDataLabel` to align
  400. // inside the box
  401. dataLabel.getBBox = function () {
  402. return {
  403. width: width,
  404. height: height
  405. };
  406. };
  407. // Overwrite dataLabel dimensions (#13100).
  408. dataLabel.width = width;
  409. dataLabel.height = height;
  410. }
  411. H.seriesTypes.column.prototype.alignDataLabel.apply(this, arguments);
  412. }
  413. });
  414. /**
  415. * An `organization` series. If the [type](#series.organization.type) option is
  416. * not specified, it is inherited from [chart.type](#chart.type).
  417. *
  418. * @extends series,plotOptions.organization
  419. * @exclude dataSorting, boostThreshold, boostBlending
  420. * @product highcharts
  421. * @requires modules/sankey
  422. * @requires modules/organization
  423. * @apioption series.organization
  424. */
  425. /**
  426. * @type {Highcharts.SeriesOrganizationDataLabelsOptionsObject|Array<Highcharts.SeriesOrganizationDataLabelsOptionsObject>}
  427. * @product highcharts
  428. * @apioption series.organization.data.dataLabels
  429. */
  430. /**
  431. * A collection of options for the individual nodes. The nodes in an org chart
  432. * are auto-generated instances of `Highcharts.Point`, but options can be
  433. * applied here and linked by the `id`.
  434. *
  435. * @extends series.sankey.nodes
  436. * @type {Array<*>}
  437. * @product highcharts
  438. * @apioption series.organization.nodes
  439. */
  440. /**
  441. * Individual data label for each node. The options are the same as
  442. * the ones for [series.organization.dataLabels](#series.organization.dataLabels).
  443. *
  444. * @type {Highcharts.SeriesOrganizationDataLabelsOptionsObject|Array<Highcharts.SeriesOrganizationDataLabelsOptionsObject>}
  445. *
  446. * @apioption series.organization.nodes.dataLabels
  447. */
  448. /**
  449. * The job description for the node card, will be inserted by the default
  450. * `dataLabel.nodeFormatter`.
  451. *
  452. * @sample highcharts/demo/organization-chart
  453. * Org chart with job descriptions
  454. *
  455. * @type {string}
  456. * @product highcharts
  457. * @apioption series.organization.nodes.description
  458. */
  459. /**
  460. * An image for the node card, will be inserted by the default
  461. * `dataLabel.nodeFormatter`.
  462. *
  463. * @sample highcharts/demo/organization-chart
  464. * Org chart with images
  465. *
  466. * @type {string}
  467. * @product highcharts
  468. * @apioption series.organization.nodes.image
  469. */
  470. /**
  471. * Layout for the node's children. If `hanging`, this node's children will hang
  472. * below their parent, allowing a tighter packing of nodes in the diagram.
  473. *
  474. * @sample highcharts/demo/organization-chart
  475. * Hanging layout
  476. *
  477. * @type {Highcharts.SeriesOrganizationNodesLayoutValue}
  478. * @default normal
  479. * @product highcharts
  480. * @apioption series.organization.nodes.layout
  481. */
  482. /**
  483. * The job title for the node card, will be inserted by the default
  484. * `dataLabel.nodeFormatter`.
  485. *
  486. * @sample highcharts/demo/organization-chart
  487. * Org chart with job titles
  488. *
  489. * @type {string}
  490. * @product highcharts
  491. * @apioption series.organization.nodes.title
  492. */
  493. /**
  494. * An array of data points for the series. For the `organization` series
  495. * type, points can be given in the following way:
  496. *
  497. * An array of objects with named values. The following snippet shows only a
  498. * few settings, see the complete options set below. If the total number of data
  499. * points exceeds the series' [turboThreshold](#series.area.turboThreshold),
  500. * this option is not available.
  501. *
  502. * ```js
  503. * data: [{
  504. * from: 'Category1',
  505. * to: 'Category2',
  506. * weight: 2
  507. * }, {
  508. * from: 'Category1',
  509. * to: 'Category3',
  510. * weight: 5
  511. * }]
  512. * ```
  513. *
  514. * @type {Array<*>}
  515. * @extends series.sankey.data
  516. * @product highcharts
  517. * @apioption series.organization.data
  518. */
  519. ''; // adds doclets above to transpiled file