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.

110 lines
2.4 KiB

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Network | Random nodes</title>
  5. <style type="text/css">
  6. body {
  7. font: 10pt sans;
  8. }
  9. #mynetwork {
  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. <link href="../../dist/vis.css" rel="stylesheet" type="text/css" />
  17. <script type="text/javascript">
  18. var nodes = null;
  19. var edges = null;
  20. var network = null;
  21. function destroy() {
  22. if (network !== null) {
  23. network.destroy();
  24. network = null;
  25. }
  26. }
  27. function draw() {
  28. destroy();
  29. nodes = [];
  30. edges = [];
  31. var connectionCount = [];
  32. // randomly create some nodes and edges
  33. var nodeCount = document.getElementById('nodeCount').value;
  34. for (var i = 0; i < nodeCount; i++) {
  35. nodes.push({
  36. id: i,
  37. label: String(i)
  38. });
  39. connectionCount[i] = 0;
  40. // create edges in a scale-free-network way
  41. if (i == 1) {
  42. var from = i;
  43. var to = 0;
  44. edges.push({
  45. from: from,
  46. to: to
  47. });
  48. connectionCount[from]++;
  49. connectionCount[to]++;
  50. }
  51. else if (i > 1) {
  52. var conn = edges.length * 2;
  53. var rand = Math.floor(Math.random() * conn);
  54. var cum = 0;
  55. var j = 0;
  56. while (j < connectionCount.length && cum < rand) {
  57. cum += connectionCount[j];
  58. j++;
  59. }
  60. var from = i;
  61. var to = j;
  62. edges.push({
  63. from: from,
  64. to: to
  65. });
  66. connectionCount[from]++;
  67. connectionCount[to]++;
  68. }
  69. }
  70. // create a network
  71. var container = document.getElementById('mynetwork');
  72. var data = {
  73. nodes: nodes,
  74. edges: edges
  75. };
  76. var options = {
  77. physics: { stabilization: false }
  78. };
  79. network = new vis.Network(container, data, options);
  80. }
  81. </script>
  82. <script src="../googleAnalytics.js"></script>
  83. <body onload="draw();">
  84. <p>
  85. Generate a random network with nodes and edges.
  86. </p>
  87. <p>
  88. <label for="nodeCount">Number of nodes:</label>
  89. <input id="nodeCount" type="text" value="25" style="width: 50px;">
  90. <input type="submit" value="Go" onclick="draw()">
  91. </p>
  92. <div id="mynetwork"></div>
  93. </body>
  94. </html>