networkgraph.src.js 117 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807
  1. /**
  2. * @license Highcharts JS v8.2.2 (2020-10-22)
  3. *
  4. * Force directed graph module
  5. *
  6. * (c) 2010-2019 Torstein Honsi
  7. *
  8. * License: www.highcharts.com/license
  9. */
  10. 'use strict';
  11. (function (factory) {
  12. if (typeof module === 'object' && module.exports) {
  13. factory['default'] = factory;
  14. module.exports = factory;
  15. } else if (typeof define === 'function' && define.amd) {
  16. define('highcharts/modules/networkgraph', ['highcharts'], function (Highcharts) {
  17. factory(Highcharts);
  18. factory.Highcharts = Highcharts;
  19. return factory;
  20. });
  21. } else {
  22. factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
  23. }
  24. }(function (Highcharts) {
  25. var _modules = Highcharts ? Highcharts._modules : {};
  26. function _registerModule(obj, path, args, fn) {
  27. if (!obj.hasOwnProperty(path)) {
  28. obj[path] = fn.apply(null, args);
  29. }
  30. }
  31. _registerModule(_modules, 'Mixins/Nodes.js', [_modules['Core/Globals.js'], _modules['Core/Series/Point.js'], _modules['Core/Utilities.js']], function (H, Point, U) {
  32. /* *
  33. *
  34. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  35. *
  36. * */
  37. var defined = U.defined,
  38. extend = U.extend,
  39. find = U.find,
  40. pick = U.pick;
  41. var NodesMixin = H.NodesMixin = {
  42. /* eslint-disable valid-jsdoc */
  43. /**
  44. * Create a single node that holds information on incoming and outgoing
  45. * links.
  46. * @private
  47. */
  48. createNode: function (id) {
  49. /**
  50. * @private
  51. */
  52. function findById(nodes,
  53. id) {
  54. return find(nodes,
  55. function (node) {
  56. return node.id === id;
  57. });
  58. }
  59. var node = findById(this.nodes,
  60. id),
  61. PointClass = this.pointClass,
  62. options;
  63. if (!node) {
  64. options = this.options.nodes && findById(this.options.nodes, id);
  65. node = (new PointClass()).init(this, extend({
  66. className: 'highcharts-node',
  67. isNode: true,
  68. id: id,
  69. y: 1 // Pass isNull test
  70. }, options));
  71. node.linksTo = [];
  72. node.linksFrom = [];
  73. node.formatPrefix = 'node';
  74. node.name = node.name || node.options.id || ''; // for use in formats
  75. // Mass is used in networkgraph:
  76. node.mass = pick(
  77. // Node:
  78. node.options.mass, node.options.marker && node.options.marker.radius,
  79. // Series:
  80. this.options.marker && this.options.marker.radius,
  81. // Default:
  82. 4);
  83. /**
  84. * Return the largest sum of either the incoming or outgoing links.
  85. * @private
  86. */
  87. node.getSum = function () {
  88. var sumTo = 0,
  89. sumFrom = 0;
  90. node.linksTo.forEach(function (link) {
  91. sumTo += link.weight;
  92. });
  93. node.linksFrom.forEach(function (link) {
  94. sumFrom += link.weight;
  95. });
  96. return Math.max(sumTo, sumFrom);
  97. };
  98. /**
  99. * Get the offset in weight values of a point/link.
  100. * @private
  101. */
  102. node.offset = function (point, coll) {
  103. var offset = 0;
  104. for (var i = 0; i < node[coll].length; i++) {
  105. if (node[coll][i] === point) {
  106. return offset;
  107. }
  108. offset += node[coll][i].weight;
  109. }
  110. };
  111. // Return true if the node has a shape, otherwise all links are
  112. // outgoing.
  113. node.hasShape = function () {
  114. var outgoing = 0;
  115. node.linksTo.forEach(function (link) {
  116. if (link.outgoing) {
  117. outgoing++;
  118. }
  119. });
  120. return (!node.linksTo.length ||
  121. outgoing !== node.linksTo.length);
  122. };
  123. this.nodes.push(node);
  124. }
  125. return node;
  126. },
  127. /**
  128. * Extend generatePoints by adding the nodes, which are Point objects
  129. * but pushed to the this.nodes array.
  130. */
  131. generatePoints: function () {
  132. var chart = this.chart,
  133. nodeLookup = {};
  134. H.Series.prototype.generatePoints.call(this);
  135. if (!this.nodes) {
  136. this.nodes = []; // List of Point-like node items
  137. }
  138. this.colorCounter = 0;
  139. // Reset links from previous run
  140. this.nodes.forEach(function (node) {
  141. node.linksFrom.length = 0;
  142. node.linksTo.length = 0;
  143. node.level = node.options.level;
  144. });
  145. // Create the node list and set up links
  146. this.points.forEach(function (point) {
  147. if (defined(point.from)) {
  148. if (!nodeLookup[point.from]) {
  149. nodeLookup[point.from] = this.createNode(point.from);
  150. }
  151. nodeLookup[point.from].linksFrom.push(point);
  152. point.fromNode = nodeLookup[point.from];
  153. // Point color defaults to the fromNode's color
  154. if (chart.styledMode) {
  155. point.colorIndex = pick(point.options.colorIndex, nodeLookup[point.from].colorIndex);
  156. }
  157. else {
  158. point.color =
  159. point.options.color || nodeLookup[point.from].color;
  160. }
  161. }
  162. if (defined(point.to)) {
  163. if (!nodeLookup[point.to]) {
  164. nodeLookup[point.to] = this.createNode(point.to);
  165. }
  166. nodeLookup[point.to].linksTo.push(point);
  167. point.toNode = nodeLookup[point.to];
  168. }
  169. point.name = point.name || point.id; // for use in formats
  170. }, this);
  171. // Store lookup table for later use
  172. this.nodeLookup = nodeLookup;
  173. },
  174. // Destroy all nodes on setting new data
  175. setData: function () {
  176. if (this.nodes) {
  177. this.nodes.forEach(function (node) {
  178. node.destroy();
  179. });
  180. this.nodes.length = 0;
  181. }
  182. H.Series.prototype.setData.apply(this, arguments);
  183. },
  184. // Destroy alll nodes and links
  185. destroy: function () {
  186. // Nodes must also be destroyed (#8682, #9300)
  187. this.data = []
  188. .concat(this.points || [], this.nodes);
  189. return H.Series.prototype.destroy.apply(this, arguments);
  190. },
  191. /**
  192. * When hovering node, highlight all connected links. When hovering a link,
  193. * highlight all connected nodes.
  194. */
  195. setNodeState: function (state) {
  196. var args = arguments,
  197. others = this.isNode ? this.linksTo.concat(this.linksFrom) :
  198. [this.fromNode,
  199. this.toNode];
  200. if (state !== 'select') {
  201. others.forEach(function (linkOrNode) {
  202. if (linkOrNode && linkOrNode.series) {
  203. Point.prototype.setState.apply(linkOrNode, args);
  204. if (!linkOrNode.isNode) {
  205. if (linkOrNode.fromNode.graphic) {
  206. Point.prototype.setState.apply(linkOrNode.fromNode, args);
  207. }
  208. if (linkOrNode.toNode && linkOrNode.toNode.graphic) {
  209. Point.prototype.setState.apply(linkOrNode.toNode, args);
  210. }
  211. }
  212. }
  213. });
  214. }
  215. Point.prototype.setState.apply(this, args);
  216. }
  217. /* eslint-enable valid-jsdoc */
  218. };
  219. return NodesMixin;
  220. });
  221. _registerModule(_modules, 'Series/Networkgraph/Integrations.js', [_modules['Core/Globals.js']], function (H) {
  222. /* *
  223. *
  224. * Networkgraph series
  225. *
  226. * (c) 2010-2020 Paweł Fus
  227. *
  228. * License: www.highcharts.com/license
  229. *
  230. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  231. *
  232. * */
  233. /* eslint-disable no-invalid-this, valid-jsdoc */
  234. H.networkgraphIntegrations = {
  235. verlet: {
  236. /**
  237. * Attractive force funtion. Can be replaced by API's
  238. * `layoutAlgorithm.attractiveForce`
  239. *
  240. * @private
  241. * @param {number} d current distance between two nodes
  242. * @param {number} k expected distance between two nodes
  243. * @return {number} force
  244. */
  245. attractiveForceFunction: function (d, k) {
  246. // Used in API:
  247. return (k - d) / d;
  248. },
  249. /**
  250. * Repulsive force funtion. Can be replaced by API's
  251. * `layoutAlgorithm.repulsiveForce`
  252. *
  253. * @private
  254. * @param {number} d current distance between two nodes
  255. * @param {number} k expected distance between two nodes
  256. * @return {number} force
  257. */
  258. repulsiveForceFunction: function (d, k) {
  259. // Used in API:
  260. return (k - d) / d * (k > d ? 1 : 0); // Force only for close nodes
  261. },
  262. /**
  263. * Barycenter force. Calculate and applys barycenter forces on the
  264. * nodes. Making them closer to the center of their barycenter point.
  265. *
  266. * In Verlet integration, force is applied on a node immidatelly to it's
  267. * `plotX` and `plotY` position.
  268. *
  269. * @private
  270. * @return {void}
  271. */
  272. barycenter: function () {
  273. var gravitationalConstant = this.options.gravitationalConstant,
  274. xFactor = this.barycenter.xFactor,
  275. yFactor = this.barycenter.yFactor;
  276. // To consider:
  277. xFactor = (xFactor - (this.box.left + this.box.width) / 2) *
  278. gravitationalConstant;
  279. yFactor = (yFactor - (this.box.top + this.box.height) / 2) *
  280. gravitationalConstant;
  281. this.nodes.forEach(function (node) {
  282. if (!node.fixedPosition) {
  283. node.plotX -=
  284. xFactor / node.mass / node.degree;
  285. node.plotY -=
  286. yFactor / node.mass / node.degree;
  287. }
  288. });
  289. },
  290. /**
  291. * Repulsive force.
  292. *
  293. * In Verlet integration, force is applied on a node immidatelly to it's
  294. * `plotX` and `plotY` position.
  295. *
  296. * @private
  297. * @param {Highcharts.Point} node
  298. * Node that should be translated by force.
  299. * @param {number} force
  300. * Force calcualated in `repulsiveForceFunction`
  301. * @param {Highcharts.PositionObject} distance
  302. * Distance between two nodes e.g. `{x, y}`
  303. * @return {void}
  304. */
  305. repulsive: function (node, force, distanceXY) {
  306. var factor = force * this.diffTemperature / node.mass / node.degree;
  307. if (!node.fixedPosition) {
  308. node.plotX += distanceXY.x * factor;
  309. node.plotY += distanceXY.y * factor;
  310. }
  311. },
  312. /**
  313. * Attractive force.
  314. *
  315. * In Verlet integration, force is applied on a node immidatelly to it's
  316. * `plotX` and `plotY` position.
  317. *
  318. * @private
  319. * @param {Highcharts.Point} link
  320. * Link that connects two nodes
  321. * @param {number} force
  322. * Force calcualated in `repulsiveForceFunction`
  323. * @param {Highcharts.PositionObject} distance
  324. * Distance between two nodes e.g. `{x, y}`
  325. * @return {void}
  326. */
  327. attractive: function (link, force, distanceXY) {
  328. var massFactor = link.getMass(),
  329. translatedX = -distanceXY.x * force * this.diffTemperature,
  330. translatedY = -distanceXY.y * force * this.diffTemperature;
  331. if (!link.fromNode.fixedPosition) {
  332. link.fromNode.plotX -=
  333. translatedX * massFactor.fromNode / link.fromNode.degree;
  334. link.fromNode.plotY -=
  335. translatedY * massFactor.fromNode / link.fromNode.degree;
  336. }
  337. if (!link.toNode.fixedPosition) {
  338. link.toNode.plotX +=
  339. translatedX * massFactor.toNode / link.toNode.degree;
  340. link.toNode.plotY +=
  341. translatedY * massFactor.toNode / link.toNode.degree;
  342. }
  343. },
  344. /**
  345. * Integration method.
  346. *
  347. * In Verlet integration, forces are applied on node immidatelly to it's
  348. * `plotX` and `plotY` position.
  349. *
  350. * Verlet without velocity:
  351. *
  352. * x(n+1) = 2 * x(n) - x(n-1) + A(T) * deltaT ^ 2
  353. *
  354. * where:
  355. * - x(n+1) - new position
  356. * - x(n) - current position
  357. * - x(n-1) - previous position
  358. *
  359. * Assuming A(t) = 0 (no acceleration) and (deltaT = 1) we get:
  360. *
  361. * x(n+1) = x(n) + (x(n) - x(n-1))
  362. *
  363. * where:
  364. * - (x(n) - x(n-1)) - position change
  365. *
  366. * TO DO:
  367. * Consider Verlet with velocity to support additional
  368. * forces. Or even Time-Corrected Verlet by Jonathan
  369. * "lonesock" Dummer
  370. *
  371. * @private
  372. * @param {Highcharts.NetworkgraphLayout} layout layout object
  373. * @param {Highcharts.Point} node node that should be translated
  374. * @return {void}
  375. */
  376. integrate: function (layout, node) {
  377. var friction = -layout.options.friction,
  378. maxSpeed = layout.options.maxSpeed,
  379. prevX = node.prevX,
  380. prevY = node.prevY,
  381. // Apply friciton:
  382. diffX = ((node.plotX + node.dispX -
  383. prevX) * friction),
  384. diffY = ((node.plotY + node.dispY -
  385. prevY) * friction),
  386. abs = Math.abs,
  387. signX = abs(diffX) / (diffX || 1), // need to deal with 0
  388. signY = abs(diffY) / (diffY || 1);
  389. // Apply max speed:
  390. diffX = signX * Math.min(maxSpeed, Math.abs(diffX));
  391. diffY = signY * Math.min(maxSpeed, Math.abs(diffY));
  392. // Store for the next iteration:
  393. node.prevX = node.plotX + node.dispX;
  394. node.prevY = node.plotY + node.dispY;
  395. // Update positions:
  396. node.plotX += diffX;
  397. node.plotY += diffY;
  398. node.temperature = layout.vectorLength({
  399. x: diffX,
  400. y: diffY
  401. });
  402. },
  403. /**
  404. * Estiamte the best possible distance between two nodes, making graph
  405. * readable.
  406. *
  407. * @private
  408. * @param {Highcharts.NetworkgraphLayout} layout layout object
  409. * @return {number}
  410. */
  411. getK: function (layout) {
  412. return Math.pow(layout.box.width * layout.box.height / layout.nodes.length, 0.5);
  413. }
  414. },
  415. euler: {
  416. /**
  417. * Attractive force funtion. Can be replaced by API's
  418. * `layoutAlgorithm.attractiveForce`
  419. *
  420. * Other forces that can be used:
  421. *
  422. * basic, not recommended:
  423. * `function (d, k) { return d / k }`
  424. *
  425. * @private
  426. * @param {number} d current distance between two nodes
  427. * @param {number} k expected distance between two nodes
  428. * @return {number} force
  429. */
  430. attractiveForceFunction: function (d, k) {
  431. return d * d / k;
  432. },
  433. /**
  434. * Repulsive force funtion. Can be replaced by API's
  435. * `layoutAlgorithm.repulsiveForce`.
  436. *
  437. * Other forces that can be used:
  438. *
  439. * basic, not recommended:
  440. * `function (d, k) { return k / d }`
  441. *
  442. * standard:
  443. * `function (d, k) { return k * k / d }`
  444. *
  445. * grid-variant:
  446. * `function (d, k) { return k * k / d * (2 * k - d > 0 ? 1 : 0) }`
  447. *
  448. * @private
  449. * @param {number} d current distance between two nodes
  450. * @param {number} k expected distance between two nodes
  451. * @return {number} force
  452. */
  453. repulsiveForceFunction: function (d, k) {
  454. return k * k / d;
  455. },
  456. /**
  457. * Barycenter force. Calculate and applys barycenter forces on the
  458. * nodes. Making them closer to the center of their barycenter point.
  459. *
  460. * In Euler integration, force is stored in a node, not changing it's
  461. * position. Later, in `integrate()` forces are applied on nodes.
  462. *
  463. * @private
  464. * @return {void}
  465. */
  466. barycenter: function () {
  467. var gravitationalConstant = this.options.gravitationalConstant,
  468. xFactor = this.barycenter.xFactor,
  469. yFactor = this.barycenter.yFactor;
  470. this.nodes.forEach(function (node) {
  471. if (!node.fixedPosition) {
  472. var degree = node.getDegree(),
  473. phi = degree * (1 + degree / 2);
  474. node.dispX += ((xFactor - node.plotX) *
  475. gravitationalConstant *
  476. phi / node.degree);
  477. node.dispY += ((yFactor - node.plotY) *
  478. gravitationalConstant *
  479. phi / node.degree);
  480. }
  481. });
  482. },
  483. /**
  484. * Repulsive force.
  485. *
  486. * @private
  487. * @param {Highcharts.Point} node
  488. * Node that should be translated by force.
  489. * @param {number} force
  490. * Force calcualated in `repulsiveForceFunction`
  491. * @param {Highcharts.PositionObject} distanceXY
  492. * Distance between two nodes e.g. `{x, y}`
  493. * @return {void}
  494. */
  495. repulsive: function (node, force, distanceXY, distanceR) {
  496. node.dispX +=
  497. (distanceXY.x / distanceR) * force / node.degree;
  498. node.dispY +=
  499. (distanceXY.y / distanceR) * force / node.degree;
  500. },
  501. /**
  502. * Attractive force.
  503. *
  504. * In Euler integration, force is stored in a node, not changing it's
  505. * position. Later, in `integrate()` forces are applied on nodes.
  506. *
  507. * @private
  508. * @param {Highcharts.Point} link
  509. * Link that connects two nodes
  510. * @param {number} force
  511. * Force calcualated in `repulsiveForceFunction`
  512. * @param {Highcharts.PositionObject} distanceXY
  513. * Distance between two nodes e.g. `{x, y}`
  514. * @param {number} distanceR
  515. * @return {void}
  516. */
  517. attractive: function (link, force, distanceXY, distanceR) {
  518. var massFactor = link.getMass(),
  519. translatedX = (distanceXY.x / distanceR) * force,
  520. translatedY = (distanceXY.y / distanceR) * force;
  521. if (!link.fromNode.fixedPosition) {
  522. link.fromNode.dispX -=
  523. translatedX * massFactor.fromNode / link.fromNode.degree;
  524. link.fromNode.dispY -=
  525. translatedY * massFactor.fromNode / link.fromNode.degree;
  526. }
  527. if (!link.toNode.fixedPosition) {
  528. link.toNode.dispX +=
  529. translatedX * massFactor.toNode / link.toNode.degree;
  530. link.toNode.dispY +=
  531. translatedY * massFactor.toNode / link.toNode.degree;
  532. }
  533. },
  534. /**
  535. * Integration method.
  536. *
  537. * In Euler integration, force were stored in a node, not changing it's
  538. * position. Now, in the integrator method, we apply changes.
  539. *
  540. * Euler:
  541. *
  542. * Basic form: `x(n+1) = x(n) + v(n)`
  543. *
  544. * With Rengoild-Fruchterman we get:
  545. * `x(n+1) = x(n) + v(n) / length(v(n)) * min(v(n), temperature(n))`
  546. * where:
  547. * - `x(n+1)`: next position
  548. * - `x(n)`: current position
  549. * - `v(n)`: velocity (comes from net force)
  550. * - `temperature(n)`: current temperature
  551. *
  552. * Known issues:
  553. * Oscillations when force vector has the same magnitude but opposite
  554. * direction in the next step. Potentially solved by decreasing force by
  555. * `v * (1 / node.degree)`
  556. *
  557. * Note:
  558. * Actually `min(v(n), temperature(n))` replaces simulated annealing.
  559. *
  560. * @private
  561. * @param {Highcharts.NetworkgraphLayout} layout
  562. * Layout object
  563. * @param {Highcharts.Point} node
  564. * Node that should be translated
  565. * @return {void}
  566. */
  567. integrate: function (layout, node) {
  568. var distanceR;
  569. node.dispX +=
  570. node.dispX * layout.options.friction;
  571. node.dispY +=
  572. node.dispY * layout.options.friction;
  573. distanceR = node.temperature = layout.vectorLength({
  574. x: node.dispX,
  575. y: node.dispY
  576. });
  577. if (distanceR !== 0) {
  578. node.plotX += (node.dispX / distanceR *
  579. Math.min(Math.abs(node.dispX), layout.temperature));
  580. node.plotY += (node.dispY / distanceR *
  581. Math.min(Math.abs(node.dispY), layout.temperature));
  582. }
  583. },
  584. /**
  585. * Estiamte the best possible distance between two nodes, making graph
  586. * readable.
  587. *
  588. * @private
  589. * @param {object} layout layout object
  590. * @return {number}
  591. */
  592. getK: function (layout) {
  593. return Math.pow(layout.box.width * layout.box.height / layout.nodes.length, 0.3);
  594. }
  595. }
  596. };
  597. });
  598. _registerModule(_modules, 'Series/Networkgraph/QuadTree.js', [_modules['Core/Globals.js'], _modules['Core/Utilities.js']], function (H, U) {
  599. /* *
  600. *
  601. * Networkgraph series
  602. *
  603. * (c) 2010-2020 Paweł Fus
  604. *
  605. * License: www.highcharts.com/license
  606. *
  607. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  608. *
  609. * */
  610. var extend = U.extend;
  611. /* eslint-disable no-invalid-this, valid-jsdoc */
  612. /**
  613. * The QuadTree node class. Used in Networkgraph chart as a base for Barnes-Hut
  614. * approximation.
  615. *
  616. * @private
  617. * @class
  618. * @name Highcharts.QuadTreeNode
  619. *
  620. * @param {Highcharts.Dictionary<number>} box Available space for the node
  621. */
  622. var QuadTreeNode = H.QuadTreeNode = function (box) {
  623. /**
  624. * Read only. The available space for node.
  625. *
  626. * @name Highcharts.QuadTreeNode#box
  627. * @type {Highcharts.Dictionary<number>}
  628. */
  629. this.box = box;
  630. /**
  631. * Read only. The minium of width and height values.
  632. *
  633. * @name Highcharts.QuadTreeNode#boxSize
  634. * @type {number}
  635. */
  636. this.boxSize = Math.min(box.width, box.height);
  637. /**
  638. * Read only. Array of subnodes. Empty if QuadTreeNode has just one Point.
  639. * When added another Point to this QuadTreeNode, array is filled with four
  640. * subnodes.
  641. *
  642. * @name Highcharts.QuadTreeNode#nodes
  643. * @type {Array<Highcharts.QuadTreeNode>}
  644. */
  645. this.nodes = [];
  646. /**
  647. * Read only. Flag to determine if QuadTreeNode is internal (and has
  648. * subnodes with mass and central position) or external (bound to Point).
  649. *
  650. * @name Highcharts.QuadTreeNode#isInternal
  651. * @type {boolean}
  652. */
  653. this.isInternal = false;
  654. /**
  655. * Read only. If QuadTreeNode is an external node, Point is stored in
  656. * `this.body`.
  657. *
  658. * @name Highcharts.QuadTreeNode#body
  659. * @type {boolean|Highcharts.Point}
  660. */
  661. this.body = false;
  662. /**
  663. * Read only. Internal nodes when created are empty to reserve the space. If
  664. * Point is added to this QuadTreeNode, QuadTreeNode is no longer empty.
  665. *
  666. * @name Highcharts.QuadTreeNode#isEmpty
  667. * @type {boolean}
  668. */
  669. this.isEmpty = true;
  670. };
  671. extend(QuadTreeNode.prototype,
  672. /** @lends Highcharts.QuadTreeNode.prototype */
  673. {
  674. /**
  675. * Insert recursively point(node) into the QuadTree. If the given
  676. * quadrant is already occupied, divide it into smaller quadrants.
  677. *
  678. * @param {Highcharts.Point} point
  679. * Point/node to be inserted
  680. * @param {number} depth
  681. * Max depth of the QuadTree
  682. */
  683. insert: function (point, depth) {
  684. var newQuadTreeNode;
  685. if (this.isInternal) {
  686. // Internal node:
  687. this.nodes[this.getBoxPosition(point)].insert(point, depth - 1);
  688. }
  689. else {
  690. this.isEmpty = false;
  691. if (!this.body) {
  692. // First body in a quadrant:
  693. this.isInternal = false;
  694. this.body = point;
  695. }
  696. else {
  697. if (depth) {
  698. // Every other body in a quadrant:
  699. this.isInternal = true;
  700. this.divideBox();
  701. // Reinsert main body only once:
  702. if (this.body !== true) {
  703. this.nodes[this.getBoxPosition(this.body)]
  704. .insert(this.body, depth - 1);
  705. this.body = true;
  706. }
  707. // Add second body:
  708. this.nodes[this.getBoxPosition(point)]
  709. .insert(point, depth - 1);
  710. }
  711. else {
  712. // We are below max allowed depth. That means either:
  713. // - really huge number of points
  714. // - falling two points into exactly the same position
  715. // In this case, create another node in the QuadTree.
  716. //
  717. // Alternatively we could add some noise to the
  718. // position, but that could result in different
  719. // rendered chart in exporting.
  720. newQuadTreeNode = new QuadTreeNode({
  721. top: point.plotX,
  722. left: point.plotY,
  723. // Width/height below 1px
  724. width: 0.1,
  725. height: 0.1
  726. });
  727. newQuadTreeNode.body = point;
  728. newQuadTreeNode.isInternal = false;
  729. this.nodes.push(newQuadTreeNode);
  730. }
  731. }
  732. }
  733. },
  734. /**
  735. * Each quad node requires it's mass and center position. That mass and
  736. * position is used to imitate real node in the layout by approximation.
  737. */
  738. updateMassAndCenter: function () {
  739. var mass = 0,
  740. plotX = 0,
  741. plotY = 0;
  742. if (this.isInternal) {
  743. // Calcualte weightened mass of the quad node:
  744. this.nodes.forEach(function (pointMass) {
  745. if (!pointMass.isEmpty) {
  746. mass += pointMass.mass;
  747. plotX +=
  748. pointMass.plotX * pointMass.mass;
  749. plotY +=
  750. pointMass.plotY * pointMass.mass;
  751. }
  752. });
  753. plotX /= mass;
  754. plotY /= mass;
  755. }
  756. else if (this.body) {
  757. // Just one node, use coordinates directly:
  758. mass = this.body.mass;
  759. plotX = this.body.plotX;
  760. plotY = this.body.plotY;
  761. }
  762. // Store details:
  763. this.mass = mass;
  764. this.plotX = plotX;
  765. this.plotY = plotY;
  766. },
  767. /**
  768. * When inserting another node into the box, that already hove one node,
  769. * divide the available space into another four quadrants.
  770. *
  771. * Indexes of quadrants are:
  772. * ```
  773. * ------------- -------------
  774. * | | | | |
  775. * | | | 0 | 1 |
  776. * | | divide() | | |
  777. * | 1 | -----------> -------------
  778. * | | | | |
  779. * | | | 3 | 2 |
  780. * | | | | |
  781. * ------------- -------------
  782. * ```
  783. */
  784. divideBox: function () {
  785. var halfWidth = this.box.width / 2,
  786. halfHeight = this.box.height / 2;
  787. // Top left
  788. this.nodes[0] = new QuadTreeNode({
  789. left: this.box.left,
  790. top: this.box.top,
  791. width: halfWidth,
  792. height: halfHeight
  793. });
  794. // Top right
  795. this.nodes[1] = new QuadTreeNode({
  796. left: this.box.left + halfWidth,
  797. top: this.box.top,
  798. width: halfWidth,
  799. height: halfHeight
  800. });
  801. // Bottom right
  802. this.nodes[2] = new QuadTreeNode({
  803. left: this.box.left + halfWidth,
  804. top: this.box.top + halfHeight,
  805. width: halfWidth,
  806. height: halfHeight
  807. });
  808. // Bottom left
  809. this.nodes[3] = new QuadTreeNode({
  810. left: this.box.left,
  811. top: this.box.top + halfHeight,
  812. width: halfWidth,
  813. height: halfHeight
  814. });
  815. },
  816. /**
  817. * Determine which of the quadrants should be used when placing node in
  818. * the QuadTree. Returned index is always in range `< 0 , 3 >`.
  819. *
  820. * @param {Highcharts.Point} point
  821. * @return {number}
  822. */
  823. getBoxPosition: function (point) {
  824. var left = point.plotX < this.box.left + this.box.width / 2,
  825. top = point.plotY < this.box.top + this.box.height / 2,
  826. index;
  827. if (left) {
  828. if (top) {
  829. // Top left
  830. index = 0;
  831. }
  832. else {
  833. // Bottom left
  834. index = 3;
  835. }
  836. }
  837. else {
  838. if (top) {
  839. // Top right
  840. index = 1;
  841. }
  842. else {
  843. // Bottom right
  844. index = 2;
  845. }
  846. }
  847. return index;
  848. }
  849. });
  850. /**
  851. * The QuadTree class. Used in Networkgraph chart as a base for Barnes-Hut
  852. * approximation.
  853. *
  854. * @private
  855. * @class
  856. * @name Highcharts.QuadTree
  857. *
  858. * @param {number} x left position of the plotting area
  859. * @param {number} y top position of the plotting area
  860. * @param {number} width width of the plotting area
  861. * @param {number} height height of the plotting area
  862. */
  863. var QuadTree = H.QuadTree = function (x,
  864. y,
  865. width,
  866. height) {
  867. // Boundary rectangle:
  868. this.box = {
  869. left: x,
  870. top: y,
  871. width: width,
  872. height: height
  873. };
  874. this.maxDepth = 25;
  875. this.root = new QuadTreeNode(this.box, '0');
  876. this.root.isInternal = true;
  877. this.root.isRoot = true;
  878. this.root.divideBox();
  879. };
  880. extend(QuadTree.prototype,
  881. /** @lends Highcharts.QuadTree.prototype */
  882. {
  883. /**
  884. * Insert nodes into the QuadTree
  885. *
  886. * @param {Array<Highcharts.Point>} points
  887. */
  888. insertNodes: function (points) {
  889. points.forEach(function (point) {
  890. this.root.insert(point, this.maxDepth);
  891. }, this);
  892. },
  893. /**
  894. * Depfth first treversal (DFS). Using `before` and `after` callbacks,
  895. * we can get two results: preorder and postorder traversals, reminder:
  896. *
  897. * ```
  898. * (a)
  899. * / \
  900. * (b) (c)
  901. * / \
  902. * (d) (e)
  903. * ```
  904. *
  905. * DFS (preorder): `a -> b -> d -> e -> c`
  906. *
  907. * DFS (postorder): `d -> e -> b -> c -> a`
  908. *
  909. * @param {Highcharts.QuadTreeNode|null} node
  910. * @param {Function} [beforeCallback] function to be called before
  911. * visiting children nodes
  912. * @param {Function} [afterCallback] function to be called after
  913. * visiting children nodes
  914. */
  915. visitNodeRecursive: function (node, beforeCallback, afterCallback) {
  916. var goFurther;
  917. if (!node) {
  918. node = this.root;
  919. }
  920. if (node === this.root && beforeCallback) {
  921. goFurther = beforeCallback(node);
  922. }
  923. if (goFurther === false) {
  924. return;
  925. }
  926. node.nodes.forEach(function (qtNode) {
  927. if (qtNode.isInternal) {
  928. if (beforeCallback) {
  929. goFurther = beforeCallback(qtNode);
  930. }
  931. if (goFurther === false) {
  932. return;
  933. }
  934. this.visitNodeRecursive(qtNode, beforeCallback, afterCallback);
  935. }
  936. else if (qtNode.body) {
  937. if (beforeCallback) {
  938. beforeCallback(qtNode.body);
  939. }
  940. }
  941. if (afterCallback) {
  942. afterCallback(qtNode);
  943. }
  944. }, this);
  945. if (node === this.root && afterCallback) {
  946. afterCallback(node);
  947. }
  948. },
  949. /**
  950. * Calculate mass of the each QuadNode in the tree.
  951. */
  952. calculateMassAndCenter: function () {
  953. this.visitNodeRecursive(null, null, function (node) {
  954. node.updateMassAndCenter();
  955. });
  956. }
  957. });
  958. });
  959. _registerModule(_modules, 'Series/Networkgraph/Layouts.js', [_modules['Core/Chart/Chart.js'], _modules['Core/Animation/AnimationUtilities.js'], _modules['Core/Globals.js'], _modules['Core/Utilities.js']], function (Chart, A, H, U) {
  960. /* *
  961. *
  962. * Networkgraph series
  963. *
  964. * (c) 2010-2020 Paweł Fus
  965. *
  966. * License: www.highcharts.com/license
  967. *
  968. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  969. *
  970. * */
  971. var setAnimation = A.setAnimation;
  972. var addEvent = U.addEvent,
  973. clamp = U.clamp,
  974. defined = U.defined,
  975. extend = U.extend,
  976. isFunction = U.isFunction,
  977. pick = U.pick;
  978. /* eslint-disable no-invalid-this, valid-jsdoc */
  979. H.layouts = {
  980. 'reingold-fruchterman': function () {
  981. }
  982. };
  983. extend(
  984. /**
  985. * Reingold-Fruchterman algorithm from
  986. * "Graph Drawing by Force-directed Placement" paper.
  987. * @private
  988. */
  989. H.layouts['reingold-fruchterman'].prototype, {
  990. init: function (options) {
  991. this.options = options;
  992. this.nodes = [];
  993. this.links = [];
  994. this.series = [];
  995. this.box = {
  996. x: 0,
  997. y: 0,
  998. width: 0,
  999. height: 0
  1000. };
  1001. this.setInitialRendering(true);
  1002. this.integration =
  1003. H.networkgraphIntegrations[options.integration];
  1004. this.enableSimulation = options.enableSimulation;
  1005. this.attractiveForce = pick(options.attractiveForce, this.integration.attractiveForceFunction);
  1006. this.repulsiveForce = pick(options.repulsiveForce, this.integration.repulsiveForceFunction);
  1007. this.approximation = options.approximation;
  1008. },
  1009. updateSimulation: function (enable) {
  1010. this.enableSimulation = pick(enable, this.options.enableSimulation);
  1011. },
  1012. start: function () {
  1013. var layout = this,
  1014. series = this.series,
  1015. options = this.options;
  1016. layout.currentStep = 0;
  1017. layout.forces = series[0] && series[0].forces || [];
  1018. layout.chart = series[0] && series[0].chart;
  1019. if (layout.initialRendering) {
  1020. layout.initPositions();
  1021. // Render elements in initial positions:
  1022. series.forEach(function (s) {
  1023. s.finishedAnimating = true; // #13169
  1024. s.render();
  1025. });
  1026. }
  1027. layout.setK();
  1028. layout.resetSimulation(options);
  1029. if (layout.enableSimulation) {
  1030. layout.step();
  1031. }
  1032. },
  1033. step: function () {
  1034. var layout = this,
  1035. series = this.series,
  1036. options = this.options;
  1037. // Algorithm:
  1038. layout.currentStep++;
  1039. if (layout.approximation === 'barnes-hut') {
  1040. layout.createQuadTree();
  1041. layout.quadTree.calculateMassAndCenter();
  1042. }
  1043. layout.forces.forEach(function (forceName) {
  1044. layout[forceName + 'Forces'](layout.temperature);
  1045. });
  1046. // Limit to the plotting area and cool down:
  1047. layout.applyLimits(layout.temperature);
  1048. // Cool down the system:
  1049. layout.temperature = layout.coolDown(layout.startTemperature, layout.diffTemperature, layout.currentStep);
  1050. layout.prevSystemTemperature = layout.systemTemperature;
  1051. layout.systemTemperature = layout.getSystemTemperature();
  1052. if (layout.enableSimulation) {
  1053. series.forEach(function (s) {
  1054. // Chart could be destroyed during the simulation
  1055. if (s.chart) {
  1056. s.render();
  1057. }
  1058. });
  1059. if (layout.maxIterations-- &&
  1060. isFinite(layout.temperature) &&
  1061. !layout.isStable()) {
  1062. if (layout.simulation) {
  1063. H.win.cancelAnimationFrame(layout.simulation);
  1064. }
  1065. layout.simulation = H.win.requestAnimationFrame(function () {
  1066. layout.step();
  1067. });
  1068. }
  1069. else {
  1070. layout.simulation = false;
  1071. }
  1072. }
  1073. },
  1074. stop: function () {
  1075. if (this.simulation) {
  1076. H.win.cancelAnimationFrame(this.simulation);
  1077. }
  1078. },
  1079. setArea: function (x, y, w, h) {
  1080. this.box = {
  1081. left: x,
  1082. top: y,
  1083. width: w,
  1084. height: h
  1085. };
  1086. },
  1087. setK: function () {
  1088. // Optimal distance between nodes,
  1089. // available space around the node:
  1090. this.k = this.options.linkLength || this.integration.getK(this);
  1091. },
  1092. addElementsToCollection: function (elements, collection) {
  1093. elements.forEach(function (elem) {
  1094. if (collection.indexOf(elem) === -1) {
  1095. collection.push(elem);
  1096. }
  1097. });
  1098. },
  1099. removeElementFromCollection: function (element, collection) {
  1100. var index = collection.indexOf(element);
  1101. if (index !== -1) {
  1102. collection.splice(index, 1);
  1103. }
  1104. },
  1105. clear: function () {
  1106. this.nodes.length = 0;
  1107. this.links.length = 0;
  1108. this.series.length = 0;
  1109. this.resetSimulation();
  1110. },
  1111. resetSimulation: function () {
  1112. this.forcedStop = false;
  1113. this.systemTemperature = 0;
  1114. this.setMaxIterations();
  1115. this.setTemperature();
  1116. this.setDiffTemperature();
  1117. },
  1118. restartSimulation: function () {
  1119. if (!this.simulation) {
  1120. // When dragging nodes, we don't need to calculate
  1121. // initial positions and rendering nodes:
  1122. this.setInitialRendering(false);
  1123. // Start new simulation:
  1124. if (!this.enableSimulation) {
  1125. // Run only one iteration to speed things up:
  1126. this.setMaxIterations(1);
  1127. }
  1128. else {
  1129. this.start();
  1130. }
  1131. if (this.chart) {
  1132. this.chart.redraw();
  1133. }
  1134. // Restore defaults:
  1135. this.setInitialRendering(true);
  1136. }
  1137. else {
  1138. // Extend current simulation:
  1139. this.resetSimulation();
  1140. }
  1141. },
  1142. setMaxIterations: function (maxIterations) {
  1143. this.maxIterations = pick(maxIterations, this.options.maxIterations);
  1144. },
  1145. setTemperature: function () {
  1146. this.temperature = this.startTemperature =
  1147. Math.sqrt(this.nodes.length);
  1148. },
  1149. setDiffTemperature: function () {
  1150. this.diffTemperature = this.startTemperature /
  1151. (this.options.maxIterations + 1);
  1152. },
  1153. setInitialRendering: function (enable) {
  1154. this.initialRendering = enable;
  1155. },
  1156. createQuadTree: function () {
  1157. this.quadTree = new H.QuadTree(this.box.left, this.box.top, this.box.width, this.box.height);
  1158. this.quadTree.insertNodes(this.nodes);
  1159. },
  1160. initPositions: function () {
  1161. var initialPositions = this.options.initialPositions;
  1162. if (isFunction(initialPositions)) {
  1163. initialPositions.call(this);
  1164. this.nodes.forEach(function (node) {
  1165. if (!defined(node.prevX)) {
  1166. node.prevX = node.plotX;
  1167. }
  1168. if (!defined(node.prevY)) {
  1169. node.prevY = node.plotY;
  1170. }
  1171. node.dispX = 0;
  1172. node.dispY = 0;
  1173. });
  1174. }
  1175. else if (initialPositions === 'circle') {
  1176. this.setCircularPositions();
  1177. }
  1178. else {
  1179. this.setRandomPositions();
  1180. }
  1181. },
  1182. setCircularPositions: function () {
  1183. var box = this.box,
  1184. nodes = this.nodes,
  1185. nodesLength = nodes.length + 1,
  1186. angle = 2 * Math.PI / nodesLength,
  1187. rootNodes = nodes.filter(function (node) {
  1188. return node.linksTo.length === 0;
  1189. }), sortedNodes = [], visitedNodes = {}, radius = this.options.initialPositionRadius;
  1190. /**
  1191. * @private
  1192. */
  1193. function addToNodes(node) {
  1194. node.linksFrom.forEach(function (link) {
  1195. if (!visitedNodes[link.toNode.id]) {
  1196. visitedNodes[link.toNode.id] = true;
  1197. sortedNodes.push(link.toNode);
  1198. addToNodes(link.toNode);
  1199. }
  1200. });
  1201. }
  1202. // Start with identified root nodes an sort the nodes by their
  1203. // hierarchy. In trees, this ensures that branches don't cross
  1204. // eachother.
  1205. rootNodes.forEach(function (rootNode) {
  1206. sortedNodes.push(rootNode);
  1207. addToNodes(rootNode);
  1208. });
  1209. // Cyclic tree, no root node found
  1210. if (!sortedNodes.length) {
  1211. sortedNodes = nodes;
  1212. // Dangling, cyclic trees
  1213. }
  1214. else {
  1215. nodes.forEach(function (node) {
  1216. if (sortedNodes.indexOf(node) === -1) {
  1217. sortedNodes.push(node);
  1218. }
  1219. });
  1220. }
  1221. // Initial positions are laid out along a small circle, appearing
  1222. // as a cluster in the middle
  1223. sortedNodes.forEach(function (node, index) {
  1224. node.plotX = node.prevX = pick(node.plotX, box.width / 2 + radius * Math.cos(index * angle));
  1225. node.plotY = node.prevY = pick(node.plotY, box.height / 2 + radius * Math.sin(index * angle));
  1226. node.dispX = 0;
  1227. node.dispY = 0;
  1228. });
  1229. },
  1230. setRandomPositions: function () {
  1231. var box = this.box,
  1232. nodes = this.nodes,
  1233. nodesLength = nodes.length + 1;
  1234. /**
  1235. * Return a repeatable, quasi-random number based on an integer
  1236. * input. For the initial positions
  1237. * @private
  1238. */
  1239. function unrandom(n) {
  1240. var rand = n * n / Math.PI;
  1241. rand = rand - Math.floor(rand);
  1242. return rand;
  1243. }
  1244. // Initial positions:
  1245. nodes.forEach(function (node, index) {
  1246. node.plotX = node.prevX = pick(node.plotX, box.width * unrandom(index));
  1247. node.plotY = node.prevY = pick(node.plotY, box.height * unrandom(nodesLength + index));
  1248. node.dispX = 0;
  1249. node.dispY = 0;
  1250. });
  1251. },
  1252. force: function (name) {
  1253. this.integration[name].apply(this, Array.prototype.slice.call(arguments, 1));
  1254. },
  1255. barycenterForces: function () {
  1256. this.getBarycenter();
  1257. this.force('barycenter');
  1258. },
  1259. getBarycenter: function () {
  1260. var systemMass = 0,
  1261. cx = 0,
  1262. cy = 0;
  1263. this.nodes.forEach(function (node) {
  1264. cx += node.plotX * node.mass;
  1265. cy += node.plotY * node.mass;
  1266. systemMass += node.mass;
  1267. });
  1268. this.barycenter = {
  1269. x: cx,
  1270. y: cy,
  1271. xFactor: cx / systemMass,
  1272. yFactor: cy / systemMass
  1273. };
  1274. return this.barycenter;
  1275. },
  1276. barnesHutApproximation: function (node, quadNode) {
  1277. var layout = this,
  1278. distanceXY = layout.getDistXY(node,
  1279. quadNode),
  1280. distanceR = layout.vectorLength(distanceXY),
  1281. goDeeper,
  1282. force;
  1283. if (node !== quadNode && distanceR !== 0) {
  1284. if (quadNode.isInternal) {
  1285. // Internal node:
  1286. if (quadNode.boxSize / distanceR <
  1287. layout.options.theta &&
  1288. distanceR !== 0) {
  1289. // Treat as an external node:
  1290. force = layout.repulsiveForce(distanceR, layout.k);
  1291. layout.force('repulsive', node, force * quadNode.mass, distanceXY, distanceR);
  1292. goDeeper = false;
  1293. }
  1294. else {
  1295. // Go deeper:
  1296. goDeeper = true;
  1297. }
  1298. }
  1299. else {
  1300. // External node, direct force:
  1301. force = layout.repulsiveForce(distanceR, layout.k);
  1302. layout.force('repulsive', node, force * quadNode.mass, distanceXY, distanceR);
  1303. }
  1304. }
  1305. return goDeeper;
  1306. },
  1307. repulsiveForces: function () {
  1308. var layout = this;
  1309. if (layout.approximation === 'barnes-hut') {
  1310. layout.nodes.forEach(function (node) {
  1311. layout.quadTree.visitNodeRecursive(null, function (quadNode) {
  1312. return layout.barnesHutApproximation(node, quadNode);
  1313. });
  1314. });
  1315. }
  1316. else {
  1317. layout.nodes.forEach(function (node) {
  1318. layout.nodes.forEach(function (repNode) {
  1319. var force,
  1320. distanceR,
  1321. distanceXY;
  1322. if (
  1323. // Node can not repulse itself:
  1324. node !== repNode &&
  1325. // Only close nodes affect each other:
  1326. // layout.getDistR(node, repNode) < 2 * k &&
  1327. // Not dragged:
  1328. !node.fixedPosition) {
  1329. distanceXY = layout.getDistXY(node, repNode);
  1330. distanceR = layout.vectorLength(distanceXY);
  1331. if (distanceR !== 0) {
  1332. force = layout.repulsiveForce(distanceR, layout.k);
  1333. layout.force('repulsive', node, force * repNode.mass, distanceXY, distanceR);
  1334. }
  1335. }
  1336. });
  1337. });
  1338. }
  1339. },
  1340. attractiveForces: function () {
  1341. var layout = this,
  1342. distanceXY,
  1343. distanceR,
  1344. force;
  1345. layout.links.forEach(function (link) {
  1346. if (link.fromNode && link.toNode) {
  1347. distanceXY = layout.getDistXY(link.fromNode, link.toNode);
  1348. distanceR = layout.vectorLength(distanceXY);
  1349. if (distanceR !== 0) {
  1350. force = layout.attractiveForce(distanceR, layout.k);
  1351. layout.force('attractive', link, force, distanceXY, distanceR);
  1352. }
  1353. }
  1354. });
  1355. },
  1356. applyLimits: function () {
  1357. var layout = this,
  1358. nodes = layout.nodes;
  1359. nodes.forEach(function (node) {
  1360. if (node.fixedPosition) {
  1361. return;
  1362. }
  1363. layout.integration.integrate(layout, node);
  1364. layout.applyLimitBox(node, layout.box);
  1365. // Reset displacement:
  1366. node.dispX = 0;
  1367. node.dispY = 0;
  1368. });
  1369. },
  1370. /**
  1371. * External box that nodes should fall. When hitting an edge, node
  1372. * should stop or bounce.
  1373. * @private
  1374. */
  1375. applyLimitBox: function (node, box) {
  1376. var radius = node.radius;
  1377. /*
  1378. TO DO: Consider elastic collision instead of stopping.
  1379. o' means end position when hitting plotting area edge:
  1380. - "inelastic":
  1381. o
  1382. \
  1383. ______
  1384. | o'
  1385. | \
  1386. | \
  1387. - "elastic"/"bounced":
  1388. o
  1389. \
  1390. ______
  1391. | ^
  1392. | / \
  1393. |o' \
  1394. Euler sample:
  1395. if (plotX < 0) {
  1396. plotX = 0;
  1397. dispX *= -1;
  1398. }
  1399. if (plotX > box.width) {
  1400. plotX = box.width;
  1401. dispX *= -1;
  1402. }
  1403. */
  1404. // Limit X-coordinates:
  1405. node.plotX = clamp(node.plotX, box.left + radius, box.width - radius);
  1406. // Limit Y-coordinates:
  1407. node.plotY = clamp(node.plotY, box.top + radius, box.height - radius);
  1408. },
  1409. /**
  1410. * From "A comparison of simulated annealing cooling strategies" by
  1411. * Nourani and Andresen work.
  1412. * @private
  1413. */
  1414. coolDown: function (temperature, temperatureStep, currentStep) {
  1415. // Logarithmic:
  1416. /*
  1417. return Math.sqrt(this.nodes.length) -
  1418. Math.log(
  1419. currentStep * layout.diffTemperature
  1420. );
  1421. */
  1422. // Exponential:
  1423. /*
  1424. var alpha = 0.1;
  1425. layout.temperature = Math.sqrt(layout.nodes.length) *
  1426. Math.pow(alpha, layout.diffTemperature);
  1427. */
  1428. // Linear:
  1429. return temperature - temperatureStep * currentStep;
  1430. },
  1431. isStable: function () {
  1432. return Math.abs(this.systemTemperature -
  1433. this.prevSystemTemperature) < 0.00001 || this.temperature <= 0;
  1434. },
  1435. getSystemTemperature: function () {
  1436. return this.nodes.reduce(function (value, node) {
  1437. return value + node.temperature;
  1438. }, 0);
  1439. },
  1440. vectorLength: function (vector) {
  1441. return Math.sqrt(vector.x * vector.x + vector.y * vector.y);
  1442. },
  1443. getDistR: function (nodeA, nodeB) {
  1444. var distance = this.getDistXY(nodeA,
  1445. nodeB);
  1446. return this.vectorLength(distance);
  1447. },
  1448. getDistXY: function (nodeA, nodeB) {
  1449. var xDist = nodeA.plotX - nodeB.plotX,
  1450. yDist = nodeA.plotY - nodeB.plotY;
  1451. return {
  1452. x: xDist,
  1453. y: yDist,
  1454. absX: Math.abs(xDist),
  1455. absY: Math.abs(yDist)
  1456. };
  1457. }
  1458. });
  1459. /* ************************************************************************** *
  1460. * Multiple series support:
  1461. * ************************************************************************** */
  1462. // Clear previous layouts
  1463. addEvent(Chart, 'predraw', function () {
  1464. if (this.graphLayoutsLookup) {
  1465. this.graphLayoutsLookup.forEach(function (layout) {
  1466. layout.stop();
  1467. });
  1468. }
  1469. });
  1470. addEvent(Chart, 'render', function () {
  1471. var systemsStable,
  1472. afterRender = false;
  1473. /**
  1474. * @private
  1475. */
  1476. function layoutStep(layout) {
  1477. if (layout.maxIterations-- &&
  1478. isFinite(layout.temperature) &&
  1479. !layout.isStable() &&
  1480. !layout.enableSimulation) {
  1481. // Hook similar to build-in addEvent, but instead of
  1482. // creating whole events logic, use just a function.
  1483. // It's faster which is important for rAF code.
  1484. // Used e.g. in packed-bubble series for bubble radius
  1485. // calculations
  1486. if (layout.beforeStep) {
  1487. layout.beforeStep();
  1488. }
  1489. layout.step();
  1490. systemsStable = false;
  1491. afterRender = true;
  1492. }
  1493. }
  1494. if (this.graphLayoutsLookup) {
  1495. setAnimation(false, this);
  1496. // Start simulation
  1497. this.graphLayoutsLookup.forEach(function (layout) {
  1498. layout.start();
  1499. });
  1500. // Just one sync step, to run different layouts similar to
  1501. // async mode.
  1502. while (!systemsStable) {
  1503. systemsStable = true;
  1504. this.graphLayoutsLookup.forEach(layoutStep);
  1505. }
  1506. if (afterRender) {
  1507. this.series.forEach(function (s) {
  1508. if (s && s.layout) {
  1509. s.render();
  1510. }
  1511. });
  1512. }
  1513. }
  1514. });
  1515. // disable simulation before print if enabled
  1516. addEvent(Chart, 'beforePrint', function () {
  1517. if (this.graphLayoutsLookup) {
  1518. this.graphLayoutsLookup.forEach(function (layout) {
  1519. layout.updateSimulation(false);
  1520. });
  1521. this.redraw();
  1522. }
  1523. });
  1524. // re-enable simulation after print
  1525. addEvent(Chart, 'afterPrint', function () {
  1526. if (this.graphLayoutsLookup) {
  1527. this.graphLayoutsLookup.forEach(function (layout) {
  1528. // return to default simulation
  1529. layout.updateSimulation();
  1530. });
  1531. }
  1532. this.redraw();
  1533. });
  1534. });
  1535. _registerModule(_modules, 'Series/Networkgraph/DraggableNodes.js', [_modules['Core/Chart/Chart.js'], _modules['Core/Globals.js'], _modules['Core/Utilities.js']], function (Chart, H, U) {
  1536. /* *
  1537. *
  1538. * Networkgraph series
  1539. *
  1540. * (c) 2010-2020 Paweł Fus
  1541. *
  1542. * License: www.highcharts.com/license
  1543. *
  1544. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  1545. *
  1546. * */
  1547. var addEvent = U.addEvent;
  1548. /* eslint-disable no-invalid-this, valid-jsdoc */
  1549. H.dragNodesMixin = {
  1550. /**
  1551. * Mouse down action, initializing drag&drop mode.
  1552. *
  1553. * @private
  1554. * @param {Highcharts.Point} point The point that event occured.
  1555. * @param {Highcharts.PointerEventObject} event Browser event, before normalization.
  1556. * @return {void}
  1557. */
  1558. onMouseDown: function (point, event) {
  1559. var normalizedEvent = this.chart.pointer.normalize(event);
  1560. point.fixedPosition = {
  1561. chartX: normalizedEvent.chartX,
  1562. chartY: normalizedEvent.chartY,
  1563. plotX: point.plotX,
  1564. plotY: point.plotY
  1565. };
  1566. point.inDragMode = true;
  1567. },
  1568. /**
  1569. * Mouse move action during drag&drop.
  1570. *
  1571. * @private
  1572. *
  1573. * @param {global.Event} event Browser event, before normalization.
  1574. * @param {Highcharts.Point} point The point that event occured.
  1575. *
  1576. * @return {void}
  1577. */
  1578. onMouseMove: function (point, event) {
  1579. if (point.fixedPosition && point.inDragMode) {
  1580. var series = this,
  1581. chart = series.chart,
  1582. normalizedEvent = chart.pointer.normalize(event),
  1583. diffX = point.fixedPosition.chartX - normalizedEvent.chartX,
  1584. diffY = point.fixedPosition.chartY - normalizedEvent.chartY,
  1585. newPlotX,
  1586. newPlotY,
  1587. graphLayoutsLookup = chart.graphLayoutsLookup;
  1588. // At least 5px to apply change (avoids simple click):
  1589. if (Math.abs(diffX) > 5 || Math.abs(diffY) > 5) {
  1590. newPlotX = point.fixedPosition.plotX - diffX;
  1591. newPlotY = point.fixedPosition.plotY - diffY;
  1592. if (chart.isInsidePlot(newPlotX, newPlotY)) {
  1593. point.plotX = newPlotX;
  1594. point.plotY = newPlotY;
  1595. point.hasDragged = true;
  1596. this.redrawHalo(point);
  1597. graphLayoutsLookup.forEach(function (layout) {
  1598. layout.restartSimulation();
  1599. });
  1600. }
  1601. }
  1602. }
  1603. },
  1604. /**
  1605. * Mouse up action, finalizing drag&drop.
  1606. *
  1607. * @private
  1608. * @param {Highcharts.Point} point The point that event occured.
  1609. * @return {void}
  1610. */
  1611. onMouseUp: function (point, event) {
  1612. if (point.fixedPosition) {
  1613. if (point.hasDragged) {
  1614. if (this.layout.enableSimulation) {
  1615. this.layout.start();
  1616. }
  1617. else {
  1618. this.chart.redraw();
  1619. }
  1620. }
  1621. point.inDragMode = point.hasDragged = false;
  1622. if (!this.options.fixedDraggable) {
  1623. delete point.fixedPosition;
  1624. }
  1625. }
  1626. },
  1627. // Draggable mode:
  1628. /**
  1629. * Redraw halo on mousemove during the drag&drop action.
  1630. *
  1631. * @private
  1632. * @param {Highcharts.Point} point The point that should show halo.
  1633. * @return {void}
  1634. */
  1635. redrawHalo: function (point) {
  1636. if (point && this.halo) {
  1637. this.halo.attr({
  1638. d: point.haloPath(this.options.states.hover.halo.size)
  1639. });
  1640. }
  1641. }
  1642. };
  1643. /*
  1644. * Draggable mode:
  1645. */
  1646. addEvent(Chart, 'load', function () {
  1647. var chart = this,
  1648. mousedownUnbinder,
  1649. mousemoveUnbinder,
  1650. mouseupUnbinder;
  1651. if (chart.container) {
  1652. mousedownUnbinder = addEvent(chart.container, 'mousedown', function (event) {
  1653. var point = chart.hoverPoint;
  1654. if (point &&
  1655. point.series &&
  1656. point.series.hasDraggableNodes &&
  1657. point.series.options.draggable) {
  1658. point.series.onMouseDown(point, event);
  1659. mousemoveUnbinder = addEvent(chart.container, 'mousemove', function (e) {
  1660. return point &&
  1661. point.series &&
  1662. point.series.onMouseMove(point, e);
  1663. });
  1664. mouseupUnbinder = addEvent(chart.container.ownerDocument, 'mouseup', function (e) {
  1665. mousemoveUnbinder();
  1666. mouseupUnbinder();
  1667. return point &&
  1668. point.series &&
  1669. point.series.onMouseUp(point, e);
  1670. });
  1671. }
  1672. });
  1673. }
  1674. addEvent(chart, 'destroy', function () {
  1675. mousedownUnbinder();
  1676. });
  1677. });
  1678. });
  1679. _registerModule(_modules, 'Series/Networkgraph/Networkgraph.js', [_modules['Core/Series/Series.js'], _modules['Core/Globals.js'], _modules['Mixins/Nodes.js'], _modules['Core/Series/Point.js'], _modules['Core/Utilities.js']], function (BaseSeries, H, NodesMixin, Point, U) {
  1680. /* *
  1681. *
  1682. * Networkgraph series
  1683. *
  1684. * (c) 2010-2020 Paweł Fus
  1685. *
  1686. * License: www.highcharts.com/license
  1687. *
  1688. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  1689. *
  1690. * */
  1691. var addEvent = U.addEvent,
  1692. css = U.css,
  1693. defined = U.defined,
  1694. pick = U.pick;
  1695. var Series = H.Series,
  1696. seriesTypes = BaseSeries.seriesTypes,
  1697. dragNodesMixin = H.dragNodesMixin;
  1698. /**
  1699. * Formatter callback function.
  1700. *
  1701. * @callback Highcharts.SeriesNetworkgraphDataLabelsFormatterCallbackFunction
  1702. *
  1703. * @param {Highcharts.SeriesNetworkgraphDataLabelsFormatterContextObject|Highcharts.PointLabelObject} this
  1704. * Data label context to format
  1705. *
  1706. * @return {string}
  1707. * Formatted data label text
  1708. */
  1709. /**
  1710. * Context for the formatter function.
  1711. *
  1712. * @interface Highcharts.SeriesNetworkgraphDataLabelsFormatterContextObject
  1713. * @extends Highcharts.PointLabelObject
  1714. * @since 7.0.0
  1715. */ /**
  1716. * The color of the node.
  1717. * @name Highcharts.SeriesNetworkgraphDataLabelsFormatterContextObject#color
  1718. * @type {Highcharts.ColorString}
  1719. * @since 7.0.0
  1720. */ /**
  1721. * The point (node) object. The node name, if defined, is available through
  1722. * `this.point.name`. Arrays: `this.point.linksFrom` and `this.point.linksTo`
  1723. * contains all nodes connected to this point.
  1724. * @name Highcharts.SeriesNetworkgraphDataLabelsFormatterContextObject#point
  1725. * @type {Highcharts.Point}
  1726. * @since 7.0.0
  1727. */ /**
  1728. * The ID of the node.
  1729. * @name Highcharts.SeriesNetworkgraphDataLabelsFormatterContextObject#key
  1730. * @type {string}
  1731. * @since 7.0.0
  1732. */
  1733. ''; // detach doclets above
  1734. /**
  1735. * @private
  1736. * @class
  1737. * @name Highcharts.seriesTypes.networkgraph
  1738. *
  1739. * @extends Highcharts.Series
  1740. */
  1741. BaseSeries.seriesType('networkgraph', 'line',
  1742. /**
  1743. * A networkgraph is a type of relationship chart, where connnections
  1744. * (links) attracts nodes (points) and other nodes repulse each other.
  1745. *
  1746. * @extends plotOptions.line
  1747. * @product highcharts
  1748. * @sample highcharts/demo/network-graph/
  1749. * Networkgraph
  1750. * @since 7.0.0
  1751. * @excluding boostThreshold, animation, animationLimit, connectEnds,
  1752. * colorAxis, colorKey, connectNulls, cropThreshold, dragDrop,
  1753. * getExtremesFromAll, label, linecap, negativeColor,
  1754. * pointInterval, pointIntervalUnit, pointPlacement,
  1755. * pointStart, softThreshold, stack, stacking, step,
  1756. * threshold, xAxis, yAxis, zoneAxis, dataSorting,
  1757. * boostBlending
  1758. * @requires modules/networkgraph
  1759. * @optionparent plotOptions.networkgraph
  1760. */
  1761. {
  1762. stickyTracking: false,
  1763. /**
  1764. * @ignore-option
  1765. * @private
  1766. */
  1767. inactiveOtherPoints: true,
  1768. marker: {
  1769. enabled: true,
  1770. states: {
  1771. /**
  1772. * The opposite state of a hover for a single point node.
  1773. * Applied to all not connected nodes to the hovered one.
  1774. *
  1775. * @declare Highcharts.PointStatesInactiveOptionsObject
  1776. */
  1777. inactive: {
  1778. /**
  1779. * Opacity of inactive markers.
  1780. */
  1781. opacity: 0.3,
  1782. /**
  1783. * Animation when not hovering over the node.
  1784. *
  1785. * @type {boolean|Partial<Highcharts.AnimationOptionsObject>}
  1786. */
  1787. animation: {
  1788. /** @internal */
  1789. duration: 50
  1790. }
  1791. }
  1792. }
  1793. },
  1794. states: {
  1795. /**
  1796. * The opposite state of a hover for a single point link. Applied
  1797. * to all links that are not comming from the hovered node.
  1798. *
  1799. * @declare Highcharts.SeriesStatesInactiveOptionsObject
  1800. */
  1801. inactive: {
  1802. /**
  1803. * Opacity of inactive links.
  1804. */
  1805. linkOpacity: 0.3,
  1806. /**
  1807. * Animation when not hovering over the node.
  1808. *
  1809. * @type {boolean|Partial<Highcharts.AnimationOptionsObject>}
  1810. */
  1811. animation: {
  1812. /** @internal */
  1813. duration: 50
  1814. }
  1815. }
  1816. },
  1817. /**
  1818. * @sample highcharts/series-networkgraph/link-datalabels
  1819. * Networkgraph with labels on links
  1820. * @sample highcharts/series-networkgraph/textpath-datalabels
  1821. * Networkgraph with labels around nodes
  1822. * @sample highcharts/series-networkgraph/link-datalabels
  1823. * Data labels moved into the nodes
  1824. * @sample highcharts/series-networkgraph/link-datalabels
  1825. * Data labels moved under the links
  1826. *
  1827. * @declare Highcharts.SeriesNetworkgraphDataLabelsOptionsObject
  1828. *
  1829. * @private
  1830. */
  1831. dataLabels: {
  1832. /**
  1833. * The
  1834. * [format string](https://www.highcharts.com/docs/chart-concepts/labels-and-string-formatting)
  1835. * specifying what to show for _node_ in the networkgraph. In v7.0
  1836. * defaults to `{key}`, since v7.1 defaults to `undefined` and
  1837. * `formatter` is used instead.
  1838. *
  1839. * @type {string}
  1840. * @since 7.0.0
  1841. * @apioption plotOptions.networkgraph.dataLabels.format
  1842. */
  1843. // eslint-disable-next-line valid-jsdoc
  1844. /**
  1845. * Callback JavaScript function to format the data label for a node.
  1846. * Note that if a `format` is defined, the format takes precedence
  1847. * and the formatter is ignored.
  1848. *
  1849. * @type {Highcharts.SeriesNetworkgraphDataLabelsFormatterCallbackFunction}
  1850. * @since 7.0.0
  1851. */
  1852. formatter: function () {
  1853. return this.key;
  1854. },
  1855. /**
  1856. * The
  1857. * [format string](https://www.highcharts.com/docs/chart-concepts/labels-and-string-formatting)
  1858. * specifying what to show for _links_ in the networkgraph.
  1859. * (Default: `undefined`)
  1860. *
  1861. * @type {string}
  1862. * @since 7.1.0
  1863. * @apioption plotOptions.networkgraph.dataLabels.linkFormat
  1864. */
  1865. // eslint-disable-next-line valid-jsdoc
  1866. /**
  1867. * Callback to format data labels for _links_ in the sankey diagram.
  1868. * The `linkFormat` option takes precedence over the
  1869. * `linkFormatter`.
  1870. *
  1871. * @type {Highcharts.SeriesNetworkgraphDataLabelsFormatterCallbackFunction}
  1872. * @since 7.1.0
  1873. */
  1874. linkFormatter: function () {
  1875. return (this.point.fromNode.name +
  1876. '<br>' +
  1877. this.point.toNode.name);
  1878. },
  1879. /**
  1880. * Options for a _link_ label text which should follow link
  1881. * connection. Border and background are disabled for a label that
  1882. * follows a path.
  1883. *
  1884. * **Note:** Only SVG-based renderer supports this option. Setting
  1885. * `useHTML` to true will disable this option.
  1886. *
  1887. * @extends plotOptions.networkgraph.dataLabels.textPath
  1888. * @since 7.1.0
  1889. */
  1890. linkTextPath: {
  1891. enabled: true
  1892. },
  1893. textPath: {
  1894. enabled: false
  1895. },
  1896. style: {
  1897. transition: 'opacity 2000ms'
  1898. }
  1899. },
  1900. /**
  1901. * Link style options
  1902. * @private
  1903. */
  1904. link: {
  1905. /**
  1906. * A name for the dash style to use for links.
  1907. *
  1908. * @type {string}
  1909. * @apioption plotOptions.networkgraph.link.dashStyle
  1910. */
  1911. /**
  1912. * Color of the link between two nodes.
  1913. */
  1914. color: 'rgba(100, 100, 100, 0.5)',
  1915. /**
  1916. * Width (px) of the link between two nodes.
  1917. */
  1918. width: 1
  1919. },
  1920. /**
  1921. * Flag to determine if nodes are draggable or not.
  1922. * @private
  1923. */
  1924. draggable: true,
  1925. layoutAlgorithm: {
  1926. /**
  1927. * Repulsive force applied on a node. Passed are two arguments:
  1928. * - `d` - which is current distance between two nodes
  1929. * - `k` - which is desired distance between two nodes
  1930. *
  1931. * In `verlet` integration, defaults to:
  1932. * `function (d, k) { return (k - d) / d * (k > d ? 1 : 0) }`
  1933. *
  1934. * @see [layoutAlgorithm.integration](#series.networkgraph.layoutAlgorithm.integration)
  1935. *
  1936. * @sample highcharts/series-networkgraph/forces/
  1937. * Custom forces with Euler integration
  1938. * @sample highcharts/series-networkgraph/cuboids/
  1939. * Custom forces with Verlet integration
  1940. *
  1941. * @type {Function}
  1942. * @default function (d, k) { return k * k / d; }
  1943. * @apioption plotOptions.networkgraph.layoutAlgorithm.repulsiveForce
  1944. */
  1945. /**
  1946. * Attraction force applied on a node which is conected to another
  1947. * node by a link. Passed are two arguments:
  1948. * - `d` - which is current distance between two nodes
  1949. * - `k` - which is desired distance between two nodes
  1950. *
  1951. * In `verlet` integration, defaults to:
  1952. * `function (d, k) { return (k - d) / d; }`
  1953. *
  1954. * @see [layoutAlgorithm.integration](#series.networkgraph.layoutAlgorithm.integration)
  1955. *
  1956. * @sample highcharts/series-networkgraph/forces/
  1957. * Custom forces with Euler integration
  1958. * @sample highcharts/series-networkgraph/cuboids/
  1959. * Custom forces with Verlet integration
  1960. *
  1961. * @type {Function}
  1962. * @default function (d, k) { return k * k / d; }
  1963. * @apioption plotOptions.networkgraph.layoutAlgorithm.attractiveForce
  1964. */
  1965. /**
  1966. * Ideal length (px) of the link between two nodes. When not
  1967. * defined, length is calculated as:
  1968. * `Math.pow(availableWidth * availableHeight / nodesLength, 0.4);`
  1969. *
  1970. * Note: Because of the algorithm specification, length of each link
  1971. * might be not exactly as specified.
  1972. *
  1973. * @sample highcharts/series-networkgraph/styled-links/
  1974. * Numerical values
  1975. *
  1976. * @type {number}
  1977. * @apioption plotOptions.networkgraph.layoutAlgorithm.linkLength
  1978. */
  1979. /**
  1980. * Initial layout algorithm for positioning nodes. Can be one of
  1981. * built-in options ("circle", "random") or a function where
  1982. * positions should be set on each node (`this.nodes`) as
  1983. * `node.plotX` and `node.plotY`
  1984. *
  1985. * @sample highcharts/series-networkgraph/initial-positions/
  1986. * Initial positions with callback
  1987. *
  1988. * @type {"circle"|"random"|Function}
  1989. */
  1990. initialPositions: 'circle',
  1991. /**
  1992. * When `initialPositions` are set to 'circle',
  1993. * `initialPositionRadius` is a distance from the center of circle,
  1994. * in which nodes are created.
  1995. *
  1996. * @type {number}
  1997. * @default 1
  1998. * @since 7.1.0
  1999. */
  2000. initialPositionRadius: 1,
  2001. /**
  2002. * Experimental. Enables live simulation of the algorithm
  2003. * implementation. All nodes are animated as the forces applies on
  2004. * them.
  2005. *
  2006. * @sample highcharts/demo/network-graph/
  2007. * Live simulation enabled
  2008. */
  2009. enableSimulation: false,
  2010. /**
  2011. * Barnes-Hut approximation only.
  2012. * Deteremines when distance between cell and node is small enough
  2013. * to caculate forces. Value of `theta` is compared directly with
  2014. * quotient `s / d`, where `s` is the size of the cell, and `d` is
  2015. * distance between center of cell's mass and currently compared
  2016. * node.
  2017. *
  2018. * @see [layoutAlgorithm.approximation](#series.networkgraph.layoutAlgorithm.approximation)
  2019. *
  2020. * @since 7.1.0
  2021. */
  2022. theta: 0.5,
  2023. /**
  2024. * Verlet integration only.
  2025. * Max speed that node can get in one iteration. In terms of
  2026. * simulation, it's a maximum translation (in pixels) that node can
  2027. * move (in both, x and y, dimensions). While `friction` is applied
  2028. * on all nodes, max speed is applied only for nodes that move very
  2029. * fast, for example small or disconnected ones.
  2030. *
  2031. * @see [layoutAlgorithm.integration](#series.networkgraph.layoutAlgorithm.integration)
  2032. * @see [layoutAlgorithm.friction](#series.networkgraph.layoutAlgorithm.friction)
  2033. *
  2034. * @since 7.1.0
  2035. */
  2036. maxSpeed: 10,
  2037. /**
  2038. * Approximation used to calculate repulsive forces affecting nodes.
  2039. * By default, when calculateing net force, nodes are compared
  2040. * against each other, which gives O(N^2) complexity. Using
  2041. * Barnes-Hut approximation, we decrease this to O(N log N), but the
  2042. * resulting graph will have different layout. Barnes-Hut
  2043. * approximation divides space into rectangles via quad tree, where
  2044. * forces exerted on nodes are calculated directly for nearby cells,
  2045. * and for all others, cells are treated as a separate node with
  2046. * center of mass.
  2047. *
  2048. * @see [layoutAlgorithm.theta](#series.networkgraph.layoutAlgorithm.theta)
  2049. *
  2050. * @sample highcharts/series-networkgraph/barnes-hut-approximation/
  2051. * A graph with Barnes-Hut approximation
  2052. *
  2053. * @type {string}
  2054. * @validvalue ["barnes-hut", "none"]
  2055. * @since 7.1.0
  2056. */
  2057. approximation: 'none',
  2058. /**
  2059. * Type of the algorithm used when positioning nodes.
  2060. *
  2061. * @type {string}
  2062. * @validvalue ["reingold-fruchterman"]
  2063. */
  2064. type: 'reingold-fruchterman',
  2065. /**
  2066. * Integration type. Available options are `'euler'` and `'verlet'`.
  2067. * Integration determines how forces are applied on particles. In
  2068. * Euler integration, force is applied direct as
  2069. * `newPosition += velocity;`.
  2070. * In Verlet integration, new position is based on a previous
  2071. * posittion without velocity:
  2072. * `newPosition += previousPosition - newPosition`.
  2073. *
  2074. * Note that different integrations give different results as forces
  2075. * are different.
  2076. *
  2077. * In Highcharts v7.0.x only `'euler'` integration was supported.
  2078. *
  2079. * @sample highcharts/series-networkgraph/integration-comparison/
  2080. * Comparison of Verlet and Euler integrations
  2081. *
  2082. * @type {string}
  2083. * @validvalue ["euler", "verlet"]
  2084. * @since 7.1.0
  2085. */
  2086. integration: 'euler',
  2087. /**
  2088. * Max number of iterations before algorithm will stop. In general,
  2089. * algorithm should find positions sooner, but when rendering huge
  2090. * number of nodes, it is recommended to increase this value as
  2091. * finding perfect graph positions can require more time.
  2092. */
  2093. maxIterations: 1000,
  2094. /**
  2095. * Gravitational const used in the barycenter force of the
  2096. * algorithm.
  2097. *
  2098. * @sample highcharts/series-networkgraph/forces/
  2099. * Custom forces with Euler integration
  2100. */
  2101. gravitationalConstant: 0.0625,
  2102. /**
  2103. * Friction applied on forces to prevent nodes rushing to fast to
  2104. * the desired positions.
  2105. */
  2106. friction: -0.981
  2107. },
  2108. showInLegend: false
  2109. }, {
  2110. /**
  2111. * Array of internal forces. Each force should be later defined in
  2112. * integrations.js.
  2113. * @private
  2114. */
  2115. forces: ['barycenter', 'repulsive', 'attractive'],
  2116. hasDraggableNodes: true,
  2117. drawGraph: null,
  2118. isCartesian: false,
  2119. requireSorting: false,
  2120. directTouch: true,
  2121. noSharedTooltip: true,
  2122. pointArrayMap: ['from', 'to'],
  2123. trackerGroups: ['group', 'markerGroup', 'dataLabelsGroup'],
  2124. drawTracker: H.TrackerMixin.drawTrackerPoint,
  2125. // Animation is run in `series.simulation`.
  2126. animate: null,
  2127. buildKDTree: H.noop,
  2128. /**
  2129. * Create a single node that holds information on incoming and outgoing
  2130. * links.
  2131. * @private
  2132. */
  2133. createNode: NodesMixin.createNode,
  2134. destroy: function () {
  2135. if (this.layout) {
  2136. this.layout.removeElementFromCollection(this, this.layout.series);
  2137. }
  2138. NodesMixin.destroy.call(this);
  2139. },
  2140. /* eslint-disable no-invalid-this, valid-jsdoc */
  2141. /**
  2142. * Extend init with base event, which should stop simulation during
  2143. * update. After data is updated, `chart.render` resumes the simulation.
  2144. * @private
  2145. */
  2146. init: function () {
  2147. Series.prototype.init.apply(this, arguments);
  2148. addEvent(this, 'updatedData', function () {
  2149. if (this.layout) {
  2150. this.layout.stop();
  2151. }
  2152. });
  2153. return this;
  2154. },
  2155. /**
  2156. * Extend generatePoints by adding the nodes, which are Point objects
  2157. * but pushed to the this.nodes array.
  2158. * @private
  2159. */
  2160. generatePoints: function () {
  2161. var node,
  2162. i;
  2163. NodesMixin.generatePoints.apply(this, arguments);
  2164. // In networkgraph, it's fine to define stanalone nodes, create
  2165. // them:
  2166. if (this.options.nodes) {
  2167. this.options.nodes.forEach(function (nodeOptions) {
  2168. if (!this.nodeLookup[nodeOptions.id]) {
  2169. this.nodeLookup[nodeOptions.id] =
  2170. this.createNode(nodeOptions.id);
  2171. }
  2172. }, this);
  2173. }
  2174. for (i = this.nodes.length - 1; i >= 0; i--) {
  2175. node = this.nodes[i];
  2176. node.degree = node.getDegree();
  2177. node.radius = pick(node.marker && node.marker.radius, this.options.marker && this.options.marker.radius, 0);
  2178. // If node exists, but it's not available in nodeLookup,
  2179. // then it's leftover from previous runs (e.g. setData)
  2180. if (!this.nodeLookup[node.id]) {
  2181. node.remove();
  2182. }
  2183. }
  2184. this.data.forEach(function (link) {
  2185. link.formatPrefix = 'link';
  2186. });
  2187. this.indexateNodes();
  2188. },
  2189. /**
  2190. * In networkgraph, series.points refers to links,
  2191. * but series.nodes refers to actual points.
  2192. * @private
  2193. */
  2194. getPointsCollection: function () {
  2195. return this.nodes || [];
  2196. },
  2197. /**
  2198. * Set index for each node. Required for proper `node.update()`.
  2199. * Note that links are indexated out of the box in `generatePoints()`.
  2200. *
  2201. * @private
  2202. */
  2203. indexateNodes: function () {
  2204. this.nodes.forEach(function (node, index) {
  2205. node.index = index;
  2206. });
  2207. },
  2208. /**
  2209. * Extend the default marker attribs by using a non-rounded X position,
  2210. * otherwise the nodes will jump from pixel to pixel which looks a bit
  2211. * jaggy when approaching equilibrium.
  2212. * @private
  2213. */
  2214. markerAttribs: function (point, state) {
  2215. var attribs = Series.prototype.markerAttribs.call(this,
  2216. point,
  2217. state);
  2218. // series.render() is called before initial positions are set:
  2219. if (!defined(point.plotY)) {
  2220. attribs.y = 0;
  2221. }
  2222. attribs.x = (point.plotX || 0) - (attribs.width / 2 || 0);
  2223. return attribs;
  2224. },
  2225. /**
  2226. * Run pre-translation and register nodes&links to the deffered layout.
  2227. * @private
  2228. */
  2229. translate: function () {
  2230. if (!this.processedXData) {
  2231. this.processData();
  2232. }
  2233. this.generatePoints();
  2234. this.deferLayout();
  2235. this.nodes.forEach(function (node) {
  2236. // Draw the links from this node
  2237. node.isInside = true;
  2238. node.linksFrom.forEach(function (point) {
  2239. point.shapeType = 'path';
  2240. // Pass test in drawPoints
  2241. point.y = 1;
  2242. });
  2243. });
  2244. },
  2245. /**
  2246. * Defer the layout.
  2247. * Each series first registers all nodes and links, then layout
  2248. * calculates all nodes positions and calls `series.render()` in every
  2249. * simulation step.
  2250. *
  2251. * Note:
  2252. * Animation is done through `requestAnimationFrame` directly, without
  2253. * `Highcharts.animate()` use.
  2254. * @private
  2255. */
  2256. deferLayout: function () {
  2257. var layoutOptions = this.options.layoutAlgorithm,
  2258. graphLayoutsStorage = this.chart.graphLayoutsStorage,
  2259. graphLayoutsLookup = this.chart.graphLayoutsLookup,
  2260. chartOptions = this.chart.options.chart,
  2261. layout;
  2262. if (!this.visible) {
  2263. return;
  2264. }
  2265. if (!graphLayoutsStorage) {
  2266. this.chart.graphLayoutsStorage = graphLayoutsStorage = {};
  2267. this.chart.graphLayoutsLookup = graphLayoutsLookup = [];
  2268. }
  2269. layout = graphLayoutsStorage[layoutOptions.type];
  2270. if (!layout) {
  2271. layoutOptions.enableSimulation =
  2272. !defined(chartOptions.forExport) ?
  2273. layoutOptions.enableSimulation :
  2274. !chartOptions.forExport;
  2275. graphLayoutsStorage[layoutOptions.type] = layout =
  2276. new H.layouts[layoutOptions.type]();
  2277. layout.init(layoutOptions);
  2278. graphLayoutsLookup.splice(layout.index, 0, layout);
  2279. }
  2280. this.layout = layout;
  2281. layout.setArea(0, 0, this.chart.plotWidth, this.chart.plotHeight);
  2282. layout.addElementsToCollection([this], layout.series);
  2283. layout.addElementsToCollection(this.nodes, layout.nodes);
  2284. layout.addElementsToCollection(this.points, layout.links);
  2285. },
  2286. /**
  2287. * Extend the render function to also render this.nodes together with
  2288. * the points.
  2289. * @private
  2290. */
  2291. render: function () {
  2292. var series = this,
  2293. points = series.points,
  2294. hoverPoint = series.chart.hoverPoint,
  2295. dataLabels = [];
  2296. // Render markers:
  2297. series.points = series.nodes;
  2298. seriesTypes.line.prototype.render.call(this);
  2299. series.points = points;
  2300. points.forEach(function (point) {
  2301. if (point.fromNode && point.toNode) {
  2302. point.renderLink();
  2303. point.redrawLink();
  2304. }
  2305. });
  2306. if (hoverPoint && hoverPoint.series === series) {
  2307. series.redrawHalo(hoverPoint);
  2308. }
  2309. if (series.chart.hasRendered &&
  2310. !series.options.dataLabels.allowOverlap) {
  2311. series.nodes.concat(series.points).forEach(function (node) {
  2312. if (node.dataLabel) {
  2313. dataLabels.push(node.dataLabel);
  2314. }
  2315. });
  2316. series.chart.hideOverlappingLabels(dataLabels);
  2317. }
  2318. },
  2319. // Networkgraph has two separate collecions of nodes and lines, render
  2320. // dataLabels for both sets:
  2321. drawDataLabels: function () {
  2322. var textPath = this.options.dataLabels.textPath;
  2323. // Render node labels:
  2324. Series.prototype.drawDataLabels.apply(this, arguments);
  2325. // Render link labels:
  2326. this.points = this.data;
  2327. this.options.dataLabels.textPath =
  2328. this.options.dataLabels.linkTextPath;
  2329. Series.prototype.drawDataLabels.apply(this, arguments);
  2330. // Restore nodes
  2331. this.points = this.nodes;
  2332. this.options.dataLabels.textPath = textPath;
  2333. },
  2334. // Return the presentational attributes.
  2335. pointAttribs: function (point, state) {
  2336. // By default, only `selected` state is passed on
  2337. var pointState = state || point && point.state || 'normal',
  2338. attribs = Series.prototype.pointAttribs.call(this,
  2339. point,
  2340. pointState),
  2341. stateOptions = this.options.states[pointState];
  2342. if (point && !point.isNode) {
  2343. attribs = point.getLinkAttributes();
  2344. // For link, get prefixed names:
  2345. if (stateOptions) {
  2346. attribs = {
  2347. // TO DO: API?
  2348. stroke: stateOptions.linkColor || attribs.stroke,
  2349. dashstyle: (stateOptions.linkDashStyle || attribs.dashstyle),
  2350. opacity: pick(stateOptions.linkOpacity, attribs.opacity),
  2351. 'stroke-width': stateOptions.linkColor ||
  2352. attribs['stroke-width']
  2353. };
  2354. }
  2355. }
  2356. return attribs;
  2357. },
  2358. // Draggable mode:
  2359. /**
  2360. * Redraw halo on mousemove during the drag&drop action.
  2361. * @private
  2362. * @param {Highcharts.Point} point The point that should show halo.
  2363. */
  2364. redrawHalo: dragNodesMixin.redrawHalo,
  2365. /**
  2366. * Mouse down action, initializing drag&drop mode.
  2367. * @private
  2368. * @param {global.Event} event Browser event, before normalization.
  2369. * @param {Highcharts.Point} point The point that event occured.
  2370. */
  2371. onMouseDown: dragNodesMixin.onMouseDown,
  2372. /**
  2373. * Mouse move action during drag&drop.
  2374. * @private
  2375. * @param {global.Event} event Browser event, before normalization.
  2376. * @param {Highcharts.Point} point The point that event occured.
  2377. */
  2378. onMouseMove: dragNodesMixin.onMouseMove,
  2379. /**
  2380. * Mouse up action, finalizing drag&drop.
  2381. * @private
  2382. * @param {Highcharts.Point} point The point that event occured.
  2383. */
  2384. onMouseUp: dragNodesMixin.onMouseUp,
  2385. /**
  2386. * When state should be passed down to all points, concat nodes and
  2387. * links and apply this state to all of them.
  2388. * @private
  2389. */
  2390. setState: function (state, inherit) {
  2391. if (inherit) {
  2392. this.points = this.nodes.concat(this.data);
  2393. Series.prototype.setState.apply(this, arguments);
  2394. this.points = this.data;
  2395. }
  2396. else {
  2397. Series.prototype.setState.apply(this, arguments);
  2398. }
  2399. // If simulation is done, re-render points with new states:
  2400. if (!this.layout.simulation && !state) {
  2401. this.render();
  2402. }
  2403. }
  2404. }, {
  2405. setState: NodesMixin.setNodeState,
  2406. /**
  2407. * Basic `point.init()` and additional styles applied when
  2408. * `series.draggable` is enabled.
  2409. * @private
  2410. */
  2411. init: function () {
  2412. Point.prototype.init.apply(this, arguments);
  2413. if (this.series.options.draggable &&
  2414. !this.series.chart.styledMode) {
  2415. addEvent(this, 'mouseOver', function () {
  2416. css(this.series.chart.container, { cursor: 'move' });
  2417. });
  2418. addEvent(this, 'mouseOut', function () {
  2419. css(this.series.chart.container, { cursor: 'default' });
  2420. });
  2421. }
  2422. return this;
  2423. },
  2424. /**
  2425. * Return degree of a node. If node has no connections, it still has
  2426. * deg=1.
  2427. * @private
  2428. * @return {number}
  2429. */
  2430. getDegree: function () {
  2431. var deg = this.isNode ?
  2432. this.linksFrom.length + this.linksTo.length :
  2433. 0;
  2434. return deg === 0 ? 1 : deg;
  2435. },
  2436. // Links:
  2437. /**
  2438. * Get presentational attributes of link connecting two nodes.
  2439. * @private
  2440. * @return {Highcharts.SVGAttributes}
  2441. */
  2442. getLinkAttributes: function () {
  2443. var linkOptions = this.series.options.link,
  2444. pointOptions = this.options;
  2445. return {
  2446. 'stroke-width': pick(pointOptions.width, linkOptions.width),
  2447. stroke: (pointOptions.color || linkOptions.color),
  2448. dashstyle: (pointOptions.dashStyle || linkOptions.dashStyle),
  2449. opacity: pick(pointOptions.opacity, linkOptions.opacity, 1)
  2450. };
  2451. },
  2452. /**
  2453. * Render link and add it to the DOM.
  2454. * @private
  2455. */
  2456. renderLink: function () {
  2457. var attribs;
  2458. if (!this.graphic) {
  2459. this.graphic = this.series.chart.renderer
  2460. .path(this.getLinkPath())
  2461. .add(this.series.group);
  2462. if (!this.series.chart.styledMode) {
  2463. attribs = this.series.pointAttribs(this);
  2464. this.graphic.attr(attribs);
  2465. (this.dataLabels || []).forEach(function (label) {
  2466. if (label) {
  2467. label.attr({
  2468. opacity: attribs.opacity
  2469. });
  2470. }
  2471. });
  2472. }
  2473. }
  2474. },
  2475. /**
  2476. * Redraw link's path.
  2477. * @private
  2478. */
  2479. redrawLink: function () {
  2480. var path = this.getLinkPath(),
  2481. attribs;
  2482. if (this.graphic) {
  2483. this.shapeArgs = {
  2484. d: path
  2485. };
  2486. if (!this.series.chart.styledMode) {
  2487. attribs = this.series.pointAttribs(this);
  2488. this.graphic.attr(attribs);
  2489. (this.dataLabels || []).forEach(function (label) {
  2490. if (label) {
  2491. label.attr({
  2492. opacity: attribs.opacity
  2493. });
  2494. }
  2495. });
  2496. }
  2497. this.graphic.animate(this.shapeArgs);
  2498. // Required for dataLabels
  2499. var start = path[0];
  2500. var end = path[1];
  2501. if (start[0] === 'M' && end[0] === 'L') {
  2502. this.plotX = (start[1] + end[1]) / 2;
  2503. this.plotY = (start[2] + end[2]) / 2;
  2504. }
  2505. }
  2506. },
  2507. /**
  2508. * Get mass fraction applied on two nodes connected to each other. By
  2509. * default, when mass is equal to `1`, mass fraction for both nodes
  2510. * equal to 0.5.
  2511. * @private
  2512. * @return {Highcharts.Dictionary<number>}
  2513. * For example `{ fromNode: 0.5, toNode: 0.5 }`
  2514. */
  2515. getMass: function () {
  2516. var m1 = this.fromNode.mass,
  2517. m2 = this.toNode.mass,
  2518. sum = m1 + m2;
  2519. return {
  2520. fromNode: 1 - m1 / sum,
  2521. toNode: 1 - m2 / sum
  2522. };
  2523. },
  2524. /**
  2525. * Get link path connecting two nodes.
  2526. * @private
  2527. * @return {Array<Highcharts.SVGPathArray>}
  2528. * Path: `['M', x, y, 'L', x, y]`
  2529. */
  2530. getLinkPath: function () {
  2531. var left = this.fromNode,
  2532. right = this.toNode;
  2533. // Start always from left to the right node, to prevent rendering
  2534. // labels upside down
  2535. if (left.plotX > right.plotX) {
  2536. left = this.toNode;
  2537. right = this.fromNode;
  2538. }
  2539. return [
  2540. ['M', left.plotX || 0, left.plotY || 0],
  2541. ['L', right.plotX || 0, right.plotY || 0]
  2542. ];
  2543. /*
  2544. IDEA: different link shapes?
  2545. return [
  2546. 'M',
  2547. from.plotX,
  2548. from.plotY,
  2549. 'Q',
  2550. (to.plotX + from.plotX) / 2,
  2551. (to.plotY + from.plotY) / 2 + 15,
  2552. to.plotX,
  2553. to.plotY
  2554. ];*/
  2555. },
  2556. isValid: function () {
  2557. return !this.isNode || defined(this.id);
  2558. },
  2559. /**
  2560. * Common method for removing points and nodes in networkgraph. To
  2561. * remove `link`, use `series.data[index].remove()`. To remove `node`
  2562. * with all connections, use `series.nodes[index].remove()`.
  2563. * @private
  2564. * @param {boolean} [redraw=true]
  2565. * Whether to redraw the chart or wait for an explicit call. When
  2566. * doing more operations on the chart, for example running
  2567. * `point.remove()` in a loop, it is best practice to set
  2568. * `redraw` to false and call `chart.redraw()` after.
  2569. * @param {boolean|Partial<Highcharts.AnimationOptionsObject>} [animation=false]
  2570. * Whether to apply animation, and optionally animation
  2571. * configuration.
  2572. * @return {void}
  2573. */
  2574. remove: function (redraw, animation) {
  2575. var point = this,
  2576. series = point.series,
  2577. nodesOptions = series.options.nodes || [],
  2578. index,
  2579. i = nodesOptions.length;
  2580. // For nodes, remove all connected links:
  2581. if (point.isNode) {
  2582. // Temporary disable series.points array, because
  2583. // Series.removePoint() modifies it
  2584. series.points = [];
  2585. // Remove link from all nodes collections:
  2586. []
  2587. .concat(point.linksFrom)
  2588. .concat(point.linksTo)
  2589. .forEach(function (linkFromTo) {
  2590. // Incoming links
  2591. index = linkFromTo.fromNode.linksFrom.indexOf(linkFromTo);
  2592. if (index > -1) {
  2593. linkFromTo.fromNode.linksFrom.splice(index, 1);
  2594. }
  2595. // Outcoming links
  2596. index = linkFromTo.toNode.linksTo.indexOf(linkFromTo);
  2597. if (index > -1) {
  2598. linkFromTo.toNode.linksTo.splice(index, 1);
  2599. }
  2600. // Remove link from data/points collections
  2601. Series.prototype.removePoint.call(series, series.data.indexOf(linkFromTo), false, false);
  2602. });
  2603. // Restore points array, after links are removed
  2604. series.points = series.data.slice();
  2605. // Proceed with removing node. It's similar to
  2606. // Series.removePoint() method, but doesn't modify other arrays
  2607. series.nodes.splice(series.nodes.indexOf(point), 1);
  2608. // Remove node options from config
  2609. while (i--) {
  2610. if (nodesOptions[i].id === point.options.id) {
  2611. series.options.nodes.splice(i, 1);
  2612. break;
  2613. }
  2614. }
  2615. if (point) {
  2616. point.destroy();
  2617. }
  2618. // Run redraw if requested
  2619. series.isDirty = true;
  2620. series.isDirtyData = true;
  2621. if (redraw) {
  2622. series.chart.redraw(redraw);
  2623. }
  2624. }
  2625. else {
  2626. series.removePoint(series.data.indexOf(point), redraw, animation);
  2627. }
  2628. },
  2629. /**
  2630. * Destroy point. If it's a node, remove all links coming out of this
  2631. * node. Then remove point from the layout.
  2632. * @private
  2633. * @return {void}
  2634. */
  2635. destroy: function () {
  2636. if (this.isNode) {
  2637. this.linksFrom.concat(this.linksTo).forEach(function (link) {
  2638. // Removing multiple nodes at the same time
  2639. // will try to remove link between nodes twice
  2640. if (link.destroyElements) {
  2641. link.destroyElements();
  2642. }
  2643. });
  2644. }
  2645. this.series.layout.removeElementFromCollection(this, this.series.layout[this.isNode ? 'nodes' : 'links']);
  2646. return Point.prototype.destroy.apply(this, arguments);
  2647. }
  2648. });
  2649. /**
  2650. * A `networkgraph` series. If the [type](#series.networkgraph.type) option is
  2651. * not specified, it is inherited from [chart.type](#chart.type).
  2652. *
  2653. * @extends series,plotOptions.networkgraph
  2654. * @excluding boostThreshold, animation, animationLimit, connectEnds,
  2655. * connectNulls, cropThreshold, dragDrop, getExtremesFromAll, label,
  2656. * linecap, negativeColor, pointInterval, pointIntervalUnit,
  2657. * pointPlacement, pointStart, softThreshold, stack, stacking,
  2658. * step, threshold, xAxis, yAxis, zoneAxis, dataSorting,
  2659. * boostBlending
  2660. * @product highcharts
  2661. * @requires modules/networkgraph
  2662. * @apioption series.networkgraph
  2663. */
  2664. /**
  2665. * An array of data points for the series. For the `networkgraph` series type,
  2666. * points can be given in the following way:
  2667. *
  2668. * An array of objects with named values. The following snippet shows only a
  2669. * few settings, see the complete options set below. If the total number of
  2670. * data points exceeds the series'
  2671. * [turboThreshold](#series.area.turboThreshold), this option is not available.
  2672. *
  2673. * ```js
  2674. * data: [{
  2675. * from: 'Category1',
  2676. * to: 'Category2'
  2677. * }, {
  2678. * from: 'Category1',
  2679. * to: 'Category3'
  2680. * }]
  2681. * ```
  2682. *
  2683. * @type {Array<Object|Array|Number>}
  2684. * @extends series.line.data
  2685. * @excluding drilldown,marker,x,y,draDrop
  2686. * @sample {highcharts} highcharts/chart/reflow-true/
  2687. * Numerical values
  2688. * @sample {highcharts} highcharts/series/data-array-of-arrays/
  2689. * Arrays of numeric x and y
  2690. * @sample {highcharts} highcharts/series/data-array-of-arrays-datetime/
  2691. * Arrays of datetime x and y
  2692. * @sample {highcharts} highcharts/series/data-array-of-name-value/
  2693. * Arrays of point.name and y
  2694. * @sample {highcharts} highcharts/series/data-array-of-objects/
  2695. * Config objects
  2696. * @product highcharts
  2697. * @apioption series.networkgraph.data
  2698. */
  2699. /**
  2700. * @type {Highcharts.SeriesNetworkgraphDataLabelsOptionsObject|Array<Highcharts.SeriesNetworkgraphDataLabelsOptionsObject>}
  2701. * @product highcharts
  2702. * @apioption series.networkgraph.data.dataLabels
  2703. */
  2704. /**
  2705. * The node that the link runs from.
  2706. *
  2707. * @type {string}
  2708. * @product highcharts
  2709. * @apioption series.networkgraph.data.from
  2710. */
  2711. /**
  2712. * The node that the link runs to.
  2713. *
  2714. * @type {string}
  2715. * @product highcharts
  2716. * @apioption series.networkgraph.data.to
  2717. */
  2718. /**
  2719. * A collection of options for the individual nodes. The nodes in a
  2720. * networkgraph diagram are auto-generated instances of `Highcharts.Point`,
  2721. * but options can be applied here and linked by the `id`.
  2722. *
  2723. * @sample highcharts/series-networkgraph/data-options/
  2724. * Networkgraph diagram with node options
  2725. *
  2726. * @type {Array<*>}
  2727. * @product highcharts
  2728. * @apioption series.networkgraph.nodes
  2729. */
  2730. /**
  2731. * The id of the auto-generated node, refering to the `from` or `to` setting of
  2732. * the link.
  2733. *
  2734. * @type {string}
  2735. * @product highcharts
  2736. * @apioption series.networkgraph.nodes.id
  2737. */
  2738. /**
  2739. * The color of the auto generated node.
  2740. *
  2741. * @type {Highcharts.ColorString}
  2742. * @product highcharts
  2743. * @apioption series.networkgraph.nodes.color
  2744. */
  2745. /**
  2746. * The color index of the auto generated node, especially for use in styled
  2747. * mode.
  2748. *
  2749. * @type {number}
  2750. * @product highcharts
  2751. * @apioption series.networkgraph.nodes.colorIndex
  2752. */
  2753. /**
  2754. * The name to display for the node in data labels and tooltips. Use this when
  2755. * the name is different from the `id`. Where the id must be unique for each
  2756. * node, this is not necessary for the name.
  2757. *
  2758. * @sample highcharts/series-networkgraph/data-options/
  2759. * Networkgraph diagram with node options
  2760. *
  2761. * @type {string}
  2762. * @product highcharts
  2763. * @apioption series.networkgraph.nodes.name
  2764. */
  2765. /**
  2766. * Mass of the node. By default, each node has mass equal to it's marker radius
  2767. * . Mass is used to determine how two connected nodes should affect
  2768. * each other:
  2769. *
  2770. * Attractive force is multiplied by the ratio of two connected
  2771. * nodes; if a big node has weights twice as the small one, then the small one
  2772. * will move towards the big one twice faster than the big one to the small one
  2773. * .
  2774. *
  2775. * @sample highcharts/series-networkgraph/ragdoll/
  2776. * Mass determined by marker.radius
  2777. *
  2778. * @type {number}
  2779. * @product highcharts
  2780. * @apioption series.networkgraph.nodes.mass
  2781. */
  2782. /**
  2783. * Individual data label for each node. The options are the same as
  2784. * the ones for [series.networkgraph.dataLabels](#series.networkgraph.dataLabels).
  2785. *
  2786. * @type {Highcharts.SeriesNetworkgraphDataLabelsOptionsObject|Array<Highcharts.SeriesNetworkgraphDataLabelsOptionsObject>}
  2787. *
  2788. * @apioption series.networkgraph.nodes.dataLabels
  2789. */
  2790. ''; // adds doclets above to transpiled file
  2791. });
  2792. _registerModule(_modules, 'masters/modules/networkgraph.src.js', [], function () {
  2793. });
  2794. }));