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.

104 lines
2.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. </style>
  10. <script type="text/javascript" src="../../vis.js"></script>
  11. <script type="text/javascript">
  12. var nodes = null;
  13. var edges = null;
  14. var graph = null;
  15. function draw() {
  16. nodes = [];
  17. edges = [];
  18. var connectionCount = [];
  19. // randomly create some nodes and edges
  20. var nodeCount = document.getElementById('nodeCount').value;
  21. for (var i = 0; i < nodeCount; i++) {
  22. nodes.push({
  23. 'id': i,
  24. 'text': String(i)
  25. });
  26. connectionCount[i] = 0;
  27. // create edges in a scale-free-graph way
  28. if (i == 1) {
  29. var from = i;
  30. var to = 0;
  31. edges.push({
  32. 'from': from,
  33. 'to': to
  34. });
  35. connectionCount[from]++;
  36. connectionCount[to]++;
  37. }
  38. else if (i > 1) {
  39. var conn = edges.length * 2;
  40. var rand = Math.floor(Math.random() * conn);
  41. var cum = 0;
  42. var j = 0;
  43. while (j < connectionCount.length && cum < rand) {
  44. cum += connectionCount[j];
  45. j++;
  46. }
  47. var from = i;
  48. var to = j;
  49. edges.push({
  50. 'from': from,
  51. 'to': to
  52. });
  53. connectionCount[from]++;
  54. connectionCount[to]++;
  55. }
  56. }
  57. // specify options
  58. var options = {
  59. width: '600px',
  60. height: '600px',
  61. edges: {
  62. length: 50
  63. },
  64. stabilize: false
  65. };
  66. // create a graph
  67. graph = new vis.Graph(document.getElementById('mygraph'));
  68. // draw data
  69. graph.draw(nodes, edges, options);
  70. // add event listeners
  71. vis.events.addListener(graph, 'select', function(params) {
  72. document.getElementById('selection').innerHTML =
  73. 'Selection: ' + JSON.stringify(graph.getSelection());
  74. });
  75. }
  76. </script>
  77. </head>
  78. <body onload="draw();">
  79. <form onsubmit="draw(); return false;">
  80. <label for="nodeCount">Number of nodes:</label>
  81. <input id="nodeCount" type="text" value="25" style="width: 50px;">
  82. <input type="submit" value="Go">
  83. </form>
  84. <br>
  85. <div id="mygraph"></div>
  86. <p id="selection"></p>
  87. </body>
  88. </html>