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.

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