nodes.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import H from '../parts/Globals.js';
  2. H.NodesMixin = {
  3. // Create a single node that holds information on incoming and outgoing
  4. // links.
  5. createNode: function (id) {
  6. function findById(nodes, id) {
  7. return H.find(nodes, function (node) {
  8. return node.id === id;
  9. });
  10. }
  11. var node = findById(this.nodes, id),
  12. PointClass = this.pointClass,
  13. options;
  14. if (!node) {
  15. options = this.options.nodes && findById(this.options.nodes, id);
  16. node = (new PointClass()).init(
  17. this,
  18. H.extend({
  19. className: 'highcharts-node',
  20. isNode: true,
  21. id: id,
  22. y: 1 // Pass isNull test
  23. }, options)
  24. );
  25. node.linksTo = [];
  26. node.linksFrom = [];
  27. node.formatPrefix = 'node';
  28. node.name = node.name || node.options.id; // for use in formats
  29. // Return the largest sum of either the incoming or outgoing links.
  30. node.getSum = function () {
  31. var sumTo = 0,
  32. sumFrom = 0;
  33. node.linksTo.forEach(function (link) {
  34. sumTo += link.weight;
  35. });
  36. node.linksFrom.forEach(function (link) {
  37. sumFrom += link.weight;
  38. });
  39. return Math.max(sumTo, sumFrom);
  40. };
  41. // Get the offset in weight values of a point/link.
  42. node.offset = function (point, coll) {
  43. var offset = 0;
  44. for (var i = 0; i < node[coll].length; i++) {
  45. if (node[coll][i] === point) {
  46. return offset;
  47. }
  48. offset += node[coll][i].weight;
  49. }
  50. };
  51. // Return true if the node has a shape, otherwise all links are
  52. // outgoing.
  53. node.hasShape = function () {
  54. var outgoing = 0;
  55. node.linksTo.forEach(function (link) {
  56. if (link.outgoing) {
  57. outgoing++;
  58. }
  59. });
  60. return !node.linksTo.length || outgoing !== node.linksTo.length;
  61. };
  62. this.nodes.push(node);
  63. }
  64. return node;
  65. }
  66. };