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.

128 lines
3.9 KiB

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Network | Scale free network clustering</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 draw() {
  22. nodes = [];
  23. edges = [];
  24. var connectionCount = [];
  25. // randomly create some nodes and edges
  26. var nodeCount = document.getElementById('nodeCount').value;
  27. for (var i = 0; i < nodeCount; i++) {
  28. nodes.push({
  29. id: i,
  30. label: String(i)
  31. });
  32. connectionCount[i] = 0;
  33. // create edges in a scale-free-network way
  34. if (i == 1) {
  35. var from = i;
  36. var to = 0;
  37. edges.push({
  38. from: from,
  39. to: to
  40. });
  41. connectionCount[from]++;
  42. connectionCount[to]++;
  43. }
  44. else if (i > 1) {
  45. var conn = edges.length * 2;
  46. var rand = Math.floor(Math.random() * conn);
  47. var cum = 0;
  48. var j = 0;
  49. while (j < connectionCount.length && cum < rand) {
  50. cum += connectionCount[j];
  51. j++;
  52. }
  53. var from = i;
  54. var to = j;
  55. edges.push({
  56. from: from,
  57. to: to
  58. });
  59. connectionCount[from]++;
  60. connectionCount[to]++;
  61. }
  62. }
  63. // create a network
  64. var clusteringOn = document.getElementById('clustering').checked;
  65. var clusterEdgeThreshold = parseInt(document.getElementById('clusterEdgeThreshold').value);
  66. var container = document.getElementById('mynetwork');
  67. var data = {
  68. nodes: nodes,
  69. edges: edges
  70. };
  71. var options = {
  72. clustering: {
  73. enabled: clusteringOn,
  74. clusterEdgeThreshold: clusterEdgeThreshold
  75. },
  76. stabilize: false
  77. };
  78. network = new vis.Network(container, data, options);
  79. // add event listeners
  80. network.on('select', function(params) {
  81. document.getElementById('selection').innerHTML = 'Selection: ' + params.nodes;
  82. });
  83. }
  84. </script>
  85. </head>
  86. <body onload="draw();">
  87. <h2>Clustering - Scale-Free-Network</h2>
  88. <div style="width:700px; font-size:14px; text-align: justify;">
  89. This example shows the randomly generated <b>scale-free-network</b> set of nodes and connected edges from example 2.
  90. By clicking the checkbox you can turn clustering on and off. If you increase the number of nodes to
  91. a value higher than 100, automatic clustering is used before the initial draw (assuming the checkbox is checked).
  92. <br />
  93. <br />
  94. Clustering is done automatically when zooming out. When zooming in over the cluster, the cluster pops open. When the cluster is very big, a special instance
  95. will be created and the cluster contents will only be simulated in there. Double click will also open a cluster.
  96. <br />
  97. <br />
  98. Try values of 500 and 5000 with and without clustering. All thresholds can be changed to suit your dataset.
  99. Experiment with the clusterEdgeThreshold, which increases the formation of clusters when zoomed out (assuming the checkbox is checked).
  100. </div>
  101. <br />
  102. <form onsubmit="draw(); return false;">
  103. <label for="nodeCount">Number of nodes:</label>
  104. <input id="nodeCount" type="text" value="125" style="width: 50px;">
  105. <label for="clustering">Enable Clustering:</label>
  106. <input id="clustering" type="checkbox" onChange="draw()" checked="true">
  107. <label for="clusterEdgeThreshold">clusterEdgeThreshold:</label>
  108. <input id="clusterEdgeThreshold" type="text" value="20" style="width: 50px;">
  109. <input type="submit" value="Go">
  110. </form>
  111. <br>
  112. <div id="mynetwork"></div>
  113. <p id="selection"></p>
  114. </body>
  115. </html>