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.

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