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
5.0 KiB

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