vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

713 lines
34 KiB

  1. var util = require('../../../util');
  2. var RepulsionMixin = require('./RepulsionMixin');
  3. var HierarchialRepulsionMixin = require('./HierarchialRepulsionMixin');
  4. var BarnesHutMixin = require('./BarnesHutMixin');
  5. /**
  6. * Toggling barnes Hut calculation on and off.
  7. *
  8. * @private
  9. */
  10. exports._toggleBarnesHut = function () {
  11. this.constants.physics.barnesHut.enabled = !this.constants.physics.barnesHut.enabled;
  12. this._loadSelectedForceSolver();
  13. this.moving = true;
  14. this.start();
  15. };
  16. /**
  17. * This loads the node force solver based on the barnes hut or repulsion algorithm
  18. *
  19. * @private
  20. */
  21. exports._loadSelectedForceSolver = function () {
  22. // this overloads the this._calculateNodeForces
  23. if (this.constants.physics.barnesHut.enabled == true) {
  24. this._clearMixin(RepulsionMixin);
  25. this._clearMixin(HierarchialRepulsionMixin);
  26. this.constants.physics.centralGravity = this.constants.physics.barnesHut.centralGravity;
  27. this.constants.physics.springLength = this.constants.physics.barnesHut.springLength;
  28. this.constants.physics.springConstant = this.constants.physics.barnesHut.springConstant;
  29. this.constants.physics.damping = this.constants.physics.barnesHut.damping;
  30. this._loadMixin(BarnesHutMixin);
  31. }
  32. else if (this.constants.physics.hierarchicalRepulsion.enabled == true) {
  33. this._clearMixin(BarnesHutMixin);
  34. this._clearMixin(RepulsionMixin);
  35. this.constants.physics.centralGravity = this.constants.physics.hierarchicalRepulsion.centralGravity;
  36. this.constants.physics.springLength = this.constants.physics.hierarchicalRepulsion.springLength;
  37. this.constants.physics.springConstant = this.constants.physics.hierarchicalRepulsion.springConstant;
  38. this.constants.physics.damping = this.constants.physics.hierarchicalRepulsion.damping;
  39. this._loadMixin(HierarchialRepulsionMixin);
  40. }
  41. else {
  42. this._clearMixin(BarnesHutMixin);
  43. this._clearMixin(HierarchialRepulsionMixin);
  44. this.barnesHutTree = undefined;
  45. this.constants.physics.centralGravity = this.constants.physics.repulsion.centralGravity;
  46. this.constants.physics.springLength = this.constants.physics.repulsion.springLength;
  47. this.constants.physics.springConstant = this.constants.physics.repulsion.springConstant;
  48. this.constants.physics.damping = this.constants.physics.repulsion.damping;
  49. this._loadMixin(RepulsionMixin);
  50. }
  51. };
  52. /**
  53. * Before calculating the forces, we check if we need to cluster to keep up performance and we check
  54. * if there is more than one node. If it is just one node, we dont calculate anything.
  55. *
  56. * @private
  57. */
  58. exports._initializeForceCalculation = function () {
  59. // stop calculation if there is only one node
  60. if (this.nodeIndices.length == 1) {
  61. this.nodes[this.nodeIndices[0]]._setForce(0, 0);
  62. }
  63. else {
  64. // we now start the force calculation
  65. this._calculateForces();
  66. }
  67. };
  68. /**
  69. * Calculate the external forces acting on the nodes
  70. * Forces are caused by: edges, repulsing forces between nodes, gravity
  71. * @private
  72. */
  73. exports._calculateForces = function () {
  74. // Gravity is required to keep separated groups from floating off
  75. // the forces are reset to zero in this loop by using _setForce instead
  76. // of _addForce
  77. this._calculateGravitationalForces();
  78. this._calculateNodeForces();
  79. if (this.constants.physics.springConstant > 0) {
  80. if (this.constants.smoothCurves.enabled == true && this.constants.smoothCurves.dynamic == true) {
  81. this._calculateSpringForcesWithSupport();
  82. }
  83. else {
  84. if (this.constants.physics.hierarchicalRepulsion.enabled == true) {
  85. this._calculateHierarchicalSpringForces();
  86. }
  87. else {
  88. this._calculateSpringForces();
  89. }
  90. }
  91. }
  92. };
  93. /**
  94. * Smooth curves are created by adding invisible nodes in the center of the edges. These nodes are also
  95. * handled in the calculateForces function. We then use a quadratic curve with the center node as control.
  96. * This function joins the datanodes and invisible (called support) nodes into one object.
  97. * We do this so we do not contaminate this.nodes with the support nodes.
  98. *
  99. * @private
  100. */
  101. exports._updateCalculationNodes = function () {
  102. if (this.constants.smoothCurves.enabled == true && this.constants.smoothCurves.dynamic == true) {
  103. this.calculationNodes = {};
  104. this.calculationNodeIndices = [];
  105. for (var nodeId in this.nodes) {
  106. if (this.nodes.hasOwnProperty(nodeId)) {
  107. this.calculationNodes[nodeId] = this.nodes[nodeId];
  108. }
  109. }
  110. var supportNodes = this.sectors['support']['nodes'];
  111. for (var supportNodeId in supportNodes) {
  112. if (supportNodes.hasOwnProperty(supportNodeId)) {
  113. if (this.edges.hasOwnProperty(supportNodes[supportNodeId].parentEdgeId)) {
  114. this.calculationNodes[supportNodeId] = supportNodes[supportNodeId];
  115. }
  116. else {
  117. supportNodes[supportNodeId]._setForce(0, 0);
  118. }
  119. }
  120. }
  121. for (var idx in this.calculationNodes) {
  122. if (this.calculationNodes.hasOwnProperty(idx)) {
  123. this.calculationNodeIndices.push(idx);
  124. }
  125. }
  126. }
  127. else {
  128. this.calculationNodes = this.nodes;
  129. this.calculationNodeIndices = this.nodeIndices;
  130. }
  131. };
  132. /**
  133. * this function applies the central gravity effect to keep groups from floating off
  134. *
  135. * @private
  136. */
  137. exports._calculateGravitationalForces = function () {
  138. var dx, dy, distance, node, i;
  139. var nodes = this.calculationNodes;
  140. var gravity = this.constants.physics.centralGravity;
  141. var gravityForce = 0;
  142. for (i = 0; i < this.calculationNodeIndices.length; i++) {
  143. node = nodes[this.calculationNodeIndices[i]];
  144. node.damping = this.constants.physics.damping; // possibly add function to alter damping properties of clusters.
  145. // gravity does not apply when we are in a pocket sector
  146. if (this._sector() == "default" && gravity != 0) {
  147. dx = -node.x;
  148. dy = -node.y;
  149. distance = Math.sqrt(dx * dx + dy * dy);
  150. gravityForce = (distance == 0) ? 0 : (gravity / distance);
  151. node.fx = dx * gravityForce;
  152. node.fy = dy * gravityForce;
  153. }
  154. else {
  155. node.fx = 0;
  156. node.fy = 0;
  157. }
  158. }
  159. };
  160. /**
  161. * this function calculates the effects of the springs in the case of unsmooth curves.
  162. *
  163. * @private
  164. */
  165. exports._calculateSpringForces = function () {
  166. var edgeLength, edge, edgeId;
  167. var dx, dy, fx, fy, springForce, distance;
  168. var edges = this.edges;
  169. // forces caused by the edges, modelled as springs
  170. for (edgeId in edges) {
  171. if (edges.hasOwnProperty(edgeId)) {
  172. edge = edges[edgeId];
  173. if (edge.connected === true) {
  174. // only calculate forces if nodes are in the same sector
  175. if (this.nodes.hasOwnProperty(edge.toId) && this.nodes.hasOwnProperty(edge.fromId)) {
  176. edgeLength = edge.physics.springLength;
  177. dx = (edge.from.x - edge.to.x);
  178. dy = (edge.from.y - edge.to.y);
  179. distance = Math.sqrt(dx * dx + dy * dy);
  180. if (distance == 0) {
  181. distance = 0.01;
  182. }
  183. // the 1/distance is so the fx and fy can be calculated without sine or cosine.
  184. springForce = this.constants.physics.springConstant * (edgeLength - distance) / distance;
  185. fx = dx * springForce;
  186. fy = dy * springForce;
  187. edge.from.fx += fx;
  188. edge.from.fy += fy;
  189. edge.to.fx -= fx;
  190. edge.to.fy -= fy;
  191. }
  192. }
  193. }
  194. }
  195. };
  196. /**
  197. * This function calculates the springforces on the nodes, accounting for the support nodes.
  198. *
  199. * @private
  200. */
  201. exports._calculateSpringForcesWithSupport = function () {
  202. var edgeLength, edge, edgeId;
  203. var edges = this.edges;
  204. // forces caused by the edges, modelled as springs
  205. for (edgeId in edges) {
  206. if (edges.hasOwnProperty(edgeId)) {
  207. edge = edges[edgeId];
  208. if (edge.connected === true) {
  209. // only calculate forces if nodes are in the same sector
  210. if (this.nodes.hasOwnProperty(edge.toId) && this.nodes.hasOwnProperty(edge.fromId)) {
  211. if (edge.via != null) {
  212. var node1 = edge.to;
  213. var node2 = edge.via;
  214. var node3 = edge.from;
  215. edgeLength = edge.physics.springLength;
  216. this._calculateSpringForce(node1, node2, 0.5 * edgeLength);
  217. this._calculateSpringForce(node2, node3, 0.5 * edgeLength);
  218. }
  219. }
  220. }
  221. }
  222. }
  223. };
  224. /**
  225. * This is the code actually performing the calculation for the function above. It is split out to avoid repetition.
  226. *
  227. * @param node1
  228. * @param node2
  229. * @param edgeLength
  230. * @private
  231. */
  232. exports._calculateSpringForce = function (node1, node2, edgeLength) {
  233. var dx, dy, fx, fy, springForce, distance;
  234. dx = (node1.x - node2.x);
  235. dy = (node1.y - node2.y);
  236. distance = Math.sqrt(dx * dx + dy * dy);
  237. if (distance == 0) {
  238. distance = 0.01;
  239. }
  240. // the 1/distance is so the fx and fy can be calculated without sine or cosine.
  241. springForce = this.constants.physics.springConstant * (edgeLength - distance) / distance;
  242. fx = dx * springForce;
  243. fy = dy * springForce;
  244. node1.fx += fx;
  245. node1.fy += fy;
  246. node2.fx -= fx;
  247. node2.fy -= fy;
  248. };
  249. exports._cleanupPhysicsConfiguration = function() {
  250. if (this.physicsConfiguration !== undefined) {
  251. while (this.physicsConfiguration.hasChildNodes()) {
  252. this.physicsConfiguration.removeChild(this.physicsConfiguration.firstChild);
  253. }
  254. this.physicsConfiguration.parentNode.removeChild(this.physicsConfiguration);
  255. this.physicsConfiguration = undefined;
  256. }
  257. }
  258. /**
  259. * Load the HTML for the physics config and bind it
  260. * @private
  261. */
  262. exports._loadPhysicsConfiguration = function () {
  263. if (this.physicsConfiguration === undefined) {
  264. this.backupConstants = {};
  265. util.deepExtend(this.backupConstants,this.constants);
  266. var maxGravitational = Math.max(20000, (-1 * this.constants.physics.barnesHut.gravitationalConstant) * 10);
  267. var maxSpring = Math.min(0.05, this.constants.physics.barnesHut.springConstant * 10)
  268. var hierarchicalLayoutDirections = ["LR", "RL", "UD", "DU"];
  269. this.physicsConfiguration = document.createElement('div');
  270. this.physicsConfiguration.className = "PhysicsConfiguration";
  271. this.physicsConfiguration.innerHTML = '' +
  272. '<table><tr><td><b>Simulation Mode:</b></td></tr>' +
  273. '<tr>' +
  274. '<td width="120px"><input type="radio" name="graph_physicsMethod" id="graph_physicsMethod1" value="BH" checked="checked">Barnes Hut</td>' +
  275. '<td width="120px"><input type="radio" name="graph_physicsMethod" id="graph_physicsMethod2" value="R">Repulsion</td>' +
  276. '<td width="120px"><input type="radio" name="graph_physicsMethod" id="graph_physicsMethod3" value="H">Hierarchical</td>' +
  277. '</tr>' +
  278. '</table>' +
  279. '<table id="graph_BH_table" style="display:none">' +
  280. '<tr><td><b>Barnes Hut</b></td></tr>' +
  281. '<tr>' +
  282. '<td width="150px">gravitationalConstant</td><td>0</td><td><input type="range" min="0" max="'+maxGravitational+'" value="' + (-1 * this.constants.physics.barnesHut.gravitationalConstant) + '" step="25" style="width:300px" id="graph_BH_gc"></td><td width="50px">-'+maxGravitational+'</td><td><input value="' + (this.constants.physics.barnesHut.gravitationalConstant) + '" id="graph_BH_gc_value" style="width:60px"></td>' +
  283. '</tr>' +
  284. '<tr>' +
  285. '<td width="150px">centralGravity</td><td>0</td><td><input type="range" min="0" max="6" value="' + this.constants.physics.barnesHut.centralGravity + '" step="0.05" style="width:300px" id="graph_BH_cg"></td><td>3</td><td><input value="' + this.constants.physics.barnesHut.centralGravity + '" id="graph_BH_cg_value" style="width:60px"></td>' +
  286. '</tr>' +
  287. '<tr>' +
  288. '<td width="150px">springLength</td><td>0</td><td><input type="range" min="0" max="500" value="' + this.constants.physics.barnesHut.springLength + '" step="1" style="width:300px" id="graph_BH_sl"></td><td>500</td><td><input value="' + this.constants.physics.barnesHut.springLength + '" id="graph_BH_sl_value" style="width:60px"></td>' +
  289. '</tr>' +
  290. '<tr>' +
  291. '<td width="150px">springConstant</td><td>0</td><td><input type="range" min="0" max="'+maxSpring+'" value="' + this.constants.physics.barnesHut.springConstant + '" step="0.0001" style="width:300px" id="graph_BH_sc"></td><td>'+maxSpring+'</td><td><input value="' + this.constants.physics.barnesHut.springConstant + '" id="graph_BH_sc_value" style="width:60px"></td>' +
  292. '</tr>' +
  293. '<tr>' +
  294. '<td width="150px">damping</td><td>0</td><td><input type="range" min="0" max="0.3" value="' + this.constants.physics.barnesHut.damping + '" step="0.005" style="width:300px" id="graph_BH_damp"></td><td>0.3</td><td><input value="' + this.constants.physics.barnesHut.damping + '" id="graph_BH_damp_value" style="width:60px"></td>' +
  295. '</tr>' +
  296. '</table>' +
  297. '<table id="graph_R_table" style="display:none">' +
  298. '<tr><td><b>Repulsion</b></td></tr>' +
  299. '<tr>' +
  300. '<td width="150px">nodeDistance</td><td>0</td><td><input type="range" min="0" max="300" value="' + this.constants.physics.repulsion.nodeDistance + '" step="1" style="width:300px" id="graph_R_nd"></td><td width="50px">300</td><td><input value="' + this.constants.physics.repulsion.nodeDistance + '" id="graph_R_nd_value" style="width:60px"></td>' +
  301. '</tr>' +
  302. '<tr>' +
  303. '<td width="150px">centralGravity</td><td>0</td><td><input type="range" min="0" max="3" value="' + this.constants.physics.repulsion.centralGravity + '" step="0.05" style="width:300px" id="graph_R_cg"></td><td>3</td><td><input value="' + this.constants.physics.repulsion.centralGravity + '" id="graph_R_cg_value" style="width:60px"></td>' +
  304. '</tr>' +
  305. '<tr>' +
  306. '<td width="150px">springLength</td><td>0</td><td><input type="range" min="0" max="500" value="' + this.constants.physics.repulsion.springLength + '" step="1" style="width:300px" id="graph_R_sl"></td><td>500</td><td><input value="' + this.constants.physics.repulsion.springLength + '" id="graph_R_sl_value" style="width:60px"></td>' +
  307. '</tr>' +
  308. '<tr>' +
  309. '<td width="150px">springConstant</td><td>0</td><td><input type="range" min="0" max="0.5" value="' + this.constants.physics.repulsion.springConstant + '" step="0.001" style="width:300px" id="graph_R_sc"></td><td>0.5</td><td><input value="' + this.constants.physics.repulsion.springConstant + '" id="graph_R_sc_value" style="width:60px"></td>' +
  310. '</tr>' +
  311. '<tr>' +
  312. '<td width="150px">damping</td><td>0</td><td><input type="range" min="0" max="0.3" value="' + this.constants.physics.repulsion.damping + '" step="0.005" style="width:300px" id="graph_R_damp"></td><td>0.3</td><td><input value="' + this.constants.physics.repulsion.damping + '" id="graph_R_damp_value" style="width:60px"></td>' +
  313. '</tr>' +
  314. '</table>' +
  315. '<table id="graph_H_table" style="display:none">' +
  316. '<tr><td width="150"><b>Hierarchical</b></td></tr>' +
  317. '<tr>' +
  318. '<td width="150px">nodeDistance</td><td>0</td><td><input type="range" min="0" max="300" value="' + this.constants.physics.hierarchicalRepulsion.nodeDistance + '" step="1" style="width:300px" id="graph_H_nd"></td><td width="50px">300</td><td><input value="' + this.constants.physics.hierarchicalRepulsion.nodeDistance + '" id="graph_H_nd_value" style="width:60px"></td>' +
  319. '</tr>' +
  320. '<tr>' +
  321. '<td width="150px">centralGravity</td><td>0</td><td><input type="range" min="0" max="3" value="' + this.constants.physics.hierarchicalRepulsion.centralGravity + '" step="0.05" style="width:300px" id="graph_H_cg"></td><td>3</td><td><input value="' + this.constants.physics.hierarchicalRepulsion.centralGravity + '" id="graph_H_cg_value" style="width:60px"></td>' +
  322. '</tr>' +
  323. '<tr>' +
  324. '<td width="150px">springLength</td><td>0</td><td><input type="range" min="0" max="500" value="' + this.constants.physics.hierarchicalRepulsion.springLength + '" step="1" style="width:300px" id="graph_H_sl"></td><td>500</td><td><input value="' + this.constants.physics.hierarchicalRepulsion.springLength + '" id="graph_H_sl_value" style="width:60px"></td>' +
  325. '</tr>' +
  326. '<tr>' +
  327. '<td width="150px">springConstant</td><td>0</td><td><input type="range" min="0" max="0.5" value="' + this.constants.physics.hierarchicalRepulsion.springConstant + '" step="0.001" style="width:300px" id="graph_H_sc"></td><td>0.5</td><td><input value="' + this.constants.physics.hierarchicalRepulsion.springConstant + '" id="graph_H_sc_value" style="width:60px"></td>' +
  328. '</tr>' +
  329. '<tr>' +
  330. '<td width="150px">damping</td><td>0</td><td><input type="range" min="0" max="0.3" value="' + this.constants.physics.hierarchicalRepulsion.damping + '" step="0.005" style="width:300px" id="graph_H_damp"></td><td>0.3</td><td><input value="' + this.constants.physics.hierarchicalRepulsion.damping + '" id="graph_H_damp_value" style="width:60px"></td>' +
  331. '</tr>' +
  332. '<tr>' +
  333. '<td width="150px">direction</td><td>1</td><td><input type="range" min="0" max="3" value="' + hierarchicalLayoutDirections.indexOf(this.constants.hierarchicalLayout.direction) + '" step="1" style="width:300px" id="graph_H_direction"></td><td>4</td><td><input value="' + this.constants.hierarchicalLayout.direction + '" id="graph_H_direction_value" style="width:60px"></td>' +
  334. '</tr>' +
  335. '<tr>' +
  336. '<td width="150px">levelSeparation</td><td>1</td><td><input type="range" min="0" max="500" value="' + this.constants.hierarchicalLayout.levelSeparation + '" step="1" style="width:300px" id="graph_H_levsep"></td><td>500</td><td><input value="' + this.constants.hierarchicalLayout.levelSeparation + '" id="graph_H_levsep_value" style="width:60px"></td>' +
  337. '</tr>' +
  338. '<tr>' +
  339. '<td width="150px">nodeSpacing</td><td>1</td><td><input type="range" min="0" max="500" value="' + this.constants.hierarchicalLayout.nodeSpacing + '" step="1" style="width:300px" id="graph_H_nspac"></td><td>500</td><td><input value="' + this.constants.hierarchicalLayout.nodeSpacing + '" id="graph_H_nspac_value" style="width:60px"></td>' +
  340. '</tr>' +
  341. '</table>' +
  342. '<table><tr><td><b>Options:</b></td></tr>' +
  343. '<tr>' +
  344. '<td width="180px"><input type="button" id="graph_toggleSmooth" value="Toggle smoothCurves" style="width:150px"></td>' +
  345. '<td width="180px"><input type="button" id="graph_repositionNodes" value="Reinitialize" style="width:150px"></td>' +
  346. '<td width="180px"><input type="button" id="graph_generateOptions" value="Generate Options" style="width:150px"></td>' +
  347. '</tr>' +
  348. '</table>'
  349. this.containerElement.parentElement.insertBefore(this.physicsConfiguration, this.containerElement);
  350. this.optionsDiv = document.createElement("div");
  351. this.optionsDiv.style.fontSize = "14px";
  352. this.optionsDiv.style.fontFamily = "verdana";
  353. this.containerElement.parentElement.insertBefore(this.optionsDiv, this.containerElement);
  354. var rangeElement;
  355. rangeElement = document.getElementById('graph_BH_gc');
  356. rangeElement.onchange = showValueOfRange.bind(this, 'graph_BH_gc', -1, "physics_barnesHut_gravitationalConstant");
  357. rangeElement = document.getElementById('graph_BH_cg');
  358. rangeElement.onchange = showValueOfRange.bind(this, 'graph_BH_cg', 1, "physics_centralGravity");
  359. rangeElement = document.getElementById('graph_BH_sc');
  360. rangeElement.onchange = showValueOfRange.bind(this, 'graph_BH_sc', 1, "physics_springConstant");
  361. rangeElement = document.getElementById('graph_BH_sl');
  362. rangeElement.onchange = showValueOfRange.bind(this, 'graph_BH_sl', 1, "physics_springLength");
  363. rangeElement = document.getElementById('graph_BH_damp');
  364. rangeElement.onchange = showValueOfRange.bind(this, 'graph_BH_damp', 1, "physics_damping");
  365. rangeElement = document.getElementById('graph_R_nd');
  366. rangeElement.onchange = showValueOfRange.bind(this, 'graph_R_nd', 1, "physics_repulsion_nodeDistance");
  367. rangeElement = document.getElementById('graph_R_cg');
  368. rangeElement.onchange = showValueOfRange.bind(this, 'graph_R_cg', 1, "physics_centralGravity");
  369. rangeElement = document.getElementById('graph_R_sc');
  370. rangeElement.onchange = showValueOfRange.bind(this, 'graph_R_sc', 1, "physics_springConstant");
  371. rangeElement = document.getElementById('graph_R_sl');
  372. rangeElement.onchange = showValueOfRange.bind(this, 'graph_R_sl', 1, "physics_springLength");
  373. rangeElement = document.getElementById('graph_R_damp');
  374. rangeElement.onchange = showValueOfRange.bind(this, 'graph_R_damp', 1, "physics_damping");
  375. rangeElement = document.getElementById('graph_H_nd');
  376. rangeElement.onchange = showValueOfRange.bind(this, 'graph_H_nd', 1, "physics_hierarchicalRepulsion_nodeDistance");
  377. rangeElement = document.getElementById('graph_H_cg');
  378. rangeElement.onchange = showValueOfRange.bind(this, 'graph_H_cg', 1, "physics_centralGravity");
  379. rangeElement = document.getElementById('graph_H_sc');
  380. rangeElement.onchange = showValueOfRange.bind(this, 'graph_H_sc', 1, "physics_springConstant");
  381. rangeElement = document.getElementById('graph_H_sl');
  382. rangeElement.onchange = showValueOfRange.bind(this, 'graph_H_sl', 1, "physics_springLength");
  383. rangeElement = document.getElementById('graph_H_damp');
  384. rangeElement.onchange = showValueOfRange.bind(this, 'graph_H_damp', 1, "physics_damping");
  385. rangeElement = document.getElementById('graph_H_direction');
  386. rangeElement.onchange = showValueOfRange.bind(this, 'graph_H_direction', hierarchicalLayoutDirections, "hierarchicalLayout_direction");
  387. rangeElement = document.getElementById('graph_H_levsep');
  388. rangeElement.onchange = showValueOfRange.bind(this, 'graph_H_levsep', 1, "hierarchicalLayout_levelSeparation");
  389. rangeElement = document.getElementById('graph_H_nspac');
  390. rangeElement.onchange = showValueOfRange.bind(this, 'graph_H_nspac', 1, "hierarchicalLayout_nodeSpacing");
  391. var radioButton1 = document.getElementById("graph_physicsMethod1");
  392. var radioButton2 = document.getElementById("graph_physicsMethod2");
  393. var radioButton3 = document.getElementById("graph_physicsMethod3");
  394. radioButton2.checked = true;
  395. if (this.constants.physics.barnesHut.enabled) {
  396. radioButton1.checked = true;
  397. }
  398. if (this.constants.hierarchicalLayout.enabled) {
  399. radioButton3.checked = true;
  400. }
  401. var graph_toggleSmooth = document.getElementById("graph_toggleSmooth");
  402. var graph_repositionNodes = document.getElementById("graph_repositionNodes");
  403. var graph_generateOptions = document.getElementById("graph_generateOptions");
  404. graph_toggleSmooth.onclick = graphToggleSmoothCurves.bind(this);
  405. graph_repositionNodes.onclick = graphRepositionNodes.bind(this);
  406. graph_generateOptions.onclick = graphGenerateOptions.bind(this);
  407. if (this.constants.smoothCurves == true && this.constants.dynamicSmoothCurves == false) {
  408. graph_toggleSmooth.style.background = "#A4FF56";
  409. }
  410. else {
  411. graph_toggleSmooth.style.background = "#FF8532";
  412. }
  413. switchConfigurations.apply(this);
  414. radioButton1.onchange = switchConfigurations.bind(this);
  415. radioButton2.onchange = switchConfigurations.bind(this);
  416. radioButton3.onchange = switchConfigurations.bind(this);
  417. }
  418. };
  419. /**
  420. * This overwrites the this.constants.
  421. *
  422. * @param constantsVariableName
  423. * @param value
  424. * @private
  425. */
  426. exports._overWriteGraphConstants = function (constantsVariableName, value) {
  427. var nameArray = constantsVariableName.split("_");
  428. if (nameArray.length == 1) {
  429. this.constants[nameArray[0]] = value;
  430. }
  431. else if (nameArray.length == 2) {
  432. this.constants[nameArray[0]][nameArray[1]] = value;
  433. }
  434. else if (nameArray.length == 3) {
  435. this.constants[nameArray[0]][nameArray[1]][nameArray[2]] = value;
  436. }
  437. };
  438. /**
  439. * this function is bound to the toggle smooth curves button. That is also why it is not in the prototype.
  440. */
  441. function graphToggleSmoothCurves () {
  442. this.constants.smoothCurves.enabled = !this.constants.smoothCurves.enabled;
  443. var graph_toggleSmooth = document.getElementById("graph_toggleSmooth");
  444. if (this.constants.smoothCurves.enabled == true) {graph_toggleSmooth.style.background = "#A4FF56";}
  445. else {graph_toggleSmooth.style.background = "#FF8532";}
  446. this._configureSmoothCurves(false);
  447. }
  448. /**
  449. * this function is used to scramble the nodes
  450. *
  451. */
  452. function graphRepositionNodes () {
  453. for (var nodeId in this.calculationNodes) {
  454. if (this.calculationNodes.hasOwnProperty(nodeId)) {
  455. this.calculationNodes[nodeId].vx = 0; this.calculationNodes[nodeId].vy = 0;
  456. this.calculationNodes[nodeId].fx = 0; this.calculationNodes[nodeId].fy = 0;
  457. }
  458. }
  459. if (this.constants.hierarchicalLayout.enabled == true) {
  460. this._setupHierarchicalLayout();
  461. showValueOfRange.call(this, 'graph_H_nd', 1, "physics_hierarchicalRepulsion_nodeDistance");
  462. showValueOfRange.call(this, 'graph_H_cg', 1, "physics_centralGravity");
  463. showValueOfRange.call(this, 'graph_H_sc', 1, "physics_springConstant");
  464. showValueOfRange.call(this, 'graph_H_sl', 1, "physics_springLength");
  465. showValueOfRange.call(this, 'graph_H_damp', 1, "physics_damping");
  466. }
  467. else {
  468. this.repositionNodes();
  469. }
  470. this.moving = true;
  471. this.start();
  472. }
  473. /**
  474. * this is used to generate an options file from the playing with physics system.
  475. */
  476. function graphGenerateOptions () {
  477. var options = "No options are required, default values used.";
  478. var optionsSpecific = [];
  479. var radioButton1 = document.getElementById("graph_physicsMethod1");
  480. var radioButton2 = document.getElementById("graph_physicsMethod2");
  481. if (radioButton1.checked == true) {
  482. if (this.constants.physics.barnesHut.gravitationalConstant != this.backupConstants.physics.barnesHut.gravitationalConstant) {optionsSpecific.push("gravitationalConstant: " + this.constants.physics.barnesHut.gravitationalConstant);}
  483. if (this.constants.physics.centralGravity != this.backupConstants.physics.barnesHut.centralGravity) {optionsSpecific.push("centralGravity: " + this.constants.physics.centralGravity);}
  484. if (this.constants.physics.springLength != this.backupConstants.physics.barnesHut.springLength) {optionsSpecific.push("springLength: " + this.constants.physics.springLength);}
  485. if (this.constants.physics.springConstant != this.backupConstants.physics.barnesHut.springConstant) {optionsSpecific.push("springConstant: " + this.constants.physics.springConstant);}
  486. if (this.constants.physics.damping != this.backupConstants.physics.barnesHut.damping) {optionsSpecific.push("damping: " + this.constants.physics.damping);}
  487. if (optionsSpecific.length != 0) {
  488. options = "var options = {";
  489. options += "physics: {barnesHut: {";
  490. for (var i = 0; i < optionsSpecific.length; i++) {
  491. options += optionsSpecific[i];
  492. if (i < optionsSpecific.length - 1) {
  493. options += ", "
  494. }
  495. }
  496. options += '}}'
  497. }
  498. if (this.constants.smoothCurves.enabled != this.backupConstants.smoothCurves.enabled) {
  499. if (optionsSpecific.length == 0) {options = "var options = {";}
  500. else {options += ", "}
  501. options += "smoothCurves: " + this.constants.smoothCurves.enabled;
  502. }
  503. if (options != "No options are required, default values used.") {
  504. options += '};'
  505. }
  506. }
  507. else if (radioButton2.checked == true) {
  508. options = "var options = {";
  509. options += "physics: {barnesHut: {enabled: false}";
  510. if (this.constants.physics.repulsion.nodeDistance != this.backupConstants.physics.repulsion.nodeDistance) {optionsSpecific.push("nodeDistance: " + this.constants.physics.repulsion.nodeDistance);}
  511. if (this.constants.physics.centralGravity != this.backupConstants.physics.repulsion.centralGravity) {optionsSpecific.push("centralGravity: " + this.constants.physics.centralGravity);}
  512. if (this.constants.physics.springLength != this.backupConstants.physics.repulsion.springLength) {optionsSpecific.push("springLength: " + this.constants.physics.springLength);}
  513. if (this.constants.physics.springConstant != this.backupConstants.physics.repulsion.springConstant) {optionsSpecific.push("springConstant: " + this.constants.physics.springConstant);}
  514. if (this.constants.physics.damping != this.backupConstants.physics.repulsion.damping) {optionsSpecific.push("damping: " + this.constants.physics.damping);}
  515. if (optionsSpecific.length != 0) {
  516. options += ", repulsion: {";
  517. for (var i = 0; i < optionsSpecific.length; i++) {
  518. options += optionsSpecific[i];
  519. if (i < optionsSpecific.length - 1) {
  520. options += ", "
  521. }
  522. }
  523. options += '}}'
  524. }
  525. if (optionsSpecific.length == 0) {options += "}"}
  526. if (this.constants.smoothCurves != this.backupConstants.smoothCurves) {
  527. options += ", smoothCurves: " + this.constants.smoothCurves;
  528. }
  529. options += '};'
  530. }
  531. else {
  532. options = "var options = {";
  533. if (this.constants.physics.hierarchicalRepulsion.nodeDistance != this.backupConstants.physics.hierarchicalRepulsion.nodeDistance) {optionsSpecific.push("nodeDistance: " + this.constants.physics.hierarchicalRepulsion.nodeDistance);}
  534. if (this.constants.physics.centralGravity != this.backupConstants.physics.hierarchicalRepulsion.centralGravity) {optionsSpecific.push("centralGravity: " + this.constants.physics.centralGravity);}
  535. if (this.constants.physics.springLength != this.backupConstants.physics.hierarchicalRepulsion.springLength) {optionsSpecific.push("springLength: " + this.constants.physics.springLength);}
  536. if (this.constants.physics.springConstant != this.backupConstants.physics.hierarchicalRepulsion.springConstant) {optionsSpecific.push("springConstant: " + this.constants.physics.springConstant);}
  537. if (this.constants.physics.damping != this.backupConstants.physics.hierarchicalRepulsion.damping) {optionsSpecific.push("damping: " + this.constants.physics.damping);}
  538. if (optionsSpecific.length != 0) {
  539. options += "physics: {hierarchicalRepulsion: {";
  540. for (var i = 0; i < optionsSpecific.length; i++) {
  541. options += optionsSpecific[i];
  542. if (i < optionsSpecific.length - 1) {
  543. options += ", ";
  544. }
  545. }
  546. options += '}},';
  547. }
  548. options += 'hierarchicalLayout: {';
  549. optionsSpecific = [];
  550. if (this.constants.hierarchicalLayout.direction != this.backupConstants.hierarchicalLayout.direction) {optionsSpecific.push("direction: " + this.constants.hierarchicalLayout.direction);}
  551. if (Math.abs(this.constants.hierarchicalLayout.levelSeparation) != this.backupConstants.hierarchicalLayout.levelSeparation) {optionsSpecific.push("levelSeparation: " + this.constants.hierarchicalLayout.levelSeparation);}
  552. if (this.constants.hierarchicalLayout.nodeSpacing != this.backupConstants.hierarchicalLayout.nodeSpacing) {optionsSpecific.push("nodeSpacing: " + this.constants.hierarchicalLayout.nodeSpacing);}
  553. if (optionsSpecific.length != 0) {
  554. for (var i = 0; i < optionsSpecific.length; i++) {
  555. options += optionsSpecific[i];
  556. if (i < optionsSpecific.length - 1) {
  557. options += ", "
  558. }
  559. }
  560. options += '}'
  561. }
  562. else {
  563. options += "enabled:true}";
  564. }
  565. options += '};'
  566. }
  567. this.optionsDiv.innerHTML = options;
  568. }
  569. /**
  570. * this is used to switch between barnesHut, repulsion and hierarchical.
  571. *
  572. */
  573. function switchConfigurations () {
  574. var ids = ["graph_BH_table", "graph_R_table", "graph_H_table"];
  575. var radioButton = document.querySelector('input[name="graph_physicsMethod"]:checked').value;
  576. var tableId = "graph_" + radioButton + "_table";
  577. var table = document.getElementById(tableId);
  578. table.style.display = "block";
  579. for (var i = 0; i < ids.length; i++) {
  580. if (ids[i] != tableId) {
  581. table = document.getElementById(ids[i]);
  582. table.style.display = "none";
  583. }
  584. }
  585. this._restoreNodes();
  586. if (radioButton == "R") {
  587. this.constants.hierarchicalLayout.enabled = false;
  588. this.constants.physics.hierarchicalRepulsion.enabled = false;
  589. this.constants.physics.barnesHut.enabled = false;
  590. }
  591. else if (radioButton == "H") {
  592. if (this.constants.hierarchicalLayout.enabled == false) {
  593. this.constants.hierarchicalLayout.enabled = true;
  594. this.constants.physics.hierarchicalRepulsion.enabled = true;
  595. this.constants.physics.barnesHut.enabled = false;
  596. this.constants.smoothCurves.enabled = false;
  597. this._setupHierarchicalLayout();
  598. }
  599. }
  600. else {
  601. this.constants.hierarchicalLayout.enabled = false;
  602. this.constants.physics.hierarchicalRepulsion.enabled = false;
  603. this.constants.physics.barnesHut.enabled = true;
  604. }
  605. this._loadSelectedForceSolver();
  606. var graph_toggleSmooth = document.getElementById("graph_toggleSmooth");
  607. if (this.constants.smoothCurves.enabled == true) {graph_toggleSmooth.style.background = "#A4FF56";}
  608. else {graph_toggleSmooth.style.background = "#FF8532";}
  609. this.moving = true;
  610. this.start();
  611. }
  612. /**
  613. * this generates the ranges depending on the iniital values.
  614. *
  615. * @param id
  616. * @param map
  617. * @param constantsVariableName
  618. */
  619. function showValueOfRange (id,map,constantsVariableName) {
  620. var valueId = id + "_value";
  621. var rangeValue = document.getElementById(id).value;
  622. if (Array.isArray(map)) {
  623. document.getElementById(valueId).value = map[parseInt(rangeValue)];
  624. this._overWriteGraphConstants(constantsVariableName,map[parseInt(rangeValue)]);
  625. }
  626. else {
  627. document.getElementById(valueId).value = parseInt(map) * parseFloat(rangeValue);
  628. this._overWriteGraphConstants(constantsVariableName, parseInt(map) * parseFloat(rangeValue));
  629. }
  630. if (constantsVariableName == "hierarchicalLayout_direction" ||
  631. constantsVariableName == "hierarchicalLayout_levelSeparation" ||
  632. constantsVariableName == "hierarchicalLayout_nodeSpacing") {
  633. this._setupHierarchicalLayout();
  634. }
  635. this.moving = true;
  636. this.start();
  637. }