Layouts.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /* *
  2. *
  3. * Networkgraph series
  4. *
  5. * (c) 2010-2020 Paweł Fus
  6. *
  7. * License: www.highcharts.com/license
  8. *
  9. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  10. *
  11. * */
  12. 'use strict';
  13. import Chart from '../../Core/Chart/Chart.js';
  14. import H from '../../Core/Globals.js';
  15. import U from '../../Core/Utilities.js';
  16. var addEvent = U.addEvent, clamp = U.clamp, defined = U.defined, extend = U.extend, isFunction = U.isFunction, pick = U.pick, setAnimation = U.setAnimation;
  17. import './Integrations.js';
  18. import './QuadTree.js';
  19. /* eslint-disable no-invalid-this, valid-jsdoc */
  20. H.layouts = {
  21. 'reingold-fruchterman': function () {
  22. }
  23. };
  24. extend(
  25. /**
  26. * Reingold-Fruchterman algorithm from
  27. * "Graph Drawing by Force-directed Placement" paper.
  28. * @private
  29. */
  30. H.layouts['reingold-fruchterman'].prototype, {
  31. init: function (options) {
  32. this.options = options;
  33. this.nodes = [];
  34. this.links = [];
  35. this.series = [];
  36. this.box = {
  37. x: 0,
  38. y: 0,
  39. width: 0,
  40. height: 0
  41. };
  42. this.setInitialRendering(true);
  43. this.integration =
  44. H.networkgraphIntegrations[options.integration];
  45. this.enableSimulation = options.enableSimulation;
  46. this.attractiveForce = pick(options.attractiveForce, this.integration.attractiveForceFunction);
  47. this.repulsiveForce = pick(options.repulsiveForce, this.integration.repulsiveForceFunction);
  48. this.approximation = options.approximation;
  49. },
  50. updateSimulation: function (enable) {
  51. this.enableSimulation = pick(enable, this.options.enableSimulation);
  52. },
  53. start: function () {
  54. var layout = this, series = this.series, options = this.options;
  55. layout.currentStep = 0;
  56. layout.forces = series[0] && series[0].forces || [];
  57. layout.chart = series[0] && series[0].chart;
  58. if (layout.initialRendering) {
  59. layout.initPositions();
  60. // Render elements in initial positions:
  61. series.forEach(function (s) {
  62. s.finishedAnimating = true; // #13169
  63. s.render();
  64. });
  65. }
  66. layout.setK();
  67. layout.resetSimulation(options);
  68. if (layout.enableSimulation) {
  69. layout.step();
  70. }
  71. },
  72. step: function () {
  73. var layout = this, series = this.series, options = this.options;
  74. // Algorithm:
  75. layout.currentStep++;
  76. if (layout.approximation === 'barnes-hut') {
  77. layout.createQuadTree();
  78. layout.quadTree.calculateMassAndCenter();
  79. }
  80. layout.forces.forEach(function (forceName) {
  81. layout[forceName + 'Forces'](layout.temperature);
  82. });
  83. // Limit to the plotting area and cool down:
  84. layout.applyLimits(layout.temperature);
  85. // Cool down the system:
  86. layout.temperature = layout.coolDown(layout.startTemperature, layout.diffTemperature, layout.currentStep);
  87. layout.prevSystemTemperature = layout.systemTemperature;
  88. layout.systemTemperature = layout.getSystemTemperature();
  89. if (layout.enableSimulation) {
  90. series.forEach(function (s) {
  91. // Chart could be destroyed during the simulation
  92. if (s.chart) {
  93. s.render();
  94. }
  95. });
  96. if (layout.maxIterations-- &&
  97. isFinite(layout.temperature) &&
  98. !layout.isStable()) {
  99. if (layout.simulation) {
  100. H.win.cancelAnimationFrame(layout.simulation);
  101. }
  102. layout.simulation = H.win.requestAnimationFrame(function () {
  103. layout.step();
  104. });
  105. }
  106. else {
  107. layout.simulation = false;
  108. }
  109. }
  110. },
  111. stop: function () {
  112. if (this.simulation) {
  113. H.win.cancelAnimationFrame(this.simulation);
  114. }
  115. },
  116. setArea: function (x, y, w, h) {
  117. this.box = {
  118. left: x,
  119. top: y,
  120. width: w,
  121. height: h
  122. };
  123. },
  124. setK: function () {
  125. // Optimal distance between nodes,
  126. // available space around the node:
  127. this.k = this.options.linkLength || this.integration.getK(this);
  128. },
  129. addElementsToCollection: function (elements, collection) {
  130. elements.forEach(function (elem) {
  131. if (collection.indexOf(elem) === -1) {
  132. collection.push(elem);
  133. }
  134. });
  135. },
  136. removeElementFromCollection: function (element, collection) {
  137. var index = collection.indexOf(element);
  138. if (index !== -1) {
  139. collection.splice(index, 1);
  140. }
  141. },
  142. clear: function () {
  143. this.nodes.length = 0;
  144. this.links.length = 0;
  145. this.series.length = 0;
  146. this.resetSimulation();
  147. },
  148. resetSimulation: function () {
  149. this.forcedStop = false;
  150. this.systemTemperature = 0;
  151. this.setMaxIterations();
  152. this.setTemperature();
  153. this.setDiffTemperature();
  154. },
  155. restartSimulation: function () {
  156. if (!this.simulation) {
  157. // When dragging nodes, we don't need to calculate
  158. // initial positions and rendering nodes:
  159. this.setInitialRendering(false);
  160. // Start new simulation:
  161. if (!this.enableSimulation) {
  162. // Run only one iteration to speed things up:
  163. this.setMaxIterations(1);
  164. }
  165. else {
  166. this.start();
  167. }
  168. if (this.chart) {
  169. this.chart.redraw();
  170. }
  171. // Restore defaults:
  172. this.setInitialRendering(true);
  173. }
  174. else {
  175. // Extend current simulation:
  176. this.resetSimulation();
  177. }
  178. },
  179. setMaxIterations: function (maxIterations) {
  180. this.maxIterations = pick(maxIterations, this.options.maxIterations);
  181. },
  182. setTemperature: function () {
  183. this.temperature = this.startTemperature =
  184. Math.sqrt(this.nodes.length);
  185. },
  186. setDiffTemperature: function () {
  187. this.diffTemperature = this.startTemperature /
  188. (this.options.maxIterations + 1);
  189. },
  190. setInitialRendering: function (enable) {
  191. this.initialRendering = enable;
  192. },
  193. createQuadTree: function () {
  194. this.quadTree = new H.QuadTree(this.box.left, this.box.top, this.box.width, this.box.height);
  195. this.quadTree.insertNodes(this.nodes);
  196. },
  197. initPositions: function () {
  198. var initialPositions = this.options.initialPositions;
  199. if (isFunction(initialPositions)) {
  200. initialPositions.call(this);
  201. this.nodes.forEach(function (node) {
  202. if (!defined(node.prevX)) {
  203. node.prevX = node.plotX;
  204. }
  205. if (!defined(node.prevY)) {
  206. node.prevY = node.plotY;
  207. }
  208. node.dispX = 0;
  209. node.dispY = 0;
  210. });
  211. }
  212. else if (initialPositions === 'circle') {
  213. this.setCircularPositions();
  214. }
  215. else {
  216. this.setRandomPositions();
  217. }
  218. },
  219. setCircularPositions: function () {
  220. var box = this.box, nodes = this.nodes, nodesLength = nodes.length + 1, angle = 2 * Math.PI / nodesLength, rootNodes = nodes.filter(function (node) {
  221. return node.linksTo.length === 0;
  222. }), sortedNodes = [], visitedNodes = {}, radius = this.options.initialPositionRadius;
  223. /**
  224. * @private
  225. */
  226. function addToNodes(node) {
  227. node.linksFrom.forEach(function (link) {
  228. if (!visitedNodes[link.toNode.id]) {
  229. visitedNodes[link.toNode.id] = true;
  230. sortedNodes.push(link.toNode);
  231. addToNodes(link.toNode);
  232. }
  233. });
  234. }
  235. // Start with identified root nodes an sort the nodes by their
  236. // hierarchy. In trees, this ensures that branches don't cross
  237. // eachother.
  238. rootNodes.forEach(function (rootNode) {
  239. sortedNodes.push(rootNode);
  240. addToNodes(rootNode);
  241. });
  242. // Cyclic tree, no root node found
  243. if (!sortedNodes.length) {
  244. sortedNodes = nodes;
  245. // Dangling, cyclic trees
  246. }
  247. else {
  248. nodes.forEach(function (node) {
  249. if (sortedNodes.indexOf(node) === -1) {
  250. sortedNodes.push(node);
  251. }
  252. });
  253. }
  254. // Initial positions are laid out along a small circle, appearing
  255. // as a cluster in the middle
  256. sortedNodes.forEach(function (node, index) {
  257. node.plotX = node.prevX = pick(node.plotX, box.width / 2 + radius * Math.cos(index * angle));
  258. node.plotY = node.prevY = pick(node.plotY, box.height / 2 + radius * Math.sin(index * angle));
  259. node.dispX = 0;
  260. node.dispY = 0;
  261. });
  262. },
  263. setRandomPositions: function () {
  264. var box = this.box, nodes = this.nodes, nodesLength = nodes.length + 1;
  265. /**
  266. * Return a repeatable, quasi-random number based on an integer
  267. * input. For the initial positions
  268. * @private
  269. */
  270. function unrandom(n) {
  271. var rand = n * n / Math.PI;
  272. rand = rand - Math.floor(rand);
  273. return rand;
  274. }
  275. // Initial positions:
  276. nodes.forEach(function (node, index) {
  277. node.plotX = node.prevX = pick(node.plotX, box.width * unrandom(index));
  278. node.plotY = node.prevY = pick(node.plotY, box.height * unrandom(nodesLength + index));
  279. node.dispX = 0;
  280. node.dispY = 0;
  281. });
  282. },
  283. force: function (name) {
  284. this.integration[name].apply(this, Array.prototype.slice.call(arguments, 1));
  285. },
  286. barycenterForces: function () {
  287. this.getBarycenter();
  288. this.force('barycenter');
  289. },
  290. getBarycenter: function () {
  291. var systemMass = 0, cx = 0, cy = 0;
  292. this.nodes.forEach(function (node) {
  293. cx += node.plotX * node.mass;
  294. cy += node.plotY * node.mass;
  295. systemMass += node.mass;
  296. });
  297. this.barycenter = {
  298. x: cx,
  299. y: cy,
  300. xFactor: cx / systemMass,
  301. yFactor: cy / systemMass
  302. };
  303. return this.barycenter;
  304. },
  305. barnesHutApproximation: function (node, quadNode) {
  306. var layout = this, distanceXY = layout.getDistXY(node, quadNode), distanceR = layout.vectorLength(distanceXY), goDeeper, force;
  307. if (node !== quadNode && distanceR !== 0) {
  308. if (quadNode.isInternal) {
  309. // Internal node:
  310. if (quadNode.boxSize / distanceR <
  311. layout.options.theta &&
  312. distanceR !== 0) {
  313. // Treat as an external node:
  314. force = layout.repulsiveForce(distanceR, layout.k);
  315. layout.force('repulsive', node, force * quadNode.mass, distanceXY, distanceR);
  316. goDeeper = false;
  317. }
  318. else {
  319. // Go deeper:
  320. goDeeper = true;
  321. }
  322. }
  323. else {
  324. // External node, direct force:
  325. force = layout.repulsiveForce(distanceR, layout.k);
  326. layout.force('repulsive', node, force * quadNode.mass, distanceXY, distanceR);
  327. }
  328. }
  329. return goDeeper;
  330. },
  331. repulsiveForces: function () {
  332. var layout = this;
  333. if (layout.approximation === 'barnes-hut') {
  334. layout.nodes.forEach(function (node) {
  335. layout.quadTree.visitNodeRecursive(null, function (quadNode) {
  336. return layout.barnesHutApproximation(node, quadNode);
  337. });
  338. });
  339. }
  340. else {
  341. layout.nodes.forEach(function (node) {
  342. layout.nodes.forEach(function (repNode) {
  343. var force, distanceR, distanceXY;
  344. if (
  345. // Node can not repulse itself:
  346. node !== repNode &&
  347. // Only close nodes affect each other:
  348. // layout.getDistR(node, repNode) < 2 * k &&
  349. // Not dragged:
  350. !node.fixedPosition) {
  351. distanceXY = layout.getDistXY(node, repNode);
  352. distanceR = layout.vectorLength(distanceXY);
  353. if (distanceR !== 0) {
  354. force = layout.repulsiveForce(distanceR, layout.k);
  355. layout.force('repulsive', node, force * repNode.mass, distanceXY, distanceR);
  356. }
  357. }
  358. });
  359. });
  360. }
  361. },
  362. attractiveForces: function () {
  363. var layout = this, distanceXY, distanceR, force;
  364. layout.links.forEach(function (link) {
  365. if (link.fromNode && link.toNode) {
  366. distanceXY = layout.getDistXY(link.fromNode, link.toNode);
  367. distanceR = layout.vectorLength(distanceXY);
  368. if (distanceR !== 0) {
  369. force = layout.attractiveForce(distanceR, layout.k);
  370. layout.force('attractive', link, force, distanceXY, distanceR);
  371. }
  372. }
  373. });
  374. },
  375. applyLimits: function () {
  376. var layout = this, nodes = layout.nodes;
  377. nodes.forEach(function (node) {
  378. if (node.fixedPosition) {
  379. return;
  380. }
  381. layout.integration.integrate(layout, node);
  382. layout.applyLimitBox(node, layout.box);
  383. // Reset displacement:
  384. node.dispX = 0;
  385. node.dispY = 0;
  386. });
  387. },
  388. /**
  389. * External box that nodes should fall. When hitting an edge, node
  390. * should stop or bounce.
  391. * @private
  392. */
  393. applyLimitBox: function (node, box) {
  394. var radius = node.radius;
  395. /*
  396. TO DO: Consider elastic collision instead of stopping.
  397. o' means end position when hitting plotting area edge:
  398. - "inelastic":
  399. o
  400. \
  401. ______
  402. | o'
  403. | \
  404. | \
  405. - "elastic"/"bounced":
  406. o
  407. \
  408. ______
  409. | ^
  410. | / \
  411. |o' \
  412. Euler sample:
  413. if (plotX < 0) {
  414. plotX = 0;
  415. dispX *= -1;
  416. }
  417. if (plotX > box.width) {
  418. plotX = box.width;
  419. dispX *= -1;
  420. }
  421. */
  422. // Limit X-coordinates:
  423. node.plotX = clamp(node.plotX, box.left + radius, box.width - radius);
  424. // Limit Y-coordinates:
  425. node.plotY = clamp(node.plotY, box.top + radius, box.height - radius);
  426. },
  427. /**
  428. * From "A comparison of simulated annealing cooling strategies" by
  429. * Nourani and Andresen work.
  430. * @private
  431. */
  432. coolDown: function (temperature, temperatureStep, currentStep) {
  433. // Logarithmic:
  434. /*
  435. return Math.sqrt(this.nodes.length) -
  436. Math.log(
  437. currentStep * layout.diffTemperature
  438. );
  439. */
  440. // Exponential:
  441. /*
  442. var alpha = 0.1;
  443. layout.temperature = Math.sqrt(layout.nodes.length) *
  444. Math.pow(alpha, layout.diffTemperature);
  445. */
  446. // Linear:
  447. return temperature - temperatureStep * currentStep;
  448. },
  449. isStable: function () {
  450. return Math.abs(this.systemTemperature -
  451. this.prevSystemTemperature) < 0.00001 || this.temperature <= 0;
  452. },
  453. getSystemTemperature: function () {
  454. return this.nodes.reduce(function (value, node) {
  455. return value + node.temperature;
  456. }, 0);
  457. },
  458. vectorLength: function (vector) {
  459. return Math.sqrt(vector.x * vector.x + vector.y * vector.y);
  460. },
  461. getDistR: function (nodeA, nodeB) {
  462. var distance = this.getDistXY(nodeA, nodeB);
  463. return this.vectorLength(distance);
  464. },
  465. getDistXY: function (nodeA, nodeB) {
  466. var xDist = nodeA.plotX - nodeB.plotX, yDist = nodeA.plotY - nodeB.plotY;
  467. return {
  468. x: xDist,
  469. y: yDist,
  470. absX: Math.abs(xDist),
  471. absY: Math.abs(yDist)
  472. };
  473. }
  474. });
  475. /* ************************************************************************** *
  476. * Multiple series support:
  477. * ************************************************************************** */
  478. // Clear previous layouts
  479. addEvent(Chart, 'predraw', function () {
  480. if (this.graphLayoutsLookup) {
  481. this.graphLayoutsLookup.forEach(function (layout) {
  482. layout.stop();
  483. });
  484. }
  485. });
  486. addEvent(Chart, 'render', function () {
  487. var systemsStable, afterRender = false;
  488. /**
  489. * @private
  490. */
  491. function layoutStep(layout) {
  492. if (layout.maxIterations-- &&
  493. isFinite(layout.temperature) &&
  494. !layout.isStable() &&
  495. !layout.enableSimulation) {
  496. // Hook similar to build-in addEvent, but instead of
  497. // creating whole events logic, use just a function.
  498. // It's faster which is important for rAF code.
  499. // Used e.g. in packed-bubble series for bubble radius
  500. // calculations
  501. if (layout.beforeStep) {
  502. layout.beforeStep();
  503. }
  504. layout.step();
  505. systemsStable = false;
  506. afterRender = true;
  507. }
  508. }
  509. if (this.graphLayoutsLookup) {
  510. setAnimation(false, this);
  511. // Start simulation
  512. this.graphLayoutsLookup.forEach(function (layout) {
  513. layout.start();
  514. });
  515. // Just one sync step, to run different layouts similar to
  516. // async mode.
  517. while (!systemsStable) {
  518. systemsStable = true;
  519. this.graphLayoutsLookup.forEach(layoutStep);
  520. }
  521. if (afterRender) {
  522. this.series.forEach(function (s) {
  523. if (s && s.layout) {
  524. s.render();
  525. }
  526. });
  527. }
  528. }
  529. });
  530. // disable simulation before print if enabled
  531. addEvent(Chart, 'beforePrint', function () {
  532. if (this.graphLayoutsLookup) {
  533. this.graphLayoutsLookup.forEach(function (layout) {
  534. layout.updateSimulation(false);
  535. });
  536. this.redraw();
  537. }
  538. });
  539. // re-enable simulation after print
  540. addEvent(Chart, 'afterPrint', function () {
  541. if (this.graphLayoutsLookup) {
  542. this.graphLayoutsLookup.forEach(function (layout) {
  543. // return to default simulation
  544. layout.updateSimulation();
  545. });
  546. }
  547. this.redraw();
  548. });