networkgraph.src.js 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  1. /**
  2. * @license Highcharts JS v7.0.2 (2019-01-17)
  3. * Force directed graph module
  4. *
  5. * (c) 2010-2019 Torstein Honsi
  6. *
  7. * License: www.highcharts.com/license
  8. */
  9. 'use strict';
  10. (function (factory) {
  11. if (typeof module === 'object' && module.exports) {
  12. factory['default'] = factory;
  13. module.exports = factory;
  14. } else if (typeof define === 'function' && define.amd) {
  15. define(function () {
  16. return factory;
  17. });
  18. } else {
  19. factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
  20. }
  21. }(function (Highcharts) {
  22. (function (H) {
  23. H.NodesMixin = {
  24. // Create a single node that holds information on incoming and outgoing
  25. // links.
  26. createNode: function (id) {
  27. function findById(nodes, id) {
  28. return H.find(nodes, function (node) {
  29. return node.id === id;
  30. });
  31. }
  32. var node = findById(this.nodes, id),
  33. PointClass = this.pointClass,
  34. options;
  35. if (!node) {
  36. options = this.options.nodes && findById(this.options.nodes, id);
  37. node = (new PointClass()).init(
  38. this,
  39. H.extend({
  40. className: 'highcharts-node',
  41. isNode: true,
  42. id: id,
  43. y: 1 // Pass isNull test
  44. }, options)
  45. );
  46. node.linksTo = [];
  47. node.linksFrom = [];
  48. node.formatPrefix = 'node';
  49. node.name = node.name || node.options.id; // for use in formats
  50. // Return the largest sum of either the incoming or outgoing links.
  51. node.getSum = function () {
  52. var sumTo = 0,
  53. 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. // Get the offset in weight values of a point/link.
  63. node.offset = function (point, coll) {
  64. var offset = 0;
  65. for (var i = 0; i < node[coll].length; i++) {
  66. if (node[coll][i] === point) {
  67. return offset;
  68. }
  69. offset += node[coll][i].weight;
  70. }
  71. };
  72. // Return true if the node has a shape, otherwise all links are
  73. // outgoing.
  74. node.hasShape = function () {
  75. var outgoing = 0;
  76. node.linksTo.forEach(function (link) {
  77. if (link.outgoing) {
  78. outgoing++;
  79. }
  80. });
  81. return !node.linksTo.length || outgoing !== node.linksTo.length;
  82. };
  83. this.nodes.push(node);
  84. }
  85. return node;
  86. }
  87. };
  88. }(Highcharts));
  89. (function (H) {
  90. /**
  91. * Networkgraph series
  92. *
  93. * (c) 2010-2019 Paweł Fus
  94. *
  95. * License: www.highcharts.com/license
  96. */
  97. var pick = H.pick;
  98. H.layouts = {
  99. 'reingold-fruchterman': function (options) {
  100. this.options = options;
  101. this.nodes = [];
  102. this.links = [];
  103. this.series = [];
  104. this.box = {
  105. x: 0,
  106. y: 0,
  107. width: 0,
  108. height: 0
  109. };
  110. this.setInitialRendering(true);
  111. }
  112. };
  113. H.extend(
  114. /**
  115. * Reingold-Fruchterman algorithm from
  116. * "Graph Drawing by Force-directed Placement" paper.
  117. */
  118. H.layouts['reingold-fruchterman'].prototype,
  119. {
  120. run: function () {
  121. var layout = this,
  122. series = this.series,
  123. options = this.options;
  124. if (layout.initialRendering) {
  125. layout.initPositions();
  126. // Render elements in initial positions:
  127. series.forEach(function (s) {
  128. s.render();
  129. });
  130. }
  131. // Algorithm:
  132. function localLayout() {
  133. // Barycenter forces:
  134. layout.applyBarycenterForces();
  135. // Repulsive forces:
  136. layout.applyRepulsiveForces();
  137. // Attractive forces:
  138. layout.applyAttractiveForces();
  139. // Limit to the plotting area and cool down:
  140. layout.applyLimits(layout.temperature);
  141. // Cool down:
  142. layout.temperature -= layout.diffTemperature;
  143. layout.prevSystemTemperature = layout.systemTemperature;
  144. layout.systemTemperature = layout.getSystemTemperature();
  145. if (options.enableSimulation) {
  146. series.forEach(function (s) {
  147. s.render();
  148. });
  149. if (
  150. layout.maxIterations-- &&
  151. !layout.isStable()
  152. ) {
  153. layout.simulation = H.win.requestAnimationFrame(
  154. localLayout
  155. );
  156. } else {
  157. layout.simulation = false;
  158. }
  159. }
  160. }
  161. layout.setK();
  162. layout.resetSimulation(options);
  163. if (options.enableSimulation) {
  164. // Animate it:
  165. layout.simulation = H.win.requestAnimationFrame(localLayout);
  166. } else {
  167. // Synchronous rendering:
  168. while (
  169. layout.maxIterations-- &&
  170. !layout.isStable()
  171. ) {
  172. localLayout();
  173. }
  174. series.forEach(function (s) {
  175. s.render();
  176. });
  177. }
  178. },
  179. stop: function () {
  180. if (this.simulation) {
  181. H.win.cancelAnimationFrame(this.simulation);
  182. }
  183. },
  184. setArea: function (x, y, w, h) {
  185. this.box = {
  186. left: x,
  187. top: y,
  188. width: w,
  189. height: h
  190. };
  191. },
  192. setK: function () {
  193. // Optimal distance between nodes,
  194. // available space around the node:
  195. this.k = this.options.linkLength ||
  196. Math.pow(
  197. this.box.width * this.box.height / this.nodes.length,
  198. 0.4
  199. );
  200. },
  201. addNodes: function (nodes) {
  202. nodes.forEach(function (node) {
  203. if (this.nodes.indexOf(node) === -1) {
  204. this.nodes.push(node);
  205. }
  206. }, this);
  207. },
  208. removeNode: function (node) {
  209. var index = this.nodes.indexOf(node);
  210. if (index !== -1) {
  211. this.nodes.splice(index, 1);
  212. }
  213. },
  214. removeLink: function (link) {
  215. var index = this.links.indexOf(link);
  216. if (index !== -1) {
  217. this.links.splice(index, 1);
  218. }
  219. },
  220. addLinks: function (links) {
  221. links.forEach(function (link) {
  222. if (this.links.indexOf(link) === -1) {
  223. this.links.push(link);
  224. }
  225. }, this);
  226. },
  227. addSeries: function (series) {
  228. if (this.series.indexOf(series) === -1) {
  229. this.series.push(series);
  230. }
  231. },
  232. clear: function () {
  233. this.nodes.length = 0;
  234. this.links.length = 0;
  235. this.series.length = 0;
  236. this.resetSimulation();
  237. },
  238. resetSimulation: function () {
  239. this.forcedStop = false;
  240. this.systemTemperature = 0;
  241. this.setMaxIterations();
  242. this.setTemperature();
  243. this.setDiffTemperature();
  244. },
  245. setMaxIterations: function (maxIterations) {
  246. this.maxIterations = pick(
  247. maxIterations,
  248. this.options.maxIterations
  249. );
  250. },
  251. setTemperature: function () {
  252. this.temperature = Math.sqrt(this.nodes.length);
  253. },
  254. setDiffTemperature: function () {
  255. this.diffTemperature = this.temperature /
  256. (this.options.maxIterations + 1);
  257. },
  258. setInitialRendering: function (enable) {
  259. this.initialRendering = enable;
  260. },
  261. initPositions: function () {
  262. var initialPositions = this.options.initialPositions;
  263. if (H.isFunction(initialPositions)) {
  264. initialPositions.call(this);
  265. } else if (initialPositions === 'circle') {
  266. this.setCircularPositions();
  267. } else {
  268. this.setRandomPositions();
  269. }
  270. },
  271. setCircularPositions: function () {
  272. var box = this.box,
  273. nodes = this.nodes,
  274. nodesLength = nodes.length + 1,
  275. angle = 2 * Math.PI / nodesLength,
  276. rootNodes = nodes.filter(function (node) {
  277. return node.linksTo.length === 0;
  278. }),
  279. sortedNodes = [],
  280. visitedNodes = {};
  281. function addToNodes(node) {
  282. node.linksFrom.forEach(function (link) {
  283. if (!visitedNodes[link.toNode.id]) {
  284. visitedNodes[link.toNode.id] = true;
  285. sortedNodes.push(link.toNode);
  286. addToNodes(link.toNode);
  287. }
  288. });
  289. }
  290. // Start with identified root nodes an sort the nodes by their
  291. // hierarchy. In trees, this ensures that branches don't cross
  292. // eachother.
  293. rootNodes.forEach(function (rootNode) {
  294. sortedNodes.push(rootNode);
  295. addToNodes(rootNode);
  296. });
  297. // Cyclic tree, no root node found
  298. if (!sortedNodes.length) {
  299. sortedNodes = nodes;
  300. // Dangling, cyclic trees
  301. } else {
  302. nodes.forEach(function (node) {
  303. if (sortedNodes.indexOf(node) === -1) {
  304. sortedNodes.push(node);
  305. }
  306. });
  307. }
  308. // Initial positions are laid out along a small circle, appearing
  309. // as a cluster in the middle
  310. sortedNodes.forEach(function (node, index) {
  311. node.plotX = pick(
  312. node.plotX,
  313. box.width / 2 + Math.cos(index * angle)
  314. );
  315. node.plotY = pick(
  316. node.plotY,
  317. box.height / 2 + Math.sin(index * angle)
  318. );
  319. node.dispX = 0;
  320. node.dispY = 0;
  321. });
  322. },
  323. setRandomPositions: function () {
  324. var box = this.box,
  325. nodes = this.nodes,
  326. nodesLength = nodes.length + 1;
  327. // Return a repeatable, quasi-random number based on an integer
  328. // input. For the initial positions
  329. function unrandom(n) {
  330. var rand = n * n / Math.PI;
  331. rand = rand - Math.floor(rand);
  332. return rand;
  333. }
  334. // Initial positions:
  335. nodes.forEach(
  336. function (node, index) {
  337. node.plotX = pick(
  338. node.plotX,
  339. box.width * unrandom(index)
  340. );
  341. node.plotY = pick(
  342. node.plotY,
  343. box.height * unrandom(nodesLength + index)
  344. );
  345. node.dispX = 0;
  346. node.dispY = 0;
  347. }
  348. );
  349. },
  350. applyBarycenterForces: function () {
  351. var nodesLength = this.nodes.length,
  352. gravitationalConstant = this.options.gravitationalConstant,
  353. cx = 0,
  354. cy = 0;
  355. // Calculate center:
  356. this.nodes.forEach(function (node) {
  357. cx += node.plotX;
  358. cy += node.plotY;
  359. });
  360. this.barycenter = {
  361. x: cx,
  362. y: cy
  363. };
  364. // Apply forces:
  365. this.nodes.forEach(function (node) {
  366. var degree = node.getDegree(),
  367. phi = degree * (1 + degree / 2);
  368. node.dispX = (cx / nodesLength - node.plotX) *
  369. gravitationalConstant * phi;
  370. node.dispY = (cy / nodesLength - node.plotY) *
  371. gravitationalConstant * phi;
  372. });
  373. },
  374. applyRepulsiveForces: function () {
  375. var layout = this,
  376. nodes = layout.nodes,
  377. options = layout.options,
  378. k = this.k;
  379. nodes.forEach(function (node) {
  380. nodes.forEach(function (repNode) {
  381. var force,
  382. distanceR,
  383. distanceXY;
  384. if (
  385. // Node can not repulse itself:
  386. node !== repNode &&
  387. // Only close nodes affect each other:
  388. /* layout.getDistR(node, repNode) < 2 * k && */
  389. // Not dragged:
  390. !node.fixedPosition
  391. ) {
  392. distanceXY = layout.getDistXY(node, repNode);
  393. distanceR = layout.vectorLength(distanceXY);
  394. if (distanceR !== 0) {
  395. force = options.repulsiveForce.call(
  396. layout, distanceR, k
  397. );
  398. node.dispX += (distanceXY.x / distanceR) * force;
  399. node.dispY += (distanceXY.y / distanceR) * force;
  400. }
  401. }
  402. });
  403. });
  404. },
  405. applyAttractiveForces: function () {
  406. var layout = this,
  407. links = layout.links,
  408. options = this.options,
  409. k = this.k;
  410. links.forEach(function (link) {
  411. if (link.fromNode && link.toNode) {
  412. var distanceXY = layout.getDistXY(
  413. link.fromNode,
  414. link.toNode
  415. ),
  416. distanceR = layout.vectorLength(distanceXY),
  417. force = options.attractiveForce.call(
  418. layout, distanceR, k
  419. );
  420. if (distanceR !== 0) {
  421. if (!link.fromNode.fixedPosition) {
  422. link.fromNode.dispX -= (distanceXY.x / distanceR) *
  423. force;
  424. link.fromNode.dispY -= (distanceXY.y / distanceR) *
  425. force;
  426. }
  427. if (!link.toNode.fixedPosition) {
  428. link.toNode.dispX += (distanceXY.x / distanceR) *
  429. force;
  430. link.toNode.dispY += (distanceXY.y / distanceR) *
  431. force;
  432. }
  433. }
  434. }
  435. });
  436. },
  437. applyLimits: function (temperature) {
  438. var layout = this,
  439. options = layout.options,
  440. nodes = layout.nodes,
  441. box = layout.box,
  442. distanceR;
  443. nodes.forEach(function (node) {
  444. if (node.fixedPosition) {
  445. return;
  446. }
  447. // Friction:
  448. node.dispX += options.friction * node.dispX;
  449. node.dispY += options.friction * node.dispY;
  450. distanceR = node.temperature = layout.vectorLength({
  451. x: node.dispX,
  452. y: node.dispY
  453. });
  454. // Place nodes:
  455. if (distanceR !== 0) {
  456. node.plotX += node.dispX / distanceR *
  457. Math.min(Math.abs(node.dispX), temperature);
  458. node.plotY += node.dispY / distanceR *
  459. Math.min(Math.abs(node.dispY), temperature);
  460. }
  461. /*
  462. TO DO: Consider elastic collision instead of stopping.
  463. o' means end position when hitting plotting area edge:
  464. - "inealstic":
  465. o
  466. \
  467. ______
  468. | o'
  469. | \
  470. | \
  471. - "elastic"/"bounced":
  472. o
  473. \
  474. ______
  475. | ^
  476. | / \
  477. |o' \
  478. */
  479. // Limit X-coordinates:
  480. node.plotX = Math.round(
  481. Math.max(
  482. Math.min(
  483. node.plotX,
  484. box.width
  485. ),
  486. box.left
  487. )
  488. );
  489. // Limit Y-coordinates:
  490. node.plotY = Math.round(
  491. Math.max(
  492. Math.min(
  493. node.plotY,
  494. box.height
  495. ),
  496. box.top
  497. )
  498. );
  499. // Reset displacement:
  500. node.dispX = 0;
  501. node.dispY = 0;
  502. });
  503. },
  504. isStable: function () {
  505. return Math.abs(
  506. this.systemTemperature -
  507. this.prevSystemTemperature
  508. ) === 0;
  509. },
  510. getSystemTemperature: function () {
  511. return this.nodes.reduce(function (value, node) {
  512. return value + node.temperature;
  513. }, 0);
  514. },
  515. vectorLength: function (vector) {
  516. return Math.sqrt(vector.x * vector.x + vector.y * vector.y);
  517. },
  518. getDistR: function (nodeA, nodeB) {
  519. var distance = this.getDistXY(nodeA, nodeB);
  520. return Math.sqrt(
  521. distance.x * distance.x +
  522. distance.y * distance.y
  523. );
  524. },
  525. getDistXY: function (nodeA, nodeB) {
  526. var xDist = nodeA.plotX - nodeB.plotX,
  527. yDist = nodeA.plotY - nodeB.plotY;
  528. return {
  529. x: xDist,
  530. y: yDist,
  531. absX: Math.abs(xDist),
  532. absY: Math.abs(yDist)
  533. };
  534. }
  535. }
  536. );
  537. }(Highcharts));
  538. (function (H) {
  539. /**
  540. * Networkgraph series
  541. *
  542. * (c) 2010-2019 Paweł Fus
  543. *
  544. * License: www.highcharts.com/license
  545. */
  546. var addEvent = H.addEvent,
  547. defined = H.defined,
  548. seriesType = H.seriesType,
  549. seriesTypes = H.seriesTypes,
  550. pick = H.pick,
  551. Chart = H.Chart,
  552. Point = H.Point,
  553. Series = H.Series;
  554. /**
  555. * A networkgraph is a type of relationship chart, where connnections
  556. * (links) attracts nodes (points) and other nodes repulse each other.
  557. *
  558. * @extends plotOptions.line
  559. * @product highcharts
  560. * @sample highcharts/demo/network-graph/
  561. * Networkgraph
  562. * @since 7.0.0
  563. * @excluding boostThreshold, animation, animationLimit, connectEnds,
  564. * connectNulls, dragDrop, getExtremesFromAll, label, linecap,
  565. * negativeColor, pointInterval, pointIntervalUnit,
  566. * pointPlacement, pointStart, softThreshold, stack, stacking,
  567. * step, threshold, xAxis, yAxis, zoneAxis
  568. * @optionparent plotOptions.networkgraph
  569. */
  570. seriesType('networkgraph', 'line', {
  571. marker: {
  572. enabled: true
  573. },
  574. dataLabels: {
  575. format: '{key}'
  576. },
  577. /**
  578. * Link style options
  579. */
  580. link: {
  581. /**
  582. * A name for the dash style to use for links.
  583. *
  584. * @type {String}
  585. * @apioption plotOptions.networkgraph.link.dashStyle
  586. * @defaults undefined
  587. */
  588. /**
  589. * Color of the link between two nodes.
  590. */
  591. color: 'rgba(100, 100, 100, 0.5)',
  592. /**
  593. * Width (px) of the link between two nodes.
  594. */
  595. width: 1
  596. },
  597. /**
  598. * Flag to determine if nodes are draggable or not.
  599. */
  600. draggable: true,
  601. layoutAlgorithm: {
  602. /**
  603. * Ideal length (px) of the link between two nodes. When not defined,
  604. * length is calculated as:
  605. * `Math.pow(availableWidth * availableHeight / nodesLength, 0.4);`
  606. *
  607. * Note: Because of the algorithm specification, length of each link
  608. * might be not exactly as specified.
  609. *
  610. * @type {number}
  611. * @apioption series.networkgraph.layoutAlgorithm.linkLength
  612. * @sample highcharts/series-networkgraph/styled-links/
  613. * Numerical values
  614. * @defaults undefined
  615. */
  616. /**
  617. * Initial layout algorithm for positioning nodes. Can be one of
  618. * built-in options ("circle", "random") or a function where positions
  619. * should be set on each node (`this.nodes`) as `node.plotX` and
  620. * `node.plotY`
  621. *
  622. * @sample highcharts/series-networkgraph/initial-positions/
  623. * Initial positions with callback
  624. * @type {String|Function}
  625. * @validvalue ["circle", "random"]
  626. */
  627. initialPositions: 'circle',
  628. /**
  629. * Experimental. Enables live simulation of the algorithm
  630. * implementation. All nodes are animated as the forces applies on
  631. * them.
  632. *
  633. * @sample highcharts/demo/network-graph/
  634. * Live simulation enabled
  635. */
  636. enableSimulation: false,
  637. /**
  638. * Type of the algorithm used when positioning nodes.
  639. *
  640. * @validvalue ["reingold-fruchterman"]
  641. */
  642. type: 'reingold-fruchterman',
  643. /**
  644. * Max number of iterations before algorithm will stop. In general,
  645. * algorithm should find positions sooner, but when rendering huge
  646. * number of nodes, it is recommended to increase this value as
  647. * finding perfect graph positions can require more time.
  648. */
  649. maxIterations: 1000,
  650. /**
  651. * Gravitational const used in the barycenter force of the algorithm.
  652. *
  653. * @sample highcharts/series-networkgraph/forces/
  654. * Custom forces
  655. */
  656. gravitationalConstant: 0.0625,
  657. /**
  658. * Friction applied on forces to prevent nodes rushing to fast to the
  659. * desired positions.
  660. */
  661. friction: -0.981,
  662. /**
  663. * Repulsive force applied on a node. Passed are two arguments:
  664. * - `d` - which is current distance between two nodes
  665. * - `k` - which is desired distance between two nodes
  666. *
  667. * @sample highcharts/series-networkgraph/forces/
  668. * Custom forces
  669. * @type {Function}
  670. * @default function (d, k) { return k * k / d; }
  671. */
  672. repulsiveForce: function (d, k) {
  673. /*
  674. basic, not recommended:
  675. return k / d;
  676. */
  677. /*
  678. standard:
  679. return k * k / d;
  680. */
  681. /*
  682. grid-variant:
  683. return k * k / d * (2 * k - d > 0 ? 1 : 0);
  684. */
  685. return k * k / d;
  686. },
  687. /**
  688. * Attraction force applied on a node which is conected to another node
  689. * by a link. Passed are two arguments:
  690. * - `d` - which is current distance between two nodes
  691. * - `k` - which is desired distance between two nodes
  692. *
  693. * @sample highcharts/series-networkgraph/forces/
  694. * Custom forces
  695. * @type {Function}
  696. * @default function (d, k) { return k * k / d; }
  697. */
  698. attractiveForce: function (d, k) {
  699. /*
  700. basic, not recommended:
  701. return d / k;
  702. */
  703. return d * d / k;
  704. }
  705. },
  706. showInLegend: false
  707. }, {
  708. isNetworkgraph: true,
  709. drawGraph: null,
  710. isCartesian: false,
  711. requireSorting: false,
  712. directTouch: true,
  713. noSharedTooltip: true,
  714. trackerGroups: ['group', 'markerGroup', 'dataLabelsGroup'],
  715. drawTracker: H.TrackerMixin.drawTrackerPoint,
  716. // Animation is run in `series.simulation`.
  717. animate: null,
  718. /**
  719. * Create a single node that holds information on incoming and outgoing
  720. * links.
  721. */
  722. createNode: H.NodesMixin.createNode,
  723. /**
  724. * Extend generatePoints by adding the nodes, which are Point objects
  725. * but pushed to the this.nodes array.
  726. */
  727. generatePoints: function () {
  728. var nodeLookup = {},
  729. chart = this.chart;
  730. H.Series.prototype.generatePoints.call(this);
  731. if (!this.nodes) {
  732. this.nodes = []; // List of Point-like node items
  733. }
  734. this.colorCounter = 0;
  735. // Reset links from previous run
  736. this.nodes.forEach(function (node) {
  737. node.linksFrom.length = 0;
  738. node.linksTo.length = 0;
  739. });
  740. // Create the node list and set up links
  741. this.points.forEach(function (point) {
  742. if (defined(point.from)) {
  743. if (!nodeLookup[point.from]) {
  744. nodeLookup[point.from] = this.createNode(point.from);
  745. }
  746. nodeLookup[point.from].linksFrom.push(point);
  747. point.fromNode = nodeLookup[point.from];
  748. // Point color defaults to the fromNode's color
  749. if (chart.styledMode) {
  750. point.colorIndex = pick(
  751. point.options.colorIndex,
  752. nodeLookup[point.from].colorIndex
  753. );
  754. } else {
  755. point.color =
  756. point.options.color || nodeLookup[point.from].color;
  757. }
  758. }
  759. if (defined(point.to)) {
  760. if (!nodeLookup[point.to]) {
  761. nodeLookup[point.to] = this.createNode(point.to);
  762. }
  763. nodeLookup[point.to].linksTo.push(point);
  764. point.toNode = nodeLookup[point.to];
  765. }
  766. point.name = point.name || point.id; // for use in formats
  767. }, this);
  768. if (this.options.nodes) {
  769. this.options.nodes.forEach(
  770. function (nodeOptions) {
  771. if (!nodeLookup[nodeOptions.id]) {
  772. nodeLookup[nodeOptions.id] = this
  773. .createNode(nodeOptions.id);
  774. }
  775. },
  776. this
  777. );
  778. }
  779. },
  780. /**
  781. * Run pre-translation by generating the nodeColumns.
  782. */
  783. translate: function () {
  784. if (!this.processedXData) {
  785. this.processData();
  786. }
  787. this.generatePoints();
  788. this.deferLayout();
  789. this.nodes.forEach(function (node) {
  790. // Draw the links from this node
  791. node.isInside = true;
  792. node.linksFrom.forEach(function (point) {
  793. point.shapeType = 'path';
  794. // Pass test in drawPoints
  795. point.y = 1;
  796. });
  797. });
  798. },
  799. deferLayout: function () {
  800. var layoutOptions = this.options.layoutAlgorithm,
  801. graphLayoutsStorage = this.chart.graphLayoutsStorage,
  802. chartOptions = this.chart.options.chart,
  803. layout;
  804. if (!this.visible) {
  805. return;
  806. }
  807. if (!graphLayoutsStorage) {
  808. this.chart.graphLayoutsStorage = graphLayoutsStorage = {};
  809. }
  810. layout = graphLayoutsStorage[layoutOptions.type];
  811. if (!layout) {
  812. layoutOptions.enableSimulation = !defined(chartOptions.forExport) ?
  813. layoutOptions.enableSimulation :
  814. !chartOptions.forExport;
  815. graphLayoutsStorage[layoutOptions.type] = layout =
  816. new H.layouts[layoutOptions.type](layoutOptions);
  817. }
  818. this.layout = layout;
  819. layout.setArea(0, 0, this.chart.plotWidth, this.chart.plotHeight);
  820. layout.addSeries(this);
  821. layout.addNodes(this.nodes);
  822. layout.addLinks(this.points);
  823. },
  824. /**
  825. * Extend the render function to also render this.nodes together with
  826. * the points.
  827. */
  828. render: function () {
  829. var points = this.points,
  830. hoverPoint = this.chart.hoverPoint,
  831. dataLabels = [];
  832. // Render markers:
  833. this.points = this.nodes;
  834. seriesTypes.line.prototype.render.call(this);
  835. this.points = points;
  836. points.forEach(function (point) {
  837. if (point.fromNode && point.toNode) {
  838. point.renderLink();
  839. point.redrawLink();
  840. }
  841. });
  842. if (hoverPoint && hoverPoint.series === this) {
  843. this.redrawHalo(hoverPoint);
  844. }
  845. this.nodes.forEach(function (node) {
  846. if (node.dataLabel) {
  847. dataLabels.push(node.dataLabel);
  848. }
  849. });
  850. H.Chart.prototype.hideOverlappingLabels(dataLabels);
  851. },
  852. /*
  853. * Draggable mode:
  854. */
  855. redrawHalo: function (point) {
  856. if (point && this.halo) {
  857. this.halo.attr({
  858. d: point.haloPath(
  859. this.options.states.hover.halo.size
  860. )
  861. });
  862. }
  863. },
  864. onMouseDown: function (point, event) {
  865. var normalizedEvent = this.chart.pointer.normalize(event);
  866. point.fixedPosition = {
  867. chartX: normalizedEvent.chartX,
  868. chartY: normalizedEvent.chartY,
  869. plotX: point.plotX,
  870. plotY: point.plotY
  871. };
  872. },
  873. onMouseMove: function (point, event) {
  874. if (point.fixedPosition) {
  875. var series = this,
  876. chart = series.chart,
  877. normalizedEvent = chart.pointer.normalize(event),
  878. diffX = point.fixedPosition.chartX - normalizedEvent.chartX,
  879. diffY = point.fixedPosition.chartY - normalizedEvent.chartY,
  880. newPlotX,
  881. newPlotY;
  882. // At least 5px to apply change (avoids simple click):
  883. if (Math.abs(diffX) > 5 || Math.abs(diffY) > 5) {
  884. newPlotX = point.fixedPosition.plotX - diffX;
  885. newPlotY = point.fixedPosition.plotY - diffY;
  886. if (chart.isInsidePlot(newPlotX, newPlotY)) {
  887. point.plotX = newPlotX;
  888. point.plotY = newPlotY;
  889. series.redrawHalo();
  890. if (!series.layout.simulation) {
  891. // Start new simulation:
  892. if (!series.layout.enableSimulation) {
  893. // Run only one iteration to speed things up:
  894. series.layout.setMaxIterations(1);
  895. }
  896. // When dragging nodes, we don't need to calculate
  897. // initial positions and rendering nodes:
  898. series.layout.setInitialRendering(false);
  899. series.layout.run();
  900. // Restore defaults:
  901. series.layout.setInitialRendering(true);
  902. } else {
  903. // Extend current simulation:
  904. series.layout.resetSimulation();
  905. }
  906. }
  907. }
  908. }
  909. },
  910. onMouseUp: function (point) {
  911. if (point.fixedPosition) {
  912. this.layout.run();
  913. delete point.fixedPosition;
  914. }
  915. },
  916. destroy: function () {
  917. this.nodes.forEach(function (node) {
  918. node.destroy();
  919. });
  920. return Series.prototype.destroy.apply(this, arguments);
  921. }
  922. }, {
  923. getDegree: function () {
  924. var deg = this.isNode ? this.linksFrom.length + this.linksTo.length : 0;
  925. return deg === 0 ? 1 : deg;
  926. },
  927. // Links:
  928. getLinkAttribues: function () {
  929. var linkOptions = this.series.options.link,
  930. pointOptions = this.options;
  931. return {
  932. 'stroke-width': pick(pointOptions.width, linkOptions.width),
  933. stroke: pointOptions.color || linkOptions.color,
  934. dashstyle: pointOptions.dashStyle || linkOptions.dashStyle
  935. };
  936. },
  937. renderLink: function () {
  938. if (!this.graphic) {
  939. this.graphic = this.series.chart.renderer
  940. .path(
  941. this.getLinkPath(this.fromNode, this.toNode)
  942. )
  943. .attr(this.getLinkAttribues())
  944. .add(this.series.group);
  945. }
  946. },
  947. redrawLink: function () {
  948. if (this.graphic) {
  949. this.graphic.animate({
  950. d: this.getLinkPath(this.fromNode, this.toNode)
  951. });
  952. }
  953. },
  954. getLinkPath: function (from, to) {
  955. return [
  956. 'M',
  957. from.plotX,
  958. from.plotY,
  959. 'L',
  960. to.plotX,
  961. to.plotY
  962. ];
  963. /*
  964. IDEA: different link shapes?
  965. return [
  966. 'M',
  967. from.plotX,
  968. from.plotY,
  969. 'Q',
  970. (to.plotX + from.plotX) / 2,
  971. (to.plotY + from.plotY) / 2 + 15,
  972. to.plotX,
  973. to.plotY
  974. ];*/
  975. },
  976. // Default utils:
  977. destroy: function () {
  978. if (this.isNode) {
  979. this.linksFrom.forEach(
  980. function (linkFrom) {
  981. if (linkFrom.graphic) {
  982. linkFrom.graphic = linkFrom.graphic.destroy();
  983. }
  984. }
  985. );
  986. }
  987. return Point.prototype.destroy.apply(this, arguments);
  988. }
  989. });
  990. addEvent(seriesTypes.networkgraph, 'updatedData', function () {
  991. if (this.layout) {
  992. this.layout.stop();
  993. }
  994. });
  995. addEvent(seriesTypes.networkgraph.prototype.pointClass, 'remove', function () {
  996. if (this.series.layout) {
  997. if (this.isNode) {
  998. this.series.layout.removeNode(this);
  999. } else {
  1000. this.series.layout.removeLink(this);
  1001. }
  1002. }
  1003. });
  1004. /*
  1005. * Multiple series support:
  1006. */
  1007. // Clear previous layouts
  1008. addEvent(Chart, 'predraw', function () {
  1009. if (this.graphLayoutsStorage) {
  1010. H.objectEach(
  1011. this.graphLayoutsStorage,
  1012. function (layout) {
  1013. layout.stop();
  1014. }
  1015. );
  1016. }
  1017. });
  1018. addEvent(Chart, 'render', function () {
  1019. if (this.graphLayoutsStorage) {
  1020. H.setAnimation(false, this);
  1021. H.objectEach(
  1022. this.graphLayoutsStorage,
  1023. function (layout) {
  1024. layout.run();
  1025. }
  1026. );
  1027. }
  1028. });
  1029. /*
  1030. * Draggable mode:
  1031. */
  1032. addEvent(
  1033. seriesTypes.networkgraph.prototype.pointClass,
  1034. 'mouseOver',
  1035. function () {
  1036. H.css(this.series.chart.container, { cursor: 'move' });
  1037. }
  1038. );
  1039. addEvent(
  1040. seriesTypes.networkgraph.prototype.pointClass,
  1041. 'mouseOut',
  1042. function () {
  1043. H.css(this.series.chart.container, { cursor: 'default' });
  1044. }
  1045. );
  1046. addEvent(
  1047. Chart,
  1048. 'load',
  1049. function () {
  1050. var chart = this,
  1051. unbinders = [];
  1052. if (chart.container) {
  1053. unbinders.push(
  1054. addEvent(
  1055. chart.container,
  1056. 'mousedown',
  1057. function (event) {
  1058. var point = chart.hoverPoint;
  1059. if (
  1060. point &&
  1061. point.series &&
  1062. point.series.isNetworkgraph &&
  1063. point.series.options.draggable
  1064. ) {
  1065. point.series.onMouseDown(point, event);
  1066. unbinders.push(addEvent(
  1067. chart.container,
  1068. 'mousemove',
  1069. function (e) {
  1070. return point.series.onMouseMove(point, e);
  1071. }
  1072. ));
  1073. unbinders.push(addEvent(
  1074. chart.container.ownerDocument,
  1075. 'mouseup',
  1076. function (e) {
  1077. return point.series.onMouseUp(point, e);
  1078. }
  1079. ));
  1080. }
  1081. }
  1082. )
  1083. );
  1084. }
  1085. addEvent(chart, 'destroy', function () {
  1086. unbinders.forEach(function (unbind) {
  1087. unbind();
  1088. });
  1089. });
  1090. }
  1091. );
  1092. /**
  1093. * A `networkgraph` series. If the [type](#series.networkgraph.type) option is
  1094. * not specified, it is inherited from [chart.type](#chart.type).
  1095. *
  1096. * @type {Object}
  1097. * @extends series,plotOptions.networkgraph
  1098. * @excluding boostThreshold, animation, animationLimit, connectEnds,
  1099. * connectNulls, dragDrop, getExtremesFromAll, label, linecap,
  1100. * negativeColor, pointInterval, pointIntervalUnit,
  1101. * pointPlacement, pointStart, softThreshold, stack, stacking,
  1102. * step, threshold, xAxis, yAxis, zoneAxis
  1103. * @product highcharts
  1104. * @apioption series.networkgraph
  1105. */
  1106. /**
  1107. * An array of data points for the series. For the `networkgraph` series type,
  1108. * points can be given in the following way:
  1109. *
  1110. * An array of objects with named values. The following snippet shows only a
  1111. * few settings, see the complete options set below. If the total number of
  1112. * data points exceeds the series'
  1113. * [turboThreshold](#series.area.turboThreshold), this option is not available.
  1114. *
  1115. * ```js
  1116. * data: [{
  1117. * from: 'Category1',
  1118. * to: 'Category2'
  1119. * }, {
  1120. * from: 'Category1',
  1121. * to: 'Category3'
  1122. * }]
  1123. * ```
  1124. *
  1125. * @type {Array<Object|Array|Number>}
  1126. * @extends series.line.data
  1127. * @excluding drilldown,marker,x,y,draDrop
  1128. * @sample {highcharts} highcharts/chart/reflow-true/
  1129. * Numerical values
  1130. * @sample {highcharts} highcharts/series/data-array-of-arrays/
  1131. * Arrays of numeric x and y
  1132. * @sample {highcharts} highcharts/series/data-array-of-arrays-datetime/
  1133. * Arrays of datetime x and y
  1134. * @sample {highcharts} highcharts/series/data-array-of-name-value/
  1135. * Arrays of point.name and y
  1136. * @sample {highcharts} highcharts/series/data-array-of-objects/
  1137. * Config objects
  1138. * @product highcharts
  1139. * @apioption series.networkgraph.data
  1140. */
  1141. /**
  1142. * The node that the link runs from.
  1143. *
  1144. * @type {String}
  1145. * @product highcharts
  1146. * @apioption series.networkgraph.data.from
  1147. */
  1148. /**
  1149. * The node that the link runs to.
  1150. *
  1151. * @type {String}
  1152. * @product highcharts
  1153. * @apioption series.networkgraph.data.to
  1154. */
  1155. /**
  1156. * The weight of the link.
  1157. *
  1158. * @type {Number}
  1159. * @product highcharts
  1160. * @apioption series.networkgraph.data.weight
  1161. */
  1162. /**
  1163. * A collection of options for the individual nodes. The nodes in a
  1164. * networkgraph diagram are auto-generated instances of `Highcharts.Point`,
  1165. * but options can be applied here and linked by the `id`.
  1166. *
  1167. * @sample highcharts/series-networkgraph/data-options/
  1168. * Networkgraph diagram with node options
  1169. *
  1170. * @type {Array<*>}
  1171. * @product highcharts
  1172. * @apioption series.networkgraph.nodes
  1173. */
  1174. /**
  1175. * The id of the auto-generated node, refering to the `from` or `to` setting of
  1176. * the link.
  1177. *
  1178. * @type {string}
  1179. * @product highcharts
  1180. * @apioption series.networkgraph.nodes.id
  1181. */
  1182. /**
  1183. * The color of the auto generated node.
  1184. *
  1185. * @type {Highcharts.ColorString}
  1186. * @product highcharts
  1187. * @apioption series.networkgraph.nodes.color
  1188. */
  1189. /**
  1190. * The color index of the auto generated node, especially for use in styled
  1191. * mode.
  1192. *
  1193. * @type {number}
  1194. * @product highcharts
  1195. * @apioption series.networkgraph.nodes.colorIndex
  1196. */
  1197. /**
  1198. * The name to display for the node in data labels and tooltips. Use this when
  1199. * the name is different from the `id`. Where the id must be unique for each
  1200. * node, this is not necessary for the name.
  1201. *
  1202. * @sample highcharts/series-networkgraph/data-options/
  1203. * Networkgraph diagram with node options
  1204. *
  1205. * @type {string}
  1206. * @product highcharts
  1207. * @apioption series.networkgraph.nodes.name
  1208. */
  1209. }(Highcharts));
  1210. return (function () {
  1211. }());
  1212. }));