Nodes.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* *
  2. *
  3. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  4. *
  5. * */
  6. import H from '../Core/Globals.js';
  7. import Point from '../Core/Series/Point.js';
  8. import U from '../Core/Utilities.js';
  9. var defined = U.defined, extend = U.extend, find = U.find, pick = U.pick;
  10. import '../Core/Series/Series.js';
  11. var NodesMixin = H.NodesMixin = {
  12. /* eslint-disable valid-jsdoc */
  13. /**
  14. * Create a single node that holds information on incoming and outgoing
  15. * links.
  16. * @private
  17. */
  18. createNode: function (id) {
  19. /**
  20. * @private
  21. */
  22. function findById(nodes, id) {
  23. return find(nodes, function (node) {
  24. return node.id === id;
  25. });
  26. }
  27. var node = findById(this.nodes, id), PointClass = this.pointClass, options;
  28. if (!node) {
  29. options = this.options.nodes && findById(this.options.nodes, id);
  30. node = (new PointClass()).init(this, extend({
  31. className: 'highcharts-node',
  32. isNode: true,
  33. id: id,
  34. y: 1 // Pass isNull test
  35. }, options));
  36. node.linksTo = [];
  37. node.linksFrom = [];
  38. node.formatPrefix = 'node';
  39. node.name = node.name || node.options.id || ''; // for use in formats
  40. // Mass is used in networkgraph:
  41. node.mass = pick(
  42. // Node:
  43. node.options.mass, node.options.marker && node.options.marker.radius,
  44. // Series:
  45. this.options.marker && this.options.marker.radius,
  46. // Default:
  47. 4);
  48. /**
  49. * Return the largest sum of either the incoming or outgoing links.
  50. * @private
  51. */
  52. node.getSum = function () {
  53. var sumTo = 0, sumFrom = 0;
  54. node.linksTo.forEach(function (link) {
  55. sumTo += link.weight;
  56. });
  57. node.linksFrom.forEach(function (link) {
  58. sumFrom += link.weight;
  59. });
  60. return Math.max(sumTo, sumFrom);
  61. };
  62. /**
  63. * Get the offset in weight values of a point/link.
  64. * @private
  65. */
  66. node.offset = function (point, coll) {
  67. var offset = 0;
  68. for (var i = 0; i < node[coll].length; i++) {
  69. if (node[coll][i] === point) {
  70. return offset;
  71. }
  72. offset += node[coll][i].weight;
  73. }
  74. };
  75. // Return true if the node has a shape, otherwise all links are
  76. // outgoing.
  77. node.hasShape = function () {
  78. var outgoing = 0;
  79. node.linksTo.forEach(function (link) {
  80. if (link.outgoing) {
  81. outgoing++;
  82. }
  83. });
  84. return (!node.linksTo.length ||
  85. outgoing !== node.linksTo.length);
  86. };
  87. this.nodes.push(node);
  88. }
  89. return node;
  90. },
  91. /**
  92. * Extend generatePoints by adding the nodes, which are Point objects
  93. * but pushed to the this.nodes array.
  94. */
  95. generatePoints: function () {
  96. var chart = this.chart, nodeLookup = {};
  97. H.Series.prototype.generatePoints.call(this);
  98. if (!this.nodes) {
  99. this.nodes = []; // List of Point-like node items
  100. }
  101. this.colorCounter = 0;
  102. // Reset links from previous run
  103. this.nodes.forEach(function (node) {
  104. node.linksFrom.length = 0;
  105. node.linksTo.length = 0;
  106. node.level = node.options.level;
  107. });
  108. // Create the node list and set up links
  109. this.points.forEach(function (point) {
  110. if (defined(point.from)) {
  111. if (!nodeLookup[point.from]) {
  112. nodeLookup[point.from] = this.createNode(point.from);
  113. }
  114. nodeLookup[point.from].linksFrom.push(point);
  115. point.fromNode = nodeLookup[point.from];
  116. // Point color defaults to the fromNode's color
  117. if (chart.styledMode) {
  118. point.colorIndex = pick(point.options.colorIndex, nodeLookup[point.from].colorIndex);
  119. }
  120. else {
  121. point.color =
  122. point.options.color || nodeLookup[point.from].color;
  123. }
  124. }
  125. if (defined(point.to)) {
  126. if (!nodeLookup[point.to]) {
  127. nodeLookup[point.to] = this.createNode(point.to);
  128. }
  129. nodeLookup[point.to].linksTo.push(point);
  130. point.toNode = nodeLookup[point.to];
  131. }
  132. point.name = point.name || point.id; // for use in formats
  133. }, this);
  134. // Store lookup table for later use
  135. this.nodeLookup = nodeLookup;
  136. },
  137. // Destroy all nodes on setting new data
  138. setData: function () {
  139. if (this.nodes) {
  140. this.nodes.forEach(function (node) {
  141. node.destroy();
  142. });
  143. this.nodes.length = 0;
  144. }
  145. H.Series.prototype.setData.apply(this, arguments);
  146. },
  147. // Destroy alll nodes and links
  148. destroy: function () {
  149. // Nodes must also be destroyed (#8682, #9300)
  150. this.data = []
  151. .concat(this.points || [], this.nodes);
  152. return H.Series.prototype.destroy.apply(this, arguments);
  153. },
  154. /**
  155. * When hovering node, highlight all connected links. When hovering a link,
  156. * highlight all connected nodes.
  157. */
  158. setNodeState: function (state) {
  159. var args = arguments, others = this.isNode ? this.linksTo.concat(this.linksFrom) :
  160. [this.fromNode, this.toNode];
  161. if (state !== 'select') {
  162. others.forEach(function (linkOrNode) {
  163. if (linkOrNode && linkOrNode.series) {
  164. Point.prototype.setState.apply(linkOrNode, args);
  165. if (!linkOrNode.isNode) {
  166. if (linkOrNode.fromNode.graphic) {
  167. Point.prototype.setState.apply(linkOrNode.fromNode, args);
  168. }
  169. if (linkOrNode.toNode && linkOrNode.toNode.graphic) {
  170. Point.prototype.setState.apply(linkOrNode.toNode, args);
  171. }
  172. }
  173. }
  174. });
  175. }
  176. Point.prototype.setState.apply(this, args);
  177. }
  178. /* eslint-enable valid-jsdoc */
  179. };
  180. export default NodesMixin;