dependency-wheel.src.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /**
  2. * @license Highcharts JS v8.2.2 (2020-10-22)
  3. *
  4. * Dependency wheel module
  5. *
  6. * (c) 2010-2018 Torstein Honsi
  7. *
  8. * License: www.highcharts.com/license
  9. */
  10. 'use strict';
  11. (function (factory) {
  12. if (typeof module === 'object' && module.exports) {
  13. factory['default'] = factory;
  14. module.exports = factory;
  15. } else if (typeof define === 'function' && define.amd) {
  16. define('highcharts/modules/dependency-wheel', ['highcharts', 'highcharts/modules/sankey'], function (Highcharts) {
  17. factory(Highcharts);
  18. factory.Highcharts = Highcharts;
  19. return factory;
  20. });
  21. } else {
  22. factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
  23. }
  24. }(function (Highcharts) {
  25. var _modules = Highcharts ? Highcharts._modules : {};
  26. function _registerModule(obj, path, args, fn) {
  27. if (!obj.hasOwnProperty(path)) {
  28. obj[path] = fn.apply(null, args);
  29. }
  30. }
  31. _registerModule(_modules, 'Series/DependencyWheelSeries.js', [_modules['Core/Animation/AnimationUtilities.js'], _modules['Core/Series/Series.js'], _modules['Core/Globals.js'], _modules['Mixins/Nodes.js']], function (A, BaseSeries, H, NodesMixin) {
  32. /* *
  33. *
  34. * Dependency wheel module
  35. *
  36. * (c) 2018-2020 Torstein Honsi
  37. *
  38. * License: www.highcharts.com/license
  39. *
  40. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  41. *
  42. * */
  43. var animObject = A.animObject;
  44. var base = BaseSeries.seriesTypes.sankey.prototype;
  45. /**
  46. * @private
  47. * @class
  48. * @name Highcharts.seriesTypes.dependencywheel
  49. *
  50. * @augments Highcharts.seriesTypes.sankey
  51. */
  52. BaseSeries.seriesType('dependencywheel', 'sankey',
  53. /**
  54. * A dependency wheel chart is a type of flow diagram, where all nodes are
  55. * laid out in a circle, and the flow between the are drawn as link bands.
  56. *
  57. * @sample highcharts/demo/dependency-wheel/
  58. * Dependency wheel
  59. *
  60. * @extends plotOptions.sankey
  61. * @exclude dataSorting
  62. * @since 7.1.0
  63. * @product highcharts
  64. * @requires modules/dependency-wheel
  65. * @optionparent plotOptions.dependencywheel
  66. */
  67. {
  68. /**
  69. * The center of the wheel relative to the plot area. Can be
  70. * percentages or pixel values. The default behaviour is to
  71. * center the wheel inside the plot area.
  72. *
  73. * @type {Array<number|string|null>}
  74. * @default [null, null]
  75. * @product highcharts
  76. */
  77. center: [null, null],
  78. curveFactor: 0.6,
  79. /**
  80. * The start angle of the dependency wheel, in degrees where 0 is up.
  81. */
  82. startAngle: 0
  83. }, {
  84. orderNodes: false,
  85. getCenter: BaseSeries.seriesTypes.pie.prototype.getCenter,
  86. /* eslint-disable valid-jsdoc */
  87. /**
  88. * Dependency wheel has only one column, it runs along the perimeter.
  89. * @private
  90. */
  91. createNodeColumns: function () {
  92. var columns = [this.createNodeColumn()];
  93. this.nodes.forEach(function (node) {
  94. node.column = 0;
  95. columns[0].push(node);
  96. });
  97. return columns;
  98. },
  99. /**
  100. * Translate from vertical pixels to perimeter.
  101. * @private
  102. */
  103. getNodePadding: function () {
  104. return this.options.nodePadding / Math.PI;
  105. },
  106. createNode: function (id) {
  107. var node = base.createNode.call(this,
  108. id);
  109. node.index = this.nodes.length - 1;
  110. /**
  111. * Return the sum of incoming and outgoing links.
  112. * @private
  113. */
  114. node.getSum = function () {
  115. return node.linksFrom
  116. .concat(node.linksTo)
  117. .reduce(function (acc, link) {
  118. return acc + link.weight;
  119. }, 0);
  120. };
  121. /**
  122. * Get the offset in weight values of a point/link.
  123. * @private
  124. */
  125. node.offset = function (point) {
  126. var offset = 0,
  127. i,
  128. links = node.linksFrom.concat(node.linksTo),
  129. sliced;
  130. /**
  131. * @private
  132. */
  133. function otherNode(link) {
  134. if (link.fromNode === node) {
  135. return link.toNode;
  136. }
  137. return link.fromNode;
  138. }
  139. // Sort and slice the links to avoid links going out of each
  140. // node crossing each other.
  141. links.sort(function (a, b) {
  142. return otherNode(a).index - otherNode(b).index;
  143. });
  144. for (i = 0; i < links.length; i++) {
  145. if (otherNode(links[i]).index > node.index) {
  146. links = links.slice(0, i).reverse().concat(links.slice(i).reverse());
  147. sliced = true;
  148. break;
  149. }
  150. }
  151. if (!sliced) {
  152. links.reverse();
  153. }
  154. for (i = 0; i < links.length; i++) {
  155. if (links[i] === point) {
  156. return offset;
  157. }
  158. offset += links[i].weight;
  159. }
  160. };
  161. return node;
  162. },
  163. /**
  164. * @private
  165. * @todo Override the refactored sankey translateLink and translateNode
  166. * functions instead of the whole translate function.
  167. */
  168. translate: function () {
  169. var options = this.options,
  170. factor = 2 * Math.PI /
  171. (this.chart.plotHeight + this.getNodePadding()),
  172. center = this.getCenter(),
  173. startAngle = (options.startAngle - 90) * H.deg2rad;
  174. base.translate.call(this);
  175. this.nodeColumns[0].forEach(function (node) {
  176. // Don't render the nodes if sum is 0 #12453
  177. if (node.sum) {
  178. var shapeArgs = node.shapeArgs,
  179. centerX = center[0],
  180. centerY = center[1],
  181. r = center[2] / 2,
  182. innerR = r - options.nodeWidth,
  183. start = startAngle + factor * shapeArgs.y,
  184. end = startAngle +
  185. factor * (shapeArgs.y + shapeArgs.height);
  186. // Middle angle
  187. node.angle = start + (end - start) / 2;
  188. node.shapeType = 'arc';
  189. node.shapeArgs = {
  190. x: centerX,
  191. y: centerY,
  192. r: r,
  193. innerR: innerR,
  194. start: start,
  195. end: end
  196. };
  197. node.dlBox = {
  198. x: centerX + Math.cos((start + end) / 2) * (r + innerR) / 2,
  199. y: centerY + Math.sin((start + end) / 2) * (r + innerR) / 2,
  200. width: 1,
  201. height: 1
  202. };
  203. // Draw the links from this node
  204. node.linksFrom.forEach(function (point) {
  205. if (point.linkBase) {
  206. var distance;
  207. var corners = point.linkBase.map(function (top,
  208. i) {
  209. var angle = factor * top,
  210. x = Math.cos(startAngle + angle) * (innerR + 1),
  211. y = Math.sin(startAngle + angle) * (innerR + 1),
  212. curveFactor = options.curveFactor;
  213. // The distance between the from and to node
  214. // along the perimeter. This affect how curved
  215. // the link is, so that links between neighbours
  216. // don't extend too far towards the center.
  217. distance = Math.abs(point.linkBase[3 - i] * factor - angle);
  218. if (distance > Math.PI) {
  219. distance = 2 * Math.PI - distance;
  220. }
  221. distance = distance * innerR;
  222. if (distance < innerR) {
  223. curveFactor *= (distance / innerR);
  224. }
  225. return {
  226. x: centerX + x,
  227. y: centerY + y,
  228. cpX: centerX + (1 - curveFactor) * x,
  229. cpY: centerY + (1 - curveFactor) * y
  230. };
  231. });
  232. point.shapeArgs = {
  233. d: [[
  234. 'M',
  235. corners[0].x, corners[0].y
  236. ], [
  237. 'A',
  238. innerR, innerR,
  239. 0,
  240. 0,
  241. 1,
  242. corners[1].x, corners[1].y
  243. ], [
  244. 'C',
  245. corners[1].cpX, corners[1].cpY,
  246. corners[2].cpX, corners[2].cpY,
  247. corners[2].x, corners[2].y
  248. ], [
  249. 'A',
  250. innerR, innerR,
  251. 0,
  252. 0,
  253. 1,
  254. corners[3].x, corners[3].y
  255. ], [
  256. 'C',
  257. corners[3].cpX, corners[3].cpY,
  258. corners[0].cpX, corners[0].cpY,
  259. corners[0].x, corners[0].y
  260. ]]
  261. };
  262. }
  263. });
  264. }
  265. });
  266. },
  267. animate: function (init) {
  268. if (!init) {
  269. var duration = animObject(this.options.animation).duration,
  270. step = (duration / 2) / this.nodes.length;
  271. this.nodes.forEach(function (point, i) {
  272. var graphic = point.graphic;
  273. if (graphic) {
  274. graphic.attr({ opacity: 0 });
  275. setTimeout(function () {
  276. graphic.animate({ opacity: 1 }, { duration: step });
  277. }, step * i);
  278. }
  279. }, this);
  280. this.points.forEach(function (point) {
  281. var graphic = point.graphic;
  282. if (!point.isNode && graphic) {
  283. graphic.attr({ opacity: 0 })
  284. .animate({
  285. opacity: 1
  286. }, this.options.animation);
  287. }
  288. }, this);
  289. }
  290. }
  291. /* eslint-enable valid-jsdoc */
  292. },
  293. // Point class
  294. {
  295. setState: NodesMixin.setNodeState,
  296. /* eslint-disable valid-jsdoc */
  297. /**
  298. * Return a text path that the data label uses.
  299. * @private
  300. */
  301. getDataLabelPath: function (label) {
  302. var renderer = this.series.chart.renderer,
  303. shapeArgs = this.shapeArgs,
  304. upperHalf = this.angle < 0 || this.angle > Math.PI,
  305. start = shapeArgs.start,
  306. end = shapeArgs.end;
  307. if (!this.dataLabelPath) {
  308. this.dataLabelPath = renderer
  309. .arc({ open: true })
  310. // Add it inside the data label group so it gets destroyed
  311. // with the label
  312. .add(label);
  313. }
  314. this.dataLabelPath.attr({
  315. x: shapeArgs.x,
  316. y: shapeArgs.y,
  317. r: (shapeArgs.r +
  318. (this.dataLabel.options.distance || 0)),
  319. start: (upperHalf ? start : end),
  320. end: (upperHalf ? end : start),
  321. clockwise: +upperHalf
  322. });
  323. return this.dataLabelPath;
  324. },
  325. isValid: function () {
  326. // No null points here
  327. return true;
  328. }
  329. /* eslint-enable valid-jsdoc */
  330. });
  331. /**
  332. * A `dependencywheel` series. If the [type](#series.dependencywheel.type)
  333. * option is not specified, it is inherited from [chart.type](#chart.type).
  334. *
  335. * @extends series,plotOptions.dependencywheel
  336. * @exclude dataSorting
  337. * @product highcharts
  338. * @requires modules/sankey
  339. * @requires modules/dependency-wheel
  340. * @apioption series.dependencywheel
  341. */
  342. /**
  343. * A collection of options for the individual nodes. The nodes in a dependency
  344. * diagram are auto-generated instances of `Highcharts.Point`, but options can
  345. * be applied here and linked by the `id`.
  346. *
  347. * @extends series.sankey.nodes
  348. * @type {Array<*>}
  349. * @product highcharts
  350. * @excluding offset
  351. * @apioption series.dependencywheel.nodes
  352. */
  353. /**
  354. * An array of data points for the series. For the `dependencywheel` series
  355. * type, points can be given in the following way:
  356. *
  357. * An array of objects with named values. The following snippet shows only a
  358. * few settings, see the complete options set below. If the total number of data
  359. * points exceeds the series' [turboThreshold](#series.area.turboThreshold),
  360. * this option is not available.
  361. *
  362. * ```js
  363. * data: [{
  364. * from: 'Category1',
  365. * to: 'Category2',
  366. * weight: 2
  367. * }, {
  368. * from: 'Category1',
  369. * to: 'Category3',
  370. * weight: 5
  371. * }]
  372. * ```
  373. *
  374. * @type {Array<*>}
  375. * @extends series.sankey.data
  376. * @product highcharts
  377. * @excluding outgoing, dataLabels
  378. * @apioption series.dependencywheel.data
  379. */
  380. /**
  381. * Individual data label for each node. The options are the same as
  382. * the ones for [series.dependencywheel.dataLabels](#series.dependencywheel.dataLabels).
  383. *
  384. * @apioption series.dependencywheel.nodes.dataLabels
  385. */
  386. ''; // adds doclets above to the transpiled file
  387. });
  388. _registerModule(_modules, 'masters/modules/dependency-wheel.src.js', [], function () {
  389. });
  390. }));