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.

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