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.

120 lines
3.2 KiB

10 years ago
9 years ago
9 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 = {physics:{stabilization:false}};
  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. // setTimeout(destroy,500);
  89. }
  90. </script>
  91. <script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','//www.google-analytics.com/analytics.js','ga');ga('create', 'UA-61231638-1', 'auto');ga('send', 'pageview');</script></head>
  92. <body onload="draw();">
  93. <label for="nodeCount">Number of nodes:</label>
  94. <input id="nodeCount" type="text" value="25" style="width: 50px;">
  95. <input type="submit" value="Go" onclick="draw()">
  96. <br>
  97. <div id="mynetwork"></div>
  98. <p id="selection"></p>
  99. <p id="stabilization"></p>
  100. </body>
  101. </html>