dependency-wheel.src.js 15 KB

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