SankeySeries.js 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. /* *
  2. *
  3. * Sankey diagram module
  4. *
  5. * (c) 2010-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 Series from '../Core/Series/Series.js';
  13. import CartesianSeries from '../Core/Series/CartesianSeries.js';
  14. import Color from '../Core/Color/Color.js';
  15. import ColumnSeries from './ColumnSeries.js';
  16. import H from '../Core/Globals.js';
  17. import NodesMixin from '../Mixins/Nodes.js';
  18. import Point from '../Core/Series/Point.js';
  19. import TreeSeriesMixin from '../Mixins/TreeSeries.js';
  20. var getLevelOptions = TreeSeriesMixin.getLevelOptions;
  21. import U from '../Core/Utilities.js';
  22. var defined = U.defined, find = U.find, isObject = U.isObject, merge = U.merge, pick = U.pick, relativeLength = U.relativeLength, stableSort = U.stableSort;
  23. /**
  24. * A node in a sankey diagram.
  25. *
  26. * @interface Highcharts.SankeyNodeObject
  27. * @extends Highcharts.Point
  28. * @product highcharts
  29. */ /**
  30. * The color of the auto generated node.
  31. *
  32. * @name Highcharts.SankeyNodeObject#color
  33. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  34. */ /**
  35. * The color index of the auto generated node, especially for use in styled
  36. * mode.
  37. *
  38. * @name Highcharts.SankeyNodeObject#colorIndex
  39. * @type {number}
  40. */ /**
  41. * An optional column index of where to place the node. The default behaviour is
  42. * to place it next to the preceding node.
  43. *
  44. * @see {@link https://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/plotoptions/sankey-node-column/|Highcharts-Demo:}
  45. * Specified node column
  46. *
  47. * @name Highcharts.SankeyNodeObject#column
  48. * @type {number}
  49. * @since 6.0.5
  50. */ /**
  51. * The id of the auto-generated node, refering to the `from` or `to` setting of
  52. * the link.
  53. *
  54. * @name Highcharts.SankeyNodeObject#id
  55. * @type {string}
  56. */ /**
  57. * The name to display for the node in data labels and tooltips. Use this when
  58. * the name is different from the `id`. Where the id must be unique for each
  59. * node, this is not necessary for the name.
  60. *
  61. * @see {@link https://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/css/sankey/|Highcharts-Demo:}
  62. * Sankey diagram with node options
  63. *
  64. * @name Highcharts.SankeyNodeObject#name
  65. * @type {string}
  66. * @product highcharts
  67. */ /**
  68. * The vertical offset of a node in terms of weight. Positive values shift the
  69. * node downwards, negative shift it upwards.
  70. *
  71. * @see {@link https://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/plotoptions/sankey-node-column/|Highcharts-Demo:}
  72. * Specified node offset
  73. *
  74. * @name Highcharts.SankeyNodeObject#offset
  75. * @type {number}
  76. * @default 0
  77. * @since 6.0.5
  78. */
  79. /**
  80. * Formatter callback function.
  81. *
  82. * @callback Highcharts.SeriesSankeyDataLabelsFormatterCallbackFunction
  83. *
  84. * @param {Highcharts.SeriesSankeyDataLabelsFormatterContextObject|Highcharts.PointLabelObject} this
  85. * Data label context to format
  86. *
  87. * @return {string|undefined}
  88. * Formatted data label text
  89. */
  90. /**
  91. * Context for the node formatter function.
  92. *
  93. * @interface Highcharts.SeriesSankeyDataLabelsFormatterContextObject
  94. * @extends Highcharts.PointLabelObject
  95. */ /**
  96. * The node object. The node name, if defined, is available through
  97. * `this.point.name`.
  98. * @name Highcharts.SeriesSankeyDataLabelsFormatterContextObject#point
  99. * @type {Highcharts.SankeyNodeObject}
  100. */
  101. ''; // detach doclets above
  102. // eslint-disable-next-line valid-jsdoc
  103. /**
  104. * @private
  105. */
  106. var getDLOptions = function getDLOptions(params) {
  107. var optionsPoint = (isObject(params.optionsPoint) ?
  108. params.optionsPoint.dataLabels :
  109. {}), optionsLevel = (isObject(params.level) ?
  110. params.level.dataLabels :
  111. {}), options = merge({
  112. style: {}
  113. }, optionsLevel, optionsPoint);
  114. return options;
  115. };
  116. /**
  117. * @private
  118. * @class
  119. * @name Highcharts.seriesTypes.sankey
  120. *
  121. * @augments Highcharts.Series
  122. */
  123. Series.seriesType('sankey', 'column',
  124. /**
  125. * A sankey diagram is a type of flow diagram, in which the width of the
  126. * link between two nodes is shown proportionally to the flow quantity.
  127. *
  128. * @sample highcharts/demo/sankey-diagram/
  129. * Sankey diagram
  130. * @sample highcharts/plotoptions/sankey-inverted/
  131. * Inverted sankey diagram
  132. * @sample highcharts/plotoptions/sankey-outgoing
  133. * Sankey diagram with outgoing links
  134. *
  135. * @extends plotOptions.column
  136. * @since 6.0.0
  137. * @product highcharts
  138. * @excluding animationLimit, boostThreshold, borderRadius,
  139. * crisp, cropThreshold, colorAxis, colorKey, depth, dragDrop,
  140. * edgeColor, edgeWidth, findNearestPointBy, grouping,
  141. * groupPadding, groupZPadding, maxPointWidth, negativeColor,
  142. * pointInterval, pointIntervalUnit, pointPadding,
  143. * pointPlacement, pointRange, pointStart, pointWidth,
  144. * shadow, softThreshold, stacking, threshold, zoneAxis,
  145. * zones, minPointLength, dataSorting, boostBlending
  146. * @requires modules/sankey
  147. * @optionparent plotOptions.sankey
  148. */
  149. {
  150. borderWidth: 0,
  151. colorByPoint: true,
  152. /**
  153. * Higher numbers makes the links in a sankey diagram or dependency
  154. * wheelrender more curved. A `curveFactor` of 0 makes the lines
  155. * straight.
  156. *
  157. * @private
  158. */
  159. curveFactor: 0.33,
  160. /**
  161. * Options for the data labels appearing on top of the nodes and links.
  162. * For sankey charts, data labels are visible for the nodes by default,
  163. * but hidden for links. This is controlled by modifying the
  164. * `nodeFormat`, and the `format` that applies to links and is an empty
  165. * string by default.
  166. *
  167. * @declare Highcharts.SeriesSankeyDataLabelsOptionsObject
  168. *
  169. * @private
  170. */
  171. dataLabels: {
  172. enabled: true,
  173. backgroundColor: 'none',
  174. crop: false,
  175. /**
  176. * The
  177. * [format string](https://www.highcharts.com/docs/chart-concepts/labels-and-string-formatting)
  178. * specifying what to show for _nodes_ in the sankey diagram. By
  179. * default the `nodeFormatter` returns `{point.name}`.
  180. *
  181. * @sample highcharts/plotoptions/sankey-link-datalabels/
  182. * Node and link data labels
  183. *
  184. * @type {string}
  185. */
  186. nodeFormat: void 0,
  187. // eslint-disable-next-line valid-jsdoc
  188. /**
  189. * Callback to format data labels for _nodes_ in the sankey diagram.
  190. * The `nodeFormat` option takes precedence over the
  191. * `nodeFormatter`.
  192. *
  193. * @type {Highcharts.SeriesSankeyDataLabelsFormatterCallbackFunction}
  194. * @since 6.0.2
  195. */
  196. nodeFormatter: function () {
  197. return this.point.name;
  198. },
  199. format: void 0,
  200. // eslint-disable-next-line valid-jsdoc
  201. /**
  202. * @type {Highcharts.SeriesSankeyDataLabelsFormatterCallbackFunction}
  203. */
  204. formatter: function () {
  205. return;
  206. },
  207. inside: true
  208. },
  209. /**
  210. * @ignore-option
  211. *
  212. * @private
  213. */
  214. inactiveOtherPoints: true,
  215. /**
  216. * Set options on specific levels. Takes precedence over series options,
  217. * but not node and link options.
  218. *
  219. * @sample highcharts/demo/sunburst
  220. * Sunburst chart
  221. *
  222. * @type {Array<*>}
  223. * @since 7.1.0
  224. * @apioption plotOptions.sankey.levels
  225. */
  226. /**
  227. * Can set `borderColor` on all nodes which lay on the same level.
  228. *
  229. * @type {Highcharts.ColorString}
  230. * @apioption plotOptions.sankey.levels.borderColor
  231. */
  232. /**
  233. * Can set `borderWidth` on all nodes which lay on the same level.
  234. *
  235. * @type {number}
  236. * @apioption plotOptions.sankey.levels.borderWidth
  237. */
  238. /**
  239. * Can set `color` on all nodes which lay on the same level.
  240. *
  241. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  242. * @apioption plotOptions.sankey.levels.color
  243. */
  244. /**
  245. * Can set `colorByPoint` on all nodes which lay on the same level.
  246. *
  247. * @type {boolean}
  248. * @default true
  249. * @apioption plotOptions.sankey.levels.colorByPoint
  250. */
  251. /**
  252. * Can set `dataLabels` on all points which lay on the same level.
  253. *
  254. * @extends plotOptions.sankey.dataLabels
  255. * @apioption plotOptions.sankey.levels.dataLabels
  256. */
  257. /**
  258. * Decides which level takes effect from the options set in the levels
  259. * object.
  260. *
  261. * @type {number}
  262. * @apioption plotOptions.sankey.levels.level
  263. */
  264. /**
  265. * Can set `linkOpacity` on all points which lay on the same level.
  266. *
  267. * @type {number}
  268. * @default 0.5
  269. * @apioption plotOptions.sankey.levels.linkOpacity
  270. */
  271. /**
  272. * Can set `states` on all nodes and points which lay on the same level.
  273. *
  274. * @extends plotOptions.sankey.states
  275. * @apioption plotOptions.sankey.levels.states
  276. */
  277. /**
  278. * Opacity for the links between nodes in the sankey diagram.
  279. *
  280. * @private
  281. */
  282. linkOpacity: 0.5,
  283. /**
  284. * The minimal width for a line of a sankey. By default,
  285. * 0 values are not shown.
  286. *
  287. * @sample highcharts/plotoptions/sankey-minlinkwidth
  288. * Sankey diagram with minimal link height
  289. *
  290. * @type {number}
  291. * @since 7.1.3
  292. * @default 0
  293. * @apioption plotOptions.sankey.minLinkWidth
  294. *
  295. * @private
  296. */
  297. minLinkWidth: 0,
  298. /**
  299. * The pixel width of each node in a sankey diagram or dependency wheel,
  300. * or the height in case the chart is inverted.
  301. *
  302. * @private
  303. */
  304. nodeWidth: 20,
  305. /**
  306. * The padding between nodes in a sankey diagram or dependency wheel, in
  307. * pixels.
  308. *
  309. * If the number of nodes is so great that it is possible to lay them
  310. * out within the plot area with the given `nodePadding`, they will be
  311. * rendered with a smaller padding as a strategy to avoid overflow.
  312. *
  313. * @private
  314. */
  315. nodePadding: 10,
  316. showInLegend: false,
  317. states: {
  318. hover: {
  319. /**
  320. * Opacity for the links between nodes in the sankey diagram in
  321. * hover mode.
  322. */
  323. linkOpacity: 1
  324. },
  325. /**
  326. * The opposite state of a hover for a single point node/link.
  327. *
  328. * @declare Highcharts.SeriesStatesInactiveOptionsObject
  329. */
  330. inactive: {
  331. /**
  332. * Opacity for the links between nodes in the sankey diagram in
  333. * inactive mode.
  334. */
  335. linkOpacity: 0.1,
  336. /**
  337. * Opacity of inactive markers.
  338. *
  339. * @type {number}
  340. * @apioption plotOptions.series.states.inactive.opacity
  341. */
  342. opacity: 0.1,
  343. /**
  344. * Animation when not hovering over the marker.
  345. *
  346. * @type {boolean|Partial<Highcharts.AnimationOptionsObject>}
  347. * @apioption plotOptions.series.states.inactive.animation
  348. */
  349. animation: {
  350. /** @internal */
  351. duration: 50
  352. }
  353. }
  354. },
  355. tooltip: {
  356. /**
  357. * A callback for defining the format for _nodes_ in the chart's
  358. * tooltip, as opposed to links.
  359. *
  360. * @type {Highcharts.FormatterCallbackFunction<Highcharts.SankeyNodeObject>}
  361. * @since 6.0.2
  362. * @apioption plotOptions.sankey.tooltip.nodeFormatter
  363. */
  364. /**
  365. * Whether the tooltip should follow the pointer or stay fixed on
  366. * the item.
  367. */
  368. followPointer: true,
  369. headerFormat: '<span style="font-size: 10px">{series.name}</span><br/>',
  370. pointFormat: '{point.fromNode.name} \u2192 {point.toNode.name}: <b>{point.weight}</b><br/>',
  371. /**
  372. * The
  373. * [format string](https://www.highcharts.com/docs/chart-concepts/labels-and-string-formatting)
  374. * specifying what to show for _nodes_ in tooltip of a diagram
  375. * series, as opposed to links.
  376. */
  377. nodeFormat: '{point.name}: <b>{point.sum}</b><br/>'
  378. }
  379. }, {
  380. isCartesian: false,
  381. invertable: true,
  382. forceDL: true,
  383. orderNodes: true,
  384. pointArrayMap: ['from', 'to'],
  385. // Create a single node that holds information on incoming and outgoing
  386. // links.
  387. createNode: NodesMixin.createNode,
  388. searchPoint: H.noop,
  389. setData: NodesMixin.setData,
  390. destroy: NodesMixin.destroy,
  391. /* eslint-disable valid-jsdoc */
  392. /**
  393. * Overridable function to get node padding, overridden in dependency
  394. * wheel series type.
  395. * @private
  396. */
  397. getNodePadding: function () {
  398. var nodePadding = this.options.nodePadding || 0;
  399. // If the number of columns is so great that they will overflow with
  400. // the given nodePadding, we sacrifice the padding in order to
  401. // render all nodes within the plot area (#11917).
  402. if (this.nodeColumns) {
  403. var maxLength = this.nodeColumns.reduce(function (acc, col) { return Math.max(acc, col.length); }, 0);
  404. if (maxLength * nodePadding > this.chart.plotSizeY) {
  405. nodePadding = this.chart.plotSizeY / maxLength;
  406. }
  407. }
  408. return nodePadding;
  409. },
  410. /**
  411. * Create a node column.
  412. * @private
  413. */
  414. createNodeColumn: function () {
  415. var series = this, chart = this.chart, column = [];
  416. column.sum = function () {
  417. return this.reduce(function (sum, node) {
  418. return sum + node.getSum();
  419. }, 0);
  420. };
  421. // Get the offset in pixels of a node inside the column.
  422. column.offset = function (node, factor) {
  423. var offset = 0, totalNodeOffset, nodePadding = series.nodePadding;
  424. for (var i = 0; i < column.length; i++) {
  425. var sum = column[i].getSum();
  426. var height = Math.max(sum * factor, series.options.minLinkWidth);
  427. if (sum) {
  428. totalNodeOffset = height + nodePadding;
  429. }
  430. else {
  431. // If node sum equals 0 nodePadding is missed #12453
  432. totalNodeOffset = 0;
  433. }
  434. if (column[i] === node) {
  435. return {
  436. relativeTop: offset + relativeLength(node.options.offset || 0, totalNodeOffset)
  437. };
  438. }
  439. offset += totalNodeOffset;
  440. }
  441. };
  442. // Get the top position of the column in pixels.
  443. column.top = function (factor) {
  444. var nodePadding = series.nodePadding;
  445. var height = this.reduce(function (height, node) {
  446. if (height > 0) {
  447. height += nodePadding;
  448. }
  449. var nodeHeight = Math.max(node.getSum() * factor, series.options.minLinkWidth);
  450. height += nodeHeight;
  451. return height;
  452. }, 0);
  453. return (chart.plotSizeY - height) / 2;
  454. };
  455. return column;
  456. },
  457. /**
  458. * Create node columns by analyzing the nodes and the relations between
  459. * incoming and outgoing links.
  460. * @private
  461. */
  462. createNodeColumns: function () {
  463. var columns = [];
  464. this.nodes.forEach(function (node) {
  465. var fromColumn = -1, fromNode, i, point;
  466. if (!defined(node.options.column)) {
  467. // No links to this node, place it left
  468. if (node.linksTo.length === 0) {
  469. node.column = 0;
  470. // There are incoming links, place it to the right of the
  471. // highest order column that links to this one.
  472. }
  473. else {
  474. for (i = 0; i < node.linksTo.length; i++) {
  475. point = node.linksTo[0];
  476. if (point.fromNode.column > fromColumn) {
  477. fromNode = point.fromNode;
  478. fromColumn = fromNode.column;
  479. }
  480. }
  481. node.column = fromColumn + 1;
  482. // Hanging layout for organization chart
  483. if (fromNode &&
  484. fromNode.options.layout === 'hanging') {
  485. node.hangsFrom = fromNode;
  486. i = -1; // Reuse existing variable i
  487. find(fromNode.linksFrom, function (link, index) {
  488. var found = link.toNode === node;
  489. if (found) {
  490. i = index;
  491. }
  492. return found;
  493. });
  494. node.column += i;
  495. }
  496. }
  497. }
  498. if (!columns[node.column]) {
  499. columns[node.column] = this.createNodeColumn();
  500. }
  501. columns[node.column].push(node);
  502. }, this);
  503. // Fill in empty columns (#8865)
  504. for (var i = 0; i < columns.length; i++) {
  505. if (typeof columns[i] === 'undefined') {
  506. columns[i] = this.createNodeColumn();
  507. }
  508. }
  509. return columns;
  510. },
  511. /**
  512. * Define hasData function for non-cartesian series.
  513. * @private
  514. * @return {boolean}
  515. * Returns true if the series has points at all.
  516. */
  517. hasData: function () {
  518. return !!this.processedXData.length; // != 0
  519. },
  520. /**
  521. * Return the presentational attributes.
  522. * @private
  523. */
  524. pointAttribs: function (point, state) {
  525. var series = this, level = point.isNode ? point.level : point.fromNode.level, levelOptions = series.mapOptionsToLevel[level || 0] || {}, options = point.options, stateOptions = (levelOptions.states && levelOptions.states[state]) || {}, values = [
  526. 'colorByPoint', 'borderColor', 'borderWidth', 'linkOpacity'
  527. ].reduce(function (obj, key) {
  528. obj[key] = pick(stateOptions[key], options[key], levelOptions[key], series.options[key]);
  529. return obj;
  530. }, {}), color = pick(stateOptions.color, options.color, values.colorByPoint ? point.color : levelOptions.color);
  531. // Node attributes
  532. if (point.isNode) {
  533. return {
  534. fill: color,
  535. stroke: values.borderColor,
  536. 'stroke-width': values.borderWidth
  537. };
  538. }
  539. // Link attributes
  540. return {
  541. fill: Color.parse(color).setOpacity(values.linkOpacity).get()
  542. };
  543. },
  544. /**
  545. * Extend generatePoints by adding the nodes, which are Point objects
  546. * but pushed to the this.nodes array.
  547. * @private
  548. */
  549. generatePoints: function () {
  550. NodesMixin.generatePoints.apply(this, arguments);
  551. /**
  552. * Order the nodes, starting with the root node(s). (#9818)
  553. * @private
  554. */
  555. function order(node, level) {
  556. // Prevents circular recursion:
  557. if (typeof node.level === 'undefined') {
  558. node.level = level;
  559. node.linksFrom.forEach(function (link) {
  560. if (link.toNode) {
  561. order(link.toNode, level + 1);
  562. }
  563. });
  564. }
  565. }
  566. if (this.orderNodes) {
  567. this.nodes
  568. // Identify the root node(s)
  569. .filter(function (node) {
  570. return node.linksTo.length === 0;
  571. })
  572. // Start by the root node(s) and recursively set the level
  573. // on all following nodes.
  574. .forEach(function (node) {
  575. order(node, 0);
  576. });
  577. stableSort(this.nodes, function (a, b) {
  578. return a.level - b.level;
  579. });
  580. }
  581. },
  582. /**
  583. * Run translation operations for one node.
  584. * @private
  585. */
  586. translateNode: function (node, column) {
  587. var translationFactor = this.translationFactor, chart = this.chart, options = this.options, sum = node.getSum(), height = Math.max(Math.round(sum * translationFactor), this.options.minLinkWidth), crisp = Math.round(options.borderWidth) % 2 / 2, nodeOffset = column.offset(node, translationFactor), fromNodeTop = Math.floor(pick(nodeOffset.absoluteTop, (column.top(translationFactor) +
  588. nodeOffset.relativeTop))) + crisp, left = Math.floor(this.colDistance * node.column +
  589. options.borderWidth / 2) + crisp, nodeLeft = chart.inverted ?
  590. chart.plotSizeX - left :
  591. left, nodeWidth = Math.round(this.nodeWidth);
  592. node.sum = sum;
  593. // If node sum is 0, don't render the rect #12453
  594. if (sum) {
  595. // Draw the node
  596. node.shapeType = 'rect';
  597. node.nodeX = nodeLeft;
  598. node.nodeY = fromNodeTop;
  599. if (!chart.inverted) {
  600. node.shapeArgs = {
  601. x: nodeLeft,
  602. y: fromNodeTop,
  603. width: node.options.width || options.width || nodeWidth,
  604. height: node.options.height || options.height || height
  605. };
  606. }
  607. else {
  608. node.shapeArgs = {
  609. x: nodeLeft - nodeWidth,
  610. y: chart.plotSizeY - fromNodeTop - height,
  611. width: node.options.height || options.height || nodeWidth,
  612. height: node.options.width || options.width || height
  613. };
  614. }
  615. node.shapeArgs.display = node.hasShape() ? '' : 'none';
  616. // Calculate data label options for the point
  617. node.dlOptions = getDLOptions({
  618. level: this.mapOptionsToLevel[node.level],
  619. optionsPoint: node.options
  620. });
  621. // Pass test in drawPoints
  622. node.plotY = 1;
  623. // Set the anchor position for tooltips
  624. node.tooltipPos = chart.inverted ? [
  625. chart.plotSizeY - node.shapeArgs.y - node.shapeArgs.height / 2,
  626. chart.plotSizeX - node.shapeArgs.x - node.shapeArgs.width / 2
  627. ] : [
  628. node.shapeArgs.x + node.shapeArgs.width / 2,
  629. node.shapeArgs.y + node.shapeArgs.height / 2
  630. ];
  631. }
  632. else {
  633. node.dlOptions = {
  634. enabled: false
  635. };
  636. }
  637. },
  638. /**
  639. * Run translation operations for one link.
  640. * @private
  641. */
  642. translateLink: function (point) {
  643. var getY = function (node, fromOrTo) {
  644. var _a;
  645. var linkTop = (node.offset(point, fromOrTo) *
  646. translationFactor);
  647. var y = Math.min(node.nodeY + linkTop,
  648. // Prevent links from spilling below the node (#12014)
  649. node.nodeY + ((_a = node.shapeArgs) === null || _a === void 0 ? void 0 : _a.height) - linkHeight);
  650. return y;
  651. };
  652. var fromNode = point.fromNode, toNode = point.toNode, chart = this.chart, translationFactor = this.translationFactor, linkHeight = Math.max(point.weight * translationFactor, this.options.minLinkWidth), options = this.options, curvy = ((chart.inverted ? -this.colDistance : this.colDistance) *
  653. options.curveFactor), fromY = getY(fromNode, 'linksFrom'), toY = getY(toNode, 'linksTo'), nodeLeft = fromNode.nodeX, nodeW = this.nodeWidth, right = toNode.column * this.colDistance, outgoing = point.outgoing, straight = right > nodeLeft + nodeW;
  654. if (chart.inverted) {
  655. fromY = chart.plotSizeY - fromY;
  656. toY = (chart.plotSizeY || 0) - toY;
  657. right = chart.plotSizeX - right;
  658. nodeW = -nodeW;
  659. linkHeight = -linkHeight;
  660. straight = nodeLeft > right;
  661. }
  662. point.shapeType = 'path';
  663. point.linkBase = [
  664. fromY,
  665. fromY + linkHeight,
  666. toY,
  667. toY + linkHeight
  668. ];
  669. // Links going from left to right
  670. if (straight && typeof toY === 'number') {
  671. point.shapeArgs = {
  672. d: [
  673. ['M', nodeLeft + nodeW, fromY],
  674. [
  675. 'C',
  676. nodeLeft + nodeW + curvy,
  677. fromY,
  678. right - curvy,
  679. toY,
  680. right,
  681. toY
  682. ],
  683. ['L', right + (outgoing ? nodeW : 0), toY + linkHeight / 2],
  684. ['L', right, toY + linkHeight],
  685. [
  686. 'C',
  687. right - curvy,
  688. toY + linkHeight,
  689. nodeLeft + nodeW + curvy,
  690. fromY + linkHeight,
  691. nodeLeft + nodeW, fromY + linkHeight
  692. ],
  693. ['Z']
  694. ]
  695. };
  696. // Experimental: Circular links pointing backwards. In
  697. // v6.1.0 this breaks the rendering completely, so even
  698. // this experimental rendering is an improvement. #8218.
  699. // @todo
  700. // - Make room for the link in the layout
  701. // - Automatically determine if the link should go up or
  702. // down.
  703. }
  704. else if (typeof toY === 'number') {
  705. var bend = 20, vDist = chart.plotHeight - fromY - linkHeight, x1 = right - bend - linkHeight, x2 = right - bend, x3 = right, x4 = nodeLeft + nodeW, x5 = x4 + bend, x6 = x5 + linkHeight, fy1 = fromY, fy2 = fromY + linkHeight, fy3 = fy2 + bend, y4 = fy3 + vDist, y5 = y4 + bend, y6 = y5 + linkHeight, ty1 = toY, ty2 = ty1 + linkHeight, ty3 = ty2 + bend, cfy1 = fy2 - linkHeight * 0.7, cy2 = y5 + linkHeight * 0.7, cty1 = ty2 - linkHeight * 0.7, cx1 = x3 - linkHeight * 0.7, cx2 = x4 + linkHeight * 0.7;
  706. point.shapeArgs = {
  707. d: [
  708. ['M', x4, fy1],
  709. ['C', cx2, fy1, x6, cfy1, x6, fy3],
  710. ['L', x6, y4],
  711. ['C', x6, cy2, cx2, y6, x4, y6],
  712. ['L', x3, y6],
  713. ['C', cx1, y6, x1, cy2, x1, y4],
  714. ['L', x1, ty3],
  715. ['C', x1, cty1, cx1, ty1, x3, ty1],
  716. ['L', x3, ty2],
  717. ['C', x2, ty2, x2, ty2, x2, ty3],
  718. ['L', x2, y4],
  719. ['C', x2, y5, x2, y5, x3, y5],
  720. ['L', x4, y5],
  721. ['C', x5, y5, x5, y5, x5, y4],
  722. ['L', x5, fy3],
  723. ['C', x5, fy2, x5, fy2, x4, fy2],
  724. ['Z']
  725. ]
  726. };
  727. }
  728. // Place data labels in the middle
  729. point.dlBox = {
  730. x: nodeLeft + (right - nodeLeft + nodeW) / 2,
  731. y: fromY + (toY - fromY) / 2,
  732. height: linkHeight,
  733. width: 0
  734. };
  735. // And set the tooltip anchor in the middle
  736. point.tooltipPos = chart.inverted ? [
  737. chart.plotSizeY - point.dlBox.y - linkHeight / 2,
  738. chart.plotSizeX - point.dlBox.x
  739. ] : [
  740. point.dlBox.x,
  741. point.dlBox.y + linkHeight / 2
  742. ];
  743. // Pass test in drawPoints
  744. point.y = point.plotY = 1;
  745. if (!point.color) {
  746. point.color = fromNode.color;
  747. }
  748. },
  749. /**
  750. * Run pre-translation by generating the nodeColumns.
  751. * @private
  752. */
  753. translate: function () {
  754. var _this = this;
  755. // Get the translation factor needed for each column to fill up the
  756. // plot height
  757. var getColumnTranslationFactor = function (column) {
  758. var nodes = column.slice();
  759. var minLinkWidth = _this.options.minLinkWidth || 0;
  760. var exceedsMinLinkWidth;
  761. var factor = 0;
  762. var i;
  763. var remainingHeight = chart.plotSizeY -
  764. options.borderWidth - (column.length - 1) * series.nodePadding;
  765. // Because the minLinkWidth option doesn't obey the direct
  766. // translation, we need to run translation iteratively, check
  767. // node heights, remove those nodes affected by minLinkWidth,
  768. // check again, etc.
  769. while (column.length) {
  770. factor = remainingHeight / column.sum();
  771. exceedsMinLinkWidth = false;
  772. i = column.length;
  773. while (i--) {
  774. if (column[i].getSum() * factor < minLinkWidth) {
  775. column.splice(i, 1);
  776. remainingHeight -= minLinkWidth;
  777. exceedsMinLinkWidth = true;
  778. }
  779. }
  780. if (!exceedsMinLinkWidth) {
  781. break;
  782. }
  783. }
  784. // Re-insert original nodes
  785. column.length = 0;
  786. nodes.forEach(function (node) { return column.push(node); });
  787. return factor;
  788. };
  789. if (!this.processedXData) {
  790. this.processData();
  791. }
  792. this.generatePoints();
  793. this.nodeColumns = this.createNodeColumns();
  794. this.nodeWidth = relativeLength(this.options.nodeWidth, this.chart.plotSizeX);
  795. var series = this, chart = this.chart, options = this.options, nodeWidth = this.nodeWidth, nodeColumns = this.nodeColumns;
  796. this.nodePadding = this.getNodePadding();
  797. // Find out how much space is needed. Base it on the translation
  798. // factor of the most spaceous column.
  799. this.translationFactor = nodeColumns.reduce(function (translationFactor, column) { return Math.min(translationFactor, getColumnTranslationFactor(column)); }, Infinity);
  800. this.colDistance =
  801. (chart.plotSizeX - nodeWidth -
  802. options.borderWidth) / Math.max(1, nodeColumns.length - 1);
  803. // Calculate level options used in sankey and organization
  804. series.mapOptionsToLevel = getLevelOptions({
  805. // NOTE: if support for allowTraversingTree is added, then from
  806. // should be the level of the root node.
  807. from: 1,
  808. levels: options.levels,
  809. to: nodeColumns.length - 1,
  810. defaults: {
  811. borderColor: options.borderColor,
  812. borderRadius: options.borderRadius,
  813. borderWidth: options.borderWidth,
  814. color: series.color,
  815. colorByPoint: options.colorByPoint,
  816. // NOTE: if support for allowTraversingTree is added, then
  817. // levelIsConstant should be optional.
  818. levelIsConstant: true,
  819. linkColor: options.linkColor,
  820. linkLineWidth: options.linkLineWidth,
  821. linkOpacity: options.linkOpacity,
  822. states: options.states
  823. }
  824. });
  825. // First translate all nodes so we can use them when drawing links
  826. nodeColumns.forEach(function (column) {
  827. column.forEach(function (node) {
  828. series.translateNode(node, column);
  829. });
  830. }, this);
  831. // Then translate links
  832. this.nodes.forEach(function (node) {
  833. // Translate the links from this node
  834. node.linksFrom.forEach(function (linkPoint) {
  835. // If weight is 0 - don't render the link path #12453,
  836. // render null points (for organization chart)
  837. if ((linkPoint.weight || linkPoint.isNull) && linkPoint.to) {
  838. series.translateLink(linkPoint);
  839. linkPoint.allowShadow = false;
  840. }
  841. });
  842. });
  843. },
  844. /**
  845. * Extend the render function to also render this.nodes together with
  846. * the points.
  847. * @private
  848. */
  849. render: function () {
  850. var points = this.points;
  851. this.points = this.points.concat(this.nodes || []);
  852. ColumnSeries.prototype.render.call(this);
  853. this.points = points;
  854. },
  855. /* eslint-enable valid-jsdoc */
  856. animate: CartesianSeries.prototype.animate
  857. }, {
  858. applyOptions: function (options, x) {
  859. Point.prototype.applyOptions.call(this, options, x);
  860. // Treat point.level as a synonym of point.column
  861. if (defined(this.options.level)) {
  862. this.options.column = this.column = this.options.level;
  863. }
  864. return this;
  865. },
  866. setState: NodesMixin.setNodeState,
  867. getClassName: function () {
  868. return (this.isNode ? 'highcharts-node ' : 'highcharts-link ') +
  869. Point.prototype.getClassName.call(this);
  870. },
  871. isValid: function () {
  872. return this.isNode || typeof this.weight === 'number';
  873. }
  874. });
  875. /**
  876. * A `sankey` series. If the [type](#series.sankey.type) option is not
  877. * specified, it is inherited from [chart.type](#chart.type).
  878. *
  879. * @extends series,plotOptions.sankey
  880. * @excluding animationLimit, boostBlending, boostThreshold, borderColor,
  881. * borderRadius, borderWidth, crisp, cropThreshold, dataParser,
  882. * dataURL, depth, dragDrop, edgeColor, edgeWidth,
  883. * findNearestPointBy, getExtremesFromAll, grouping, groupPadding,
  884. * groupZPadding, label, maxPointWidth, negativeColor, pointInterval,
  885. * pointIntervalUnit, pointPadding, pointPlacement, pointRange,
  886. * pointStart, pointWidth, shadow, softThreshold, stacking,
  887. * threshold, zoneAxis, zones, dataSorting
  888. * @product highcharts
  889. * @requires modules/sankey
  890. * @apioption series.sankey
  891. */
  892. /**
  893. * A collection of options for the individual nodes. The nodes in a sankey
  894. * diagram are auto-generated instances of `Highcharts.Point`, but options can
  895. * be applied here and linked by the `id`.
  896. *
  897. * @sample highcharts/css/sankey/
  898. * Sankey diagram with node options
  899. *
  900. * @declare Highcharts.SeriesSankeyNodesOptionsObject
  901. * @type {Array<*>}
  902. * @product highcharts
  903. * @apioption series.sankey.nodes
  904. */
  905. /**
  906. * The id of the auto-generated node, refering to the `from` or `to` setting of
  907. * the link.
  908. *
  909. * @type {string}
  910. * @product highcharts
  911. * @apioption series.sankey.nodes.id
  912. */
  913. /**
  914. * The color of the auto generated node.
  915. *
  916. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  917. * @product highcharts
  918. * @apioption series.sankey.nodes.color
  919. */
  920. /**
  921. * The color index of the auto generated node, especially for use in styled
  922. * mode.
  923. *
  924. * @type {number}
  925. * @product highcharts
  926. * @apioption series.sankey.nodes.colorIndex
  927. */
  928. /**
  929. * An optional column index of where to place the node. The default behaviour is
  930. * to place it next to the preceding node. Note that this option name is
  931. * counter intuitive in inverted charts, like for example an organization chart
  932. * rendered top down. In this case the "columns" are horizontal.
  933. *
  934. * @sample highcharts/plotoptions/sankey-node-column/
  935. * Specified node column
  936. *
  937. * @type {number}
  938. * @since 6.0.5
  939. * @product highcharts
  940. * @apioption series.sankey.nodes.column
  941. */
  942. /**
  943. * Individual data label for each node. The options are the same as
  944. * the ones for [series.sankey.dataLabels](#series.sankey.dataLabels).
  945. *
  946. * @extends plotOptions.sankey.dataLabels
  947. * @apioption series.sankey.nodes.dataLabels
  948. */
  949. /**
  950. * An optional level index of where to place the node. The default behaviour is
  951. * to place it next to the preceding node. Alias of `nodes.column`, but in
  952. * inverted sankeys and org charts, the levels are laid out as rows.
  953. *
  954. * @type {number}
  955. * @since 7.1.0
  956. * @product highcharts
  957. * @apioption series.sankey.nodes.level
  958. */
  959. /**
  960. * The name to display for the node in data labels and tooltips. Use this when
  961. * the name is different from the `id`. Where the id must be unique for each
  962. * node, this is not necessary for the name.
  963. *
  964. * @sample highcharts/css/sankey/
  965. * Sankey diagram with node options
  966. *
  967. * @type {string}
  968. * @product highcharts
  969. * @apioption series.sankey.nodes.name
  970. */
  971. /**
  972. * In a horizontal layout, the vertical offset of a node in terms of weight.
  973. * Positive values shift the node downwards, negative shift it upwards. In a
  974. * vertical layout, like organization chart, the offset is horizontal.
  975. *
  976. * If a percantage string is given, the node is offset by the percentage of the
  977. * node size plus `nodePadding`.
  978. *
  979. * @sample highcharts/plotoptions/sankey-node-column/
  980. * Specified node offset
  981. *
  982. * @type {number|string}
  983. * @default 0
  984. * @since 6.0.5
  985. * @product highcharts
  986. * @apioption series.sankey.nodes.offset
  987. */
  988. /**
  989. * An array of data points for the series. For the `sankey` series type,
  990. * points can be given in the following way:
  991. *
  992. * An array of objects with named values. The following snippet shows only a
  993. * few settings, see the complete options set below. If the total number of data
  994. * points exceeds the series' [turboThreshold](#series.area.turboThreshold),
  995. * this option is not available.
  996. *
  997. * ```js
  998. * data: [{
  999. * from: 'Category1',
  1000. * to: 'Category2',
  1001. * weight: 2
  1002. * }, {
  1003. * from: 'Category1',
  1004. * to: 'Category3',
  1005. * weight: 5
  1006. * }]
  1007. * ```
  1008. *
  1009. * @sample {highcharts} highcharts/series/data-array-of-objects/
  1010. * Config objects
  1011. *
  1012. * @declare Highcharts.SeriesSankeyPointOptionsObject
  1013. * @type {Array<*>}
  1014. * @extends series.line.data
  1015. * @excluding dragDrop, drilldown, marker, x, y
  1016. * @product highcharts
  1017. * @apioption series.sankey.data
  1018. */
  1019. /**
  1020. * The color for the individual _link_. By default, the link color is the same
  1021. * as the node it extends from. The `series.fillOpacity` option also applies to
  1022. * the points, so when setting a specific link color, consider setting the
  1023. * `fillOpacity` to 1.
  1024. *
  1025. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  1026. * @product highcharts
  1027. * @apioption series.sankey.data.color
  1028. */
  1029. /**
  1030. * @type {Highcharts.SeriesSankeyDataLabelsOptionsObject|Array<Highcharts.SeriesSankeyDataLabelsOptionsObject>}
  1031. * @product highcharts
  1032. * @apioption series.sankey.data.dataLabels
  1033. */
  1034. /**
  1035. * The node that the link runs from.
  1036. *
  1037. * @type {string}
  1038. * @product highcharts
  1039. * @apioption series.sankey.data.from
  1040. */
  1041. /**
  1042. * The node that the link runs to.
  1043. *
  1044. * @type {string}
  1045. * @product highcharts
  1046. * @apioption series.sankey.data.to
  1047. */
  1048. /**
  1049. * Whether the link goes out of the system.
  1050. *
  1051. * @sample highcharts/plotoptions/sankey-outgoing
  1052. * Sankey chart with outgoing links
  1053. *
  1054. * @type {boolean}
  1055. * @default false
  1056. * @product highcharts
  1057. * @apioption series.sankey.data.outgoing
  1058. */
  1059. /**
  1060. * The weight of the link.
  1061. *
  1062. * @type {number|null}
  1063. * @product highcharts
  1064. * @apioption series.sankey.data.weight
  1065. */
  1066. ''; // adds doclets above to transpiled file