DependencyWheelSeries.js 13 KB

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