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.

162 lines
4.8 KiB

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