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.

166 lines
4.2 KiB

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Network | Groups</title>
  5. <style>
  6. body {
  7. font: 10pt arial;
  8. }
  9. #mynetwork {
  10. width: 600px;
  11. height: 600px;
  12. border: 1px solid lightgray;
  13. }
  14. </style>
  15. <script type="text/javascript" src="http://www.google.com/jsapi"></script>
  16. <script type="text/javascript" src="../../dist/vis.js"></script>
  17. <link href="../../dist/vis.css" rel="stylesheet" type="text/css" />
  18. <script type="text/javascript">
  19. var nodes = null;
  20. var edges = null;
  21. var network = null;
  22. var nodesData = null;
  23. google.load('visualization', '1');
  24. // Set callback to run when API is loaded
  25. google.setOnLoadCallback(draw);
  26. function destroy() {
  27. if (network !== null) {
  28. network.destroy();
  29. network = null;
  30. }
  31. }
  32. // Called when the Visualization API is loaded.
  33. function draw() {
  34. destroy();
  35. var from, to;
  36. nodes = [];
  37. edges = [];
  38. var color = 'gray';
  39. var len = undefined;
  40. // randomly create some nodes
  41. var nodeCount = parseInt(document.getElementById('nodeCount').value);
  42. var nodeOffset = 0;
  43. var groupMin = 0;
  44. var groupMax = parseInt(document.getElementById('groupCount').value);
  45. var group = groupMin;
  46. var groupLeader = []; // will contain the node id with the most links of each group
  47. while (group < groupMax) {
  48. // randomly create some nodes
  49. var i = 0;
  50. var cols = parseInt(Math.sqrt(nodeCount));
  51. var connectionCount = [];
  52. while (i < nodeCount) {
  53. nodes.push({
  54. id: i + nodeOffset,
  55. label: String(i + nodeOffset),
  56. group: group
  57. });
  58. connectionCount[i] = 0;
  59. // create links in a scale-free-network way
  60. if (i == 1) {
  61. from = i;
  62. to = 0;
  63. edges.push({
  64. from: from + nodeOffset,
  65. to: to + nodeOffset,
  66. length: len
  67. });
  68. connectionCount[from]++;
  69. connectionCount[to]++;
  70. }
  71. else if (i > 1) {
  72. var conn = (i - 1) * 2;
  73. var rand = Math.floor(Math.random() * conn);
  74. var cum = 0;
  75. var j = 0;
  76. while (j < connectionCount.length && cum < rand) {
  77. cum += connectionCount[j];
  78. j++;
  79. }
  80. from = i;
  81. to = j;
  82. edges.push({
  83. from: from + nodeOffset,
  84. to: to + nodeOffset,
  85. length: len
  86. });
  87. connectionCount[from]++;
  88. connectionCount[to]++;
  89. }
  90. i++;
  91. }
  92. // calculate the node with the most number of connections
  93. var leader = 0;
  94. for (var c in connectionCount) {
  95. if (connectionCount.hasOwnProperty(c)) {
  96. if (connectionCount[c] > connectionCount[leader]) {
  97. leader = parseInt(c);
  98. }
  99. }
  100. }
  101. if (group > groupMin) {
  102. // connect to the leader of this group to the leader of a random other group
  103. from = leader + nodeOffset;
  104. to = groupLeader[groupMin + parseInt(Math.random() * (group - groupMin))];
  105. edges.push({
  106. from: from,
  107. to: to,
  108. length: len
  109. });
  110. }
  111. // add this leader to the list
  112. groupLeader[group] = leader + nodeOffset;
  113. nodeOffset += nodeCount;
  114. group++;
  115. }
  116. nodesData = new vis.DataSet(nodes);
  117. // create a network
  118. var container = document.getElementById('mynetwork');
  119. var data = {
  120. nodes: nodesData,
  121. edges: edges
  122. };
  123. var options = {
  124. stabilize: false,
  125. nodes: {
  126. shape: 'dot'
  127. },
  128. physics: {barnesHut:{springLength: 200}}
  129. };
  130. network = new vis.Network(container, data, options);
  131. }
  132. </script>
  133. </head>
  134. <body onload="draw()">
  135. <form onsubmit= "javascript: draw(); return false;">
  136. Number of groups:
  137. <input type="text" value="6" id="groupCount" style="width: 50px;">
  138. Number of nodes per group:
  139. <input type="text" value="7" id="nodeCount" style="width: 50px;">
  140. <input type="submit" value="Go">
  141. </form>
  142. <br>
  143. <div id="mynetwork"></div>
  144. </body>
  145. </html>