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.

146 lines
3.7 KiB

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Network | Random nodes</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 draw() {
  21. nodes = [];
  22. edges = [];
  23. var connectionCount = [];
  24. // randomly create some nodes and edges
  25. var nodeCount = document.getElementById('nodeCount').value;
  26. for (var i = 0; i < nodeCount; i++) {
  27. nodes.push({
  28. id: i,
  29. label: String(i)
  30. });
  31. connectionCount[i] = 0;
  32. // create edges in a scale-free-network way
  33. if (i == 1) {
  34. var from = i;
  35. var to = 0;
  36. edges.push({
  37. from: from,
  38. to: to
  39. });
  40. connectionCount[from]++;
  41. connectionCount[to]++;
  42. }
  43. else if (i > 1) {
  44. var conn = edges.length * 2;
  45. var rand = Math.floor(Math.random() * conn);
  46. var cum = 0;
  47. var j = 0;
  48. while (j < connectionCount.length && cum < rand) {
  49. cum += connectionCount[j];
  50. j++;
  51. }
  52. var from = i;
  53. var to = j;
  54. edges.push({
  55. from: from,
  56. to: to
  57. });
  58. connectionCount[from]++;
  59. connectionCount[to]++;
  60. }
  61. }
  62. // create a network
  63. var container = document.getElementById('mynetwork');
  64. var data = {
  65. nodes: nodes,
  66. edges: edges
  67. };
  68. var directionInput = document.getElementById("direction");
  69. var options = {
  70. stabilize: false,
  71. smoothCurves: false,
  72. hierarchicalLayout: {
  73. direction: directionInput.value
  74. }
  75. };
  76. network = new vis.Network(container, data, options);
  77. // add event listeners
  78. network.on('select', function(params) {
  79. document.getElementById('selection').innerHTML = 'Selection: ' + params.nodes;
  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. <input type="button" id="btn-UD" value="Up-Down">
  97. <input type="button" id="btn-DU" value="Down-Up">
  98. <input type="button" id="btn-LR" value="Left-Right">
  99. <input type="button" id="btn-RL" value="Right-Left">
  100. <input type="hidden" id='direction' value="UD">
  101. <script language="javascript">
  102. var directionInput = document.getElementById("direction");
  103. var btnUD = document.getElementById("btn-UD");
  104. btnUD.onclick = function() {
  105. directionInput.value = "UD";
  106. draw();
  107. }
  108. var btnDU = document.getElementById("btn-DU");
  109. btnDU.onclick = function() {
  110. directionInput.value = "DU";
  111. draw();
  112. };
  113. var btnLR = document.getElementById("btn-LR");
  114. btnLR.onclick = function() {
  115. directionInput.value = "LR";
  116. draw();
  117. };
  118. var btnRL = document.getElementById("btn-RL");
  119. btnRL.onclick = function() {
  120. directionInput.value = "RL";
  121. draw();
  122. };
  123. </script>
  124. <br>
  125. <div id="mynetwork"></div>
  126. <p id="selection"></p>
  127. </body>
  128. </html>