nodes.js 6.5 KB

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