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.

153 lines
4.3 KiB

10 years ago
  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. <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>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','//www.google-analytics.com/analytics.js','ga');ga('create', 'UA-61231638-1', 'auto');ga('send', 'pageview');</script></head>
  92. <body onload="draw();">
  93. <h2>Hierarchical Layout - Scale-Free-Network</h2>
  94. <div style="width:700px; font-size:14px; text-align: justify;">
  95. This example shows the randomly generated <b>scale-free-network</b> set of nodes and connected edges from example 2.
  96. In this example, hierarchical layout has been enabled and the vertical levels are determined automatically.
  97. </div>
  98. <br />
  99. <form onsubmit="draw(); return false;">
  100. <label for="nodeCount">Number of nodes:</label>
  101. <input id="nodeCount" type="text" value="25" style="width: 50px;">
  102. <input type="submit" value="Go">
  103. </form>
  104. <input type="button" id="btn-UD" value="Up-Down">
  105. <input type="button" id="btn-DU" value="Down-Up">
  106. <input type="button" id="btn-LR" value="Left-Right">
  107. <input type="button" id="btn-RL" value="Right-Left">
  108. <input type="hidden" id='direction' value="UD">
  109. <script language="javascript">
  110. var directionInput = document.getElementById("direction");
  111. var btnUD = document.getElementById("btn-UD");
  112. btnUD.onclick = function() {
  113. directionInput.value = "UD";
  114. draw();
  115. }
  116. var btnDU = document.getElementById("btn-DU");
  117. btnDU.onclick = function() {
  118. directionInput.value = "DU";
  119. draw();
  120. };
  121. var btnLR = document.getElementById("btn-LR");
  122. btnLR.onclick = function() {
  123. directionInput.value = "LR";
  124. draw();
  125. };
  126. var btnRL = document.getElementById("btn-RL");
  127. btnRL.onclick = function() {
  128. directionInput.value = "RL";
  129. draw();
  130. };
  131. </script>
  132. <br>
  133. <div id="mynetwork"></div>
  134. <p id="selection"></p>
  135. </body>
  136. </html>