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.

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