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.

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