DependencyWheelSeries.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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_1 = (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. if (point.graphic) {
  84. point.graphic.animate({ opacity: 1 }, { duration: step_1 });
  85. }
  86. }, step_1 * i);
  87. }
  88. }, this);
  89. this.points.forEach(function (point) {
  90. var graphic = point.graphic;
  91. if (!point.isNode && graphic) {
  92. graphic.attr({ opacity: 0 })
  93. .animate({
  94. opacity: 1
  95. }, this.options.animation);
  96. }
  97. }, this);
  98. }
  99. };
  100. DependencyWheelSeries.prototype.createNode = function (id) {
  101. var node = SankeySeries.prototype.createNode.call(this, id);
  102. node.index = this.nodes.length - 1;
  103. /**
  104. * Return the sum of incoming and outgoing links.
  105. * @private
  106. */
  107. node.getSum = function () {
  108. return node.linksFrom
  109. .concat(node.linksTo)
  110. .reduce(function (acc, link) {
  111. return acc + link.weight;
  112. }, 0);
  113. };
  114. /**
  115. * Get the offset in weight values of a point/link.
  116. * @private
  117. */
  118. node.offset = function (point) {
  119. var offset = 0, i, links = node.linksFrom.concat(node.linksTo), sliced;
  120. /**
  121. * @private
  122. */
  123. function otherNode(link) {
  124. if (link.fromNode === node) {
  125. return link.toNode;
  126. }
  127. return link.fromNode;
  128. }
  129. // Sort and slice the links to avoid links going out of each
  130. // node crossing each other.
  131. links.sort(function (a, b) {
  132. return otherNode(a).index - otherNode(b).index;
  133. });
  134. for (i = 0; i < links.length; i++) {
  135. if (otherNode(links[i]).index > node.index) {
  136. links = links.slice(0, i).reverse().concat(links.slice(i).reverse());
  137. sliced = true;
  138. break;
  139. }
  140. }
  141. if (!sliced) {
  142. links.reverse();
  143. }
  144. for (i = 0; i < links.length; i++) {
  145. if (links[i] === point) {
  146. return offset;
  147. }
  148. offset += links[i].weight;
  149. }
  150. };
  151. return node;
  152. };
  153. /**
  154. * Dependency wheel has only one column, it runs along the perimeter.
  155. * @private
  156. */
  157. DependencyWheelSeries.prototype.createNodeColumns = function () {
  158. var columns = [this.createNodeColumn()];
  159. this.nodes.forEach(function (node) {
  160. node.column = 0;
  161. columns[0].push(node);
  162. });
  163. return columns;
  164. };
  165. /**
  166. * Translate from vertical pixels to perimeter.
  167. * @private
  168. */
  169. DependencyWheelSeries.prototype.getNodePadding = function () {
  170. return this.options.nodePadding / Math.PI;
  171. };
  172. /**
  173. * @private
  174. * @todo Override the refactored sankey translateLink and translateNode
  175. * functions instead of the whole translate function.
  176. */
  177. DependencyWheelSeries.prototype.translate = function () {
  178. var options = this.options, factor = 2 * Math.PI /
  179. (this.chart.plotHeight + this.getNodePadding()), center = this.getCenter(), startAngle = (options.startAngle - 90) * deg2rad;
  180. SankeySeries.prototype.translate.call(this);
  181. this.nodeColumns[0].forEach(function (node) {
  182. // Don't render the nodes if sum is 0 #12453
  183. if (node.sum) {
  184. var shapeArgs = node.shapeArgs, centerX_1 = center[0], centerY_1 = center[1], r = center[2] / 2, innerR_1 = r - options.nodeWidth, start = startAngle + factor * (shapeArgs.y || 0), end = startAngle +
  185. factor * ((shapeArgs.y || 0) + (shapeArgs.height || 0));
  186. // Middle angle
  187. node.angle = start + (end - start) / 2;
  188. node.shapeType = 'arc';
  189. node.shapeArgs = {
  190. x: centerX_1,
  191. y: centerY_1,
  192. r: r,
  193. innerR: innerR_1,
  194. start: start,
  195. end: end
  196. };
  197. node.dlBox = {
  198. x: centerX_1 + Math.cos((start + end) / 2) * (r + innerR_1) / 2,
  199. y: centerY_1 + Math.sin((start + end) / 2) * (r + innerR_1) / 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_1;
  207. var corners = point.linkBase.map(function (top, i) {
  208. var angle = factor * top, x = Math.cos(startAngle + angle) * (innerR_1 + 1), y = Math.sin(startAngle + angle) * (innerR_1 + 1), curveFactor = options.curveFactor;
  209. // The distance between the from and to node
  210. // along the perimeter. This affect how curved
  211. // the link is, so that links between neighbours
  212. // don't extend too far towards the center.
  213. distance_1 = Math.abs(point.linkBase[3 - i] * factor - angle);
  214. if (distance_1 > Math.PI) {
  215. distance_1 = 2 * Math.PI - distance_1;
  216. }
  217. distance_1 = distance_1 * innerR_1;
  218. if (distance_1 < innerR_1) {
  219. curveFactor *= (distance_1 / innerR_1);
  220. }
  221. return {
  222. x: centerX_1 + x,
  223. y: centerY_1 + y,
  224. cpX: centerX_1 + (1 - curveFactor) * x,
  225. cpY: centerY_1 + (1 - curveFactor) * y
  226. };
  227. });
  228. point.shapeArgs = {
  229. d: [[
  230. 'M',
  231. corners[0].x, corners[0].y
  232. ], [
  233. 'A',
  234. innerR_1, innerR_1,
  235. 0,
  236. 0,
  237. 1,
  238. corners[1].x, corners[1].y
  239. ], [
  240. 'C',
  241. corners[1].cpX, corners[1].cpY,
  242. corners[2].cpX, corners[2].cpY,
  243. corners[2].x, corners[2].y
  244. ], [
  245. 'A',
  246. innerR_1, innerR_1,
  247. 0,
  248. 0,
  249. 1,
  250. corners[3].x, corners[3].y
  251. ], [
  252. 'C',
  253. corners[3].cpX, corners[3].cpY,
  254. corners[0].cpX, corners[0].cpY,
  255. corners[0].x, corners[0].y
  256. ]]
  257. };
  258. }
  259. });
  260. }
  261. });
  262. };
  263. /**
  264. * A dependency wheel chart is a type of flow diagram, where all nodes are
  265. * laid out in a circle, and the flow between the are drawn as link bands.
  266. *
  267. * @sample highcharts/demo/dependency-wheel/
  268. * Dependency wheel
  269. *
  270. * @extends plotOptions.sankey
  271. * @exclude dataSorting
  272. * @since 7.1.0
  273. * @product highcharts
  274. * @requires modules/dependency-wheel
  275. * @optionparent plotOptions.dependencywheel
  276. */
  277. DependencyWheelSeries.defaultOptions = merge(SankeySeries.defaultOptions, {
  278. /**
  279. * The center of the wheel relative to the plot area. Can be
  280. * percentages or pixel values. The default behaviour is to
  281. * center the wheel inside the plot area.
  282. *
  283. * @type {Array<number|string|null>}
  284. * @default [null, null]
  285. * @product highcharts
  286. */
  287. center: [null, null],
  288. curveFactor: 0.6,
  289. /**
  290. * The start angle of the dependency wheel, in degrees where 0 is up.
  291. */
  292. startAngle: 0
  293. });
  294. return DependencyWheelSeries;
  295. }(SankeySeries));
  296. extend(DependencyWheelSeries.prototype, {
  297. orderNodes: false,
  298. getCenter: PieSeries.prototype.getCenter
  299. });
  300. DependencyWheelSeries.prototype.pointClass = DependencyWheelPoint;
  301. SeriesRegistry.registerSeriesType('dependencywheel', DependencyWheelSeries);
  302. /* *
  303. *
  304. * Default Export
  305. *
  306. * */
  307. export default DependencyWheelSeries;
  308. /* *
  309. *
  310. * API Options
  311. *
  312. * */
  313. /**
  314. * A `dependencywheel` series. If the [type](#series.dependencywheel.type)
  315. * option is not specified, it is inherited from [chart.type](#chart.type).
  316. *
  317. * @extends series,plotOptions.dependencywheel
  318. * @exclude dataSorting
  319. * @product highcharts
  320. * @requires modules/sankey
  321. * @requires modules/dependency-wheel
  322. * @apioption series.dependencywheel
  323. */
  324. /**
  325. * A collection of options for the individual nodes. The nodes in a dependency
  326. * diagram are auto-generated instances of `Highcharts.Point`, but options can
  327. * be applied here and linked by the `id`.
  328. *
  329. * @extends series.sankey.nodes
  330. * @type {Array<*>}
  331. * @product highcharts
  332. * @excluding offset
  333. * @apioption series.dependencywheel.nodes
  334. */
  335. /**
  336. * An array of data points for the series. For the `dependencywheel` series
  337. * type, points can be given in the following way:
  338. *
  339. * An array of objects with named values. The following snippet shows only a
  340. * few settings, see the complete options set below. If the total number of data
  341. * points exceeds the series' [turboThreshold](#series.area.turboThreshold),
  342. * this option is not available.
  343. *
  344. * ```js
  345. * data: [{
  346. * from: 'Category1',
  347. * to: 'Category2',
  348. * weight: 2
  349. * }, {
  350. * from: 'Category1',
  351. * to: 'Category3',
  352. * weight: 5
  353. * }]
  354. * ```
  355. *
  356. * @type {Array<*>}
  357. * @extends series.sankey.data
  358. * @product highcharts
  359. * @excluding outgoing, dataLabels
  360. * @apioption series.dependencywheel.data
  361. */
  362. /**
  363. * Individual data label for each node. The options are the same as
  364. * the ones for [series.dependencywheel.dataLabels](#series.dependencywheel.dataLabels).
  365. *
  366. * @apioption series.dependencywheel.nodes.dataLabels
  367. */
  368. ''; // adds doclets above to the transpiled file