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.

179 lines
5.8 KiB

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Graph | Random nodes</title>
  5. <style type="text/css">
  6. body {
  7. font: 10pt sans;
  8. }
  9. #mygraph {
  10. width: 600px;
  11. height: 600px;
  12. border: 1px solid lightgray;
  13. }
  14. </style>
  15. <script type="text/javascript" src="../../dist/vis.js"></script>
  16. <script type="text/javascript">
  17. var nodes = null;
  18. var edges = null;
  19. var graph = null;
  20. function draw() {
  21. nodes = [];
  22. edges = [];
  23. var connectionCount = [];
  24. // randomly create some nodes and edges
  25. var nodeCount = document.getElementById('nodeCount').value;
  26. for (var i = 0; i < nodeCount; i++) {
  27. nodes.push({
  28. id: i,
  29. label: String(i)
  30. });
  31. connectionCount[i] = 0;
  32. // create edges in a scale-free-graph way
  33. if (i == 1) {
  34. var from = i;
  35. var to = 0;
  36. edges.push({
  37. from: from,
  38. to: to
  39. });
  40. connectionCount[from]++;
  41. connectionCount[to]++;
  42. }
  43. else if (i > 1) {
  44. var conn = edges.length * 2;
  45. var rand = Math.floor(Math.random() * conn);
  46. var cum = 0;
  47. var j = 0;
  48. while (j < connectionCount.length && cum < rand) {
  49. cum += connectionCount[j];
  50. j++;
  51. }
  52. var from = i;
  53. var to = j;
  54. edges.push({
  55. from: from,
  56. to: to
  57. });
  58. connectionCount[from]++;
  59. connectionCount[to]++;
  60. }
  61. }
  62. // create a graph
  63. var container = document.getElementById('mygraph');
  64. var data = {
  65. nodes: nodes,
  66. edges: edges
  67. };
  68. /*
  69. var options = {
  70. nodes: {
  71. shape: 'circle'
  72. },
  73. edges: {
  74. length: 50
  75. },
  76. stabilize: false
  77. };
  78. */
  79. var options = {
  80. edges: {
  81. },
  82. stabilize: false
  83. };
  84. graph = new vis.Graph(container, data, options);
  85. // add event listeners
  86. graph.on('select', function(params) {
  87. document.getElementById('selection').innerHTML = 'Selection: ' + params.nodes;
  88. });
  89. }
  90. </script>
  91. </head>
  92. <body onload="draw();">
  93. <form onsubmit="draw(); return false;">
  94. <label for="nodeCount">Number of nodes:</label>
  95. <input id="nodeCount" type="text" value="25" style="width: 50px;">
  96. <input type="submit" value="Go">
  97. </form>
  98. <input type="radio" name="physicsMethod" value="Barnes Hut" checked="checked">Barnes Hut <br />
  99. <input type="radio" name="physicsMethod" value="Repulsion">Repulsion <br />
  100. <input type="radio" name="physicsMethod" value="Hierarchical Repulsion">Hierarchical Repulsion <br />
  101. <table class="graphPhysicsTable">
  102. <tr>
  103. <td><b>Barnes Hut</b></td>
  104. </tr>
  105. <tr>
  106. <td>gravitationalConstant</td><td>-500</td><td><input type="range" min="500" max="20000" value="2000" step="50" style="width:300px"></td><td>-20000</td><td><input value="2000"></td>
  107. </tr>
  108. <tr>
  109. <td>centralGravity</td><td>0.0</td><td><input type="range" min="0" max="3" value="0.3" step="0.1" style="width:300px"></td><td>3</td><td><input value="0.03"></td>
  110. </tr>
  111. <tr>
  112. <td>springLength</td><td>0</td><td><input type="range" min="0" max="500" value="100" step="1" style="width:300px"></td><td>500</td><td><input value="100"></td>
  113. </tr>
  114. <tr>
  115. <td>springConstant</td><td>0</td><td><input type="range" min="0" max="0.5" value="0.05" step="0.01" style="width:300px"></td><td>0.5</td><td><input value="0.05"></td>
  116. </tr>
  117. <tr>
  118. <td>damping</td><td>0</td><td><input type="range" min="0" max="0.3" value="0.09" step="0.01" style="width:300px"></td><td>0.3</td><td><input value="0.09"></td>
  119. </tr>
  120. </table>
  121. <table class="graphPhysicsTable">
  122. <tr>
  123. <td><b>Repulsion</b></td>
  124. </tr>
  125. <tr>
  126. <td>nodeDistance</td><td>0</td><td><input type="range" min="0" max="300" value="100" step="50" style="width:300px"></td><td>300</td><td><input value="100"></td>
  127. </tr>
  128. <tr>
  129. <td>centralGravity</td><td>0.0</td><td><input type="range" min="0" max="3" value="0.1" step="0.1" style="width:300px"></td><td>3</td><td><input value="0.01"></td>
  130. </tr>
  131. <tr>
  132. <td>springLength</td><td>0</td><td><input type="range" min="0" max="500" value="200" step="1" style="width:300px"></td><td>500</td><td><input value="200"></td>
  133. </tr>
  134. <tr>
  135. <td>springConstant</td><td>0</td><td><input type="range" min="0" max="0.5" value="0.05" step="0.01" style="width:300px"></td><td>0.5</td><td><input value="0.05"></td>
  136. </tr>
  137. <tr>
  138. <td>damping</td><td>0</td><td><input type="range" min="0" max="0.3" value="0.09" step="0.01" style="width:300px"></td><td>0.3</td><td><input value="0.09"></td>
  139. </tr>
  140. </table>
  141. <table class="graphPhysicsTable">
  142. <tr>
  143. <td><b>Hierarchical Repulsion</b></td>
  144. </tr>
  145. <tr>
  146. <td>nodeDistance</td><td>0</td><td><input type="range" min="0" max="300" value="60" step="50" style="width:300px"></td><td>300</td><td><input value="60"></td>
  147. </tr>
  148. <tr>
  149. <td>centralGravity</td><td>0.0</td><td><input type="range" min="0" max="3" value="0" step="0.1" style="width:300px"></td><td>3</td><td><input value="0"></td>
  150. </tr>
  151. <tr>
  152. <td>springLength</td><td>0</td><td><input type="range" min="0" max="500" value="100" step="1" style="width:300px"></td><td>500</td><td><input value="100"></td>
  153. </tr>
  154. <tr>
  155. <td>springConstant</td><td>0</td><td><input type="range" min="0" max="0.5" value="0.01" step="0.01" style="width:300px"></td><td>0.5</td><td><input value="0.01"></td>
  156. </tr>
  157. <tr>
  158. <td>damping</td><td>0</td><td><input type="range" min="0" max="0.3" value="0.09" step="0.01" style="width:300px"></td><td>0.3</td><td><input value="0.09"></td>
  159. </tr>
  160. </table>
  161. <br>
  162. <div id="mygraph"></div>
  163. <p id="selection"></p>
  164. </body>
  165. </html>