DependencyWheelSeries.js 12 KB

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