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.

126 lines
2.9 KiB

10 years ago
  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 = {stabilize:false, clickToUse:true};
  77. network = new vis.Network(container, data, options);
  78. // add event listeners
  79. network.on('select', function(params) {
  80. document.getElementById('selection').innerHTML = 'Selection: ' + params.nodes;
  81. });
  82. network.on('stabilized', function (params) {
  83. document.getElementById('stabilization').innerHTML = 'Stabilization took ' + params.iterations + ' iterations.';
  84. });
  85. network.on('startStabilization', function (params) {
  86. document.getElementById('stabilization').innerHTML = 'Stabilizing...';
  87. });
  88. network.setData({
  89. nodes: nodes,
  90. edges: edges,
  91. options: options
  92. });
  93. }
  94. </script>
  95. </head>
  96. <body onload="draw();">
  97. <form onsubmit="draw(); return false;">
  98. <label for="nodeCount">Number of nodes:</label>
  99. <input id="nodeCount" type="text" value="25" style="width: 50px;">
  100. <input type="submit" value="Go">
  101. </form>
  102. <br>
  103. <div id="mynetwork"></div>
  104. <p id="selection"></p>
  105. <p id="stabilization"></p>
  106. </body>
  107. </html>