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.

140 lines
4.8 KiB

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  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. // Create and populate a data table.
  20. nodes = new google.visualization.DataTable();
  21. nodes.addColumn('number', 'id');
  22. nodes.addColumn('string', 'text');
  23. nodes.addColumn('number', 'group');
  24. edges = new google.visualization.DataTable();
  25. edges.addColumn('number', 'from');
  26. edges.addColumn('number', 'to');
  27. edges.addColumn('number', 'length');
  28. edges.addColumn('string', 'color');
  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 = new Array(); // 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 = new Array();
  43. while (i < nodeCount) {
  44. nodes.addRow([i + nodeOffset, i + nodeOffset + '', group]);
  45. connectionCount[i] = 0;
  46. // create links in a scale-free-graph way
  47. if (i == 1) {
  48. var from = i;
  49. var to = 0;
  50. edges.addRow([from + nodeOffset, to + nodeOffset, len, color]);
  51. connectionCount[from]++;
  52. connectionCount[to]++;
  53. }
  54. else if (i > 1) {
  55. var conn = (i - 1) * 2;
  56. var rand = Math.floor(Math.random() * conn);
  57. var cum = 0;
  58. var j = 0;
  59. while (j < connectionCount.length && cum < rand) {
  60. cum += connectionCount[j];
  61. j++;
  62. }
  63. var from = i;
  64. var to = j;
  65. edges.addRow([from + nodeOffset, to + nodeOffset, len, color]);
  66. connectionCount[from]++;
  67. connectionCount[to]++;
  68. }
  69. i++;
  70. }
  71. // calculate the node with the most number of connections
  72. var leader = 0;
  73. for (c in connectionCount) {
  74. if (connectionCount[c] > connectionCount[leader])
  75. leader = parseInt(c);
  76. }
  77. if (group > groupMin) {
  78. // connect to the leader of this group to the leader of a random other group
  79. var from = leader + nodeOffset;
  80. var to = groupLeader[groupMin + parseInt(Math.random() * (group - groupMin))];
  81. edges.addRow([from, to, len, color]);
  82. }
  83. // add this leader to the list
  84. groupLeader[group] = leader + nodeOffset;
  85. nodeOffset += nodeCount;
  86. group++;
  87. }
  88. // specify options
  89. var options = {
  90. width: '600px',
  91. height: '600px',
  92. stabilize: false,
  93. nodes: {
  94. style: 'dot'
  95. },
  96. edges: {
  97. length: 50
  98. }
  99. };
  100. // Instantiate our graph object.
  101. graph = new vis.Graph(document.getElementById('mygraph'));
  102. // Draw our graph with the created data and options
  103. graph.draw(nodes, edges, options);
  104. }
  105. </script>
  106. </head>
  107. <body>
  108. <form onsubmit= "javascript: draw(); return false;">
  109. Number of groups:
  110. <input type="text" value="6" id="groupCount" style="width: 50px;">
  111. Number of nodes per group:
  112. <input type="text" value="7" id="nodeCount" style="width: 50px;">
  113. <input type="submit" value="Go">
  114. </form>
  115. <br>
  116. <div id="mygraph"></div>
  117. <div id="info"></div>
  118. </body>
  119. </html>