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

10 years ago
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Network | Hierarchical layout</title>
  5. <style type="text/css">
  6. body {
  7. font: 10pt sans;
  8. }
  9. #mynetwork {
  10. width: 600px;
  11. height: 600px;
  12. border: 1px solid lightgray;
  13. }
  14. </style>
  15. <script type="text/javascript" src="../../dist/vis.js"></script>
  16. <link href="../../dist/vis.css" rel="stylesheet" type="text/css" />
  17. <script type="text/javascript">
  18. var nodes = null;
  19. var edges = null;
  20. var network = null;
  21. function destroy() {
  22. if (network !== null) {
  23. network.destroy();
  24. network = null;
  25. }
  26. }
  27. function draw() {
  28. destroy();
  29. nodes = [];
  30. edges = [];
  31. var connectionCount = [];
  32. // randomly create some nodes and edges
  33. var nodeCount = document.getElementById('nodeCount').value;
  34. for (var i = 0; i < nodeCount; i++) {
  35. nodes.push({
  36. id: i,
  37. label: String(i)
  38. });
  39. connectionCount[i] = 0;
  40. // create edges in a scale-free-network way
  41. if (i == 1) {
  42. var from = i;
  43. var to = 0;
  44. edges.push({
  45. from: from,
  46. to: to
  47. });
  48. connectionCount[from]++;
  49. connectionCount[to]++;
  50. }
  51. else if (i > 1) {
  52. var conn = edges.length * 2;
  53. var rand = Math.floor(Math.random() * conn);
  54. var cum = 0;
  55. var j = 0;
  56. while (j < connectionCount.length && cum < rand) {
  57. cum += connectionCount[j];
  58. j++;
  59. }
  60. var from = i;
  61. var to = j;
  62. edges.push({
  63. from: from,
  64. to: to
  65. });
  66. connectionCount[from]++;
  67. connectionCount[to]++;
  68. }
  69. }
  70. // create a network
  71. var container = document.getElementById('mynetwork');
  72. var data = {
  73. nodes: nodes,
  74. edges: edges
  75. };
  76. var directionInput = document.getElementById("direction").value;
  77. var options = {
  78. layout: {
  79. hierarchical:{
  80. direction: directionInput
  81. }
  82. }
  83. };
  84. network = new vis.Network(container, data, options);
  85. // add event listeners
  86. network.on('select', function(params) {
  87. document.getElementById('selection').innerHTML = 'Selection: ' + params.nodes;
  88. });
  89. }
  90. </script>
  91. <script src="../googleAnalytics.js"></script>
  92. </head>
  93. <body onload="draw();">
  94. <h2>Hierarchical Layout - Scale-Free-Network</h2>
  95. <div style="width:700px; font-size:14px; text-align: justify;">
  96. This example shows the randomly generated <b>scale-free-network</b> set of nodes and connected edges from example 2.
  97. In this example, hierarchical layout has been enabled and the vertical levels are determined automatically.
  98. </div>
  99. <br />
  100. <form onsubmit="draw(); return false;">
  101. <label for="nodeCount">Number of nodes:</label>
  102. <input id="nodeCount" type="text" value="25" style="width: 50px;">
  103. <input type="submit" value="Go">
  104. </form>
  105. <p>
  106. <input type="button" id="btn-UD" value="Up-Down">
  107. <input type="button" id="btn-DU" value="Down-Up">
  108. <input type="button" id="btn-LR" value="Left-Right">
  109. <input type="button" id="btn-RL" value="Right-Left">
  110. <input type="hidden" id='direction' value="UD">
  111. </p>
  112. <script language="javascript">
  113. var directionInput = document.getElementById("direction");
  114. var btnUD = document.getElementById("btn-UD");
  115. btnUD.onclick = function() {
  116. directionInput.value = "UD";
  117. draw();
  118. }
  119. var btnDU = document.getElementById("btn-DU");
  120. btnDU.onclick = function() {
  121. directionInput.value = "DU";
  122. draw();
  123. };
  124. var btnLR = document.getElementById("btn-LR");
  125. btnLR.onclick = function() {
  126. directionInput.value = "LR";
  127. draw();
  128. };
  129. var btnRL = document.getElementById("btn-RL");
  130. btnRL.onclick = function() {
  131. directionInput.value = "RL";
  132. draw();
  133. };
  134. </script>
  135. <br>
  136. <div id="mynetwork"></div>
  137. <p id="selection"></p>
  138. </body>
  139. </html>