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.

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