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.

706 lines
34 KiB

10 years ago
  1. /**
  2. * Created by Alex on 2/6/14.
  3. */
  4. var physicsMixin = {
  5. /**
  6. * Toggling barnes Hut calculation on and off.
  7. *
  8. * @private
  9. */
  10. _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. _loadSelectedForceSolver: function () {
  22. // this overloads the this._calculateNodeForces
  23. if (this.constants.physics.barnesHut.enabled == true) {
  24. this._clearMixin(repulsionMixin);
  25. this._clearMixin(hierarchalRepulsionMixin);
  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(hierarchalRepulsionMixin);
  40. }
  41. else {
  42. this._clearMixin(barnesHutMixin);
  43. this._clearMixin(hierarchalRepulsionMixin);
  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. _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. _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. _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. _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. _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. _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. _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. _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. _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. /**
  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 = !this.constants.smoothCurves;
  440. var graph_toggleSmooth = document.getElementById("graph_toggleSmooth");
  441. if (this.constants.smoothCurves == 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. }
  459. else {
  460. this.repositionNodes();
  461. }
  462. this.moving = true;
  463. this.start();
  464. };
  465. /**
  466. * this is used to generate an options file from the playing with physics system.
  467. */
  468. function graphGenerateOptions () {
  469. var options = "No options are required, default values used.";
  470. var optionsSpecific = [];
  471. var radioButton1 = document.getElementById("graph_physicsMethod1");
  472. var radioButton2 = document.getElementById("graph_physicsMethod2");
  473. if (radioButton1.checked == true) {
  474. if (this.constants.physics.barnesHut.gravitationalConstant != this.backupConstants.physics.barnesHut.gravitationalConstant) {optionsSpecific.push("gravitationalConstant: " + this.constants.physics.barnesHut.gravitationalConstant);}
  475. if (this.constants.physics.centralGravity != this.backupConstants.physics.barnesHut.centralGravity) {optionsSpecific.push("centralGravity: " + this.constants.physics.centralGravity);}
  476. if (this.constants.physics.springLength != this.backupConstants.physics.barnesHut.springLength) {optionsSpecific.push("springLength: " + this.constants.physics.springLength);}
  477. if (this.constants.physics.springConstant != this.backupConstants.physics.barnesHut.springConstant) {optionsSpecific.push("springConstant: " + this.constants.physics.springConstant);}
  478. if (this.constants.physics.damping != this.backupConstants.physics.barnesHut.damping) {optionsSpecific.push("damping: " + this.constants.physics.damping);}
  479. if (optionsSpecific.length != 0) {
  480. options = "var options = {";
  481. options += "physics: {barnesHut: {";
  482. for (var i = 0; i < optionsSpecific.length; i++) {
  483. options += optionsSpecific[i];
  484. if (i < optionsSpecific.length - 1) {
  485. options += ", "
  486. }
  487. }
  488. options += '}}'
  489. }
  490. if (this.constants.smoothCurves != this.backupConstants.smoothCurves) {
  491. if (optionsSpecific.length == 0) {options = "var options = {";}
  492. else {options += ", "}
  493. options += "smoothCurves: " + this.constants.smoothCurves;
  494. }
  495. if (options != "No options are required, default values used.") {
  496. options += '};'
  497. }
  498. }
  499. else if (radioButton2.checked == true) {
  500. options = "var options = {";
  501. options += "physics: {barnesHut: {enabled: false}";
  502. if (this.constants.physics.repulsion.nodeDistance != this.backupConstants.physics.repulsion.nodeDistance) {optionsSpecific.push("nodeDistance: " + this.constants.physics.repulsion.nodeDistance);}
  503. if (this.constants.physics.centralGravity != this.backupConstants.physics.repulsion.centralGravity) {optionsSpecific.push("centralGravity: " + this.constants.physics.centralGravity);}
  504. if (this.constants.physics.springLength != this.backupConstants.physics.repulsion.springLength) {optionsSpecific.push("springLength: " + this.constants.physics.springLength);}
  505. if (this.constants.physics.springConstant != this.backupConstants.physics.repulsion.springConstant) {optionsSpecific.push("springConstant: " + this.constants.physics.springConstant);}
  506. if (this.constants.physics.damping != this.backupConstants.physics.repulsion.damping) {optionsSpecific.push("damping: " + this.constants.physics.damping);}
  507. if (optionsSpecific.length != 0) {
  508. options += ", repulsion: {";
  509. for (var i = 0; i < optionsSpecific.length; i++) {
  510. options += optionsSpecific[i];
  511. if (i < optionsSpecific.length - 1) {
  512. options += ", "
  513. }
  514. }
  515. options += '}}'
  516. }
  517. if (optionsSpecific.length == 0) {options += "}"}
  518. if (this.constants.smoothCurves != this.backupConstants.smoothCurves) {
  519. options += ", smoothCurves: " + this.constants.smoothCurves;
  520. }
  521. options += '};'
  522. }
  523. else {
  524. options = "var options = {";
  525. if (this.constants.physics.hierarchicalRepulsion.nodeDistance != this.backupConstants.physics.hierarchicalRepulsion.nodeDistance) {optionsSpecific.push("nodeDistance: " + this.constants.physics.hierarchicalRepulsion.nodeDistance);}
  526. if (this.constants.physics.centralGravity != this.backupConstants.physics.hierarchicalRepulsion.centralGravity) {optionsSpecific.push("centralGravity: " + this.constants.physics.centralGravity);}
  527. if (this.constants.physics.springLength != this.backupConstants.physics.hierarchicalRepulsion.springLength) {optionsSpecific.push("springLength: " + this.constants.physics.springLength);}
  528. if (this.constants.physics.springConstant != this.backupConstants.physics.hierarchicalRepulsion.springConstant) {optionsSpecific.push("springConstant: " + this.constants.physics.springConstant);}
  529. if (this.constants.physics.damping != this.backupConstants.physics.hierarchicalRepulsion.damping) {optionsSpecific.push("damping: " + this.constants.physics.damping);}
  530. if (optionsSpecific.length != 0) {
  531. options += "physics: {hierarchicalRepulsion: {";
  532. for (var i = 0; i < optionsSpecific.length; i++) {
  533. options += optionsSpecific[i];
  534. if (i < optionsSpecific.length - 1) {
  535. options += ", ";
  536. }
  537. }
  538. options += '}},';
  539. }
  540. options += 'hierarchicalLayout: {';
  541. optionsSpecific = [];
  542. if (this.constants.hierarchicalLayout.direction != this.backupConstants.hierarchicalLayout.direction) {optionsSpecific.push("direction: " + this.constants.hierarchicalLayout.direction);}
  543. if (Math.abs(this.constants.hierarchicalLayout.levelSeparation) != this.backupConstants.hierarchicalLayout.levelSeparation) {optionsSpecific.push("levelSeparation: " + this.constants.hierarchicalLayout.levelSeparation);}
  544. if (this.constants.hierarchicalLayout.nodeSpacing != this.backupConstants.hierarchicalLayout.nodeSpacing) {optionsSpecific.push("nodeSpacing: " + this.constants.hierarchicalLayout.nodeSpacing);}
  545. if (optionsSpecific.length != 0) {
  546. for (var i = 0; i < optionsSpecific.length; i++) {
  547. options += optionsSpecific[i];
  548. if (i < optionsSpecific.length - 1) {
  549. options += ", "
  550. }
  551. }
  552. options += '}'
  553. }
  554. else {
  555. options += "enabled:true}";
  556. }
  557. options += '};'
  558. }
  559. this.optionsDiv.innerHTML = options;
  560. };
  561. /**
  562. * this is used to switch between barnesHut, repulsion and hierarchical.
  563. *
  564. */
  565. function switchConfigurations () {
  566. var ids = ["graph_BH_table", "graph_R_table", "graph_H_table"];
  567. var radioButton = document.querySelector('input[name="graph_physicsMethod"]:checked').value;
  568. var tableId = "graph_" + radioButton + "_table";
  569. var table = document.getElementById(tableId);
  570. table.style.display = "block";
  571. for (var i = 0; i < ids.length; i++) {
  572. if (ids[i] != tableId) {
  573. table = document.getElementById(ids[i]);
  574. table.style.display = "none";
  575. }
  576. }
  577. this._restoreNodes();
  578. if (radioButton == "R") {
  579. this.constants.hierarchicalLayout.enabled = false;
  580. this.constants.physics.hierarchicalRepulsion.enabled = false;
  581. this.constants.physics.barnesHut.enabled = false;
  582. }
  583. else if (radioButton == "H") {
  584. if (this.constants.hierarchicalLayout.enabled == false) {
  585. this.constants.hierarchicalLayout.enabled = true;
  586. this.constants.physics.hierarchicalRepulsion.enabled = true;
  587. this.constants.physics.barnesHut.enabled = false;
  588. this._setupHierarchicalLayout();
  589. }
  590. }
  591. else {
  592. this.constants.hierarchicalLayout.enabled = false;
  593. this.constants.physics.hierarchicalRepulsion.enabled = false;
  594. this.constants.physics.barnesHut.enabled = true;
  595. }
  596. this._loadSelectedForceSolver();
  597. var graph_toggleSmooth = document.getElementById("graph_toggleSmooth");
  598. if (this.constants.smoothCurves == true) {graph_toggleSmooth.style.background = "#A4FF56";}
  599. else {graph_toggleSmooth.style.background = "#FF8532";}
  600. this.moving = true;
  601. this.start();
  602. }
  603. /**
  604. * this generates the ranges depending on the iniital values.
  605. *
  606. * @param id
  607. * @param map
  608. * @param constantsVariableName
  609. */
  610. function showValueOfRange (id,map,constantsVariableName) {
  611. var valueId = id + "_value";
  612. var rangeValue = document.getElementById(id).value;
  613. if (map instanceof Array) {
  614. document.getElementById(valueId).value = map[parseInt(rangeValue)];
  615. this._overWriteGraphConstants(constantsVariableName,map[parseInt(rangeValue)]);
  616. }
  617. else {
  618. document.getElementById(valueId).value = parseInt(map) * parseFloat(rangeValue);
  619. this._overWriteGraphConstants(constantsVariableName, parseInt(map) * parseFloat(rangeValue));
  620. }
  621. if (constantsVariableName == "hierarchicalLayout_direction" ||
  622. constantsVariableName == "hierarchicalLayout_levelSeparation" ||
  623. constantsVariableName == "hierarchicalLayout_nodeSpacing") {
  624. this._setupHierarchicalLayout();
  625. }
  626. this.moving = true;
  627. this.start();
  628. };