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.9 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. label: 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. // create a graph
  58. var container = document.getElementById('mygraph');
  59. var data = {
  60. nodes: nodes,
  61. edges: edges
  62. };
  63. var options = {
  64. width: '600px',
  65. height: '600px',
  66. edges: {
  67. length: 50
  68. },
  69. stabilize: false
  70. };
  71. graph = new vis.Graph(container, data, options);
  72. // add event listeners
  73. vis.events.addListener(graph, 'select', function(params) {
  74. document.getElementById('selection').innerHTML =
  75. 'Selection: ' + JSON.stringify(graph.getSelection());
  76. });
  77. }
  78. </script>
  79. </head>
  80. <body onload="draw();">
  81. <form onsubmit="draw(); return false;">
  82. <label for="nodeCount">Number of nodes:</label>
  83. <input id="nodeCount" type="text" value="25" style="width: 50px;">
  84. <input type="submit" value="Go">
  85. </form>
  86. <br>
  87. <div id="mygraph"></div>
  88. <p id="selection"></p>
  89. </body>
  90. </html>