dependency-wheel.src.js 16 KB

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