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.

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