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.

185 lines
4.9 KiB

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Network | Data Manipulation</title>
  5. <style type="text/css">
  6. body {
  7. font: 10pt sans;
  8. }
  9. #mynetwork {
  10. position:relative;
  11. width: 600px;
  12. height: 600px;
  13. border: 1px solid lightgray;
  14. }
  15. table.legend_table {
  16. font-size: 11px;
  17. border-width:1px;
  18. border-color:#d3d3d3;
  19. border-style:solid;
  20. }
  21. table.legend_table,td {
  22. border-width:1px;
  23. border-color:#d3d3d3;
  24. border-style:solid;
  25. padding: 2px;
  26. }
  27. div.table_content {
  28. width:80px;
  29. text-align:center;
  30. }
  31. div.table_description {
  32. width:100px;
  33. }
  34. #operation {
  35. font-size:28px;
  36. }
  37. #network-popUp {
  38. display:none;
  39. position:absolute;
  40. top:350px;
  41. left:170px;
  42. z-index:299;
  43. width:250px;
  44. height:120px;
  45. background-color: #f9f9f9;
  46. border-style:solid;
  47. border-width:3px;
  48. border-color: #5394ed;
  49. padding:10px;
  50. text-align: center;
  51. }
  52. </style>
  53. <script type="text/javascript" src="../../dist/vis.js"></script>
  54. <link type="text/css" rel="stylesheet" href="../../dist/vis.css">
  55. <script type="text/javascript">
  56. var nodes = null;
  57. var edges = null;
  58. var network = null;
  59. function draw() {
  60. nodes = [];
  61. edges = [];
  62. var connectionCount = [];
  63. // randomly create some nodes and edges
  64. var nodeCount = 25;
  65. for (var i = 0; i < nodeCount; i++) {
  66. nodes.push({
  67. id: i,
  68. label: String(i)
  69. });
  70. connectionCount[i] = 0;
  71. // create edges in a scale-free-network way
  72. if (i == 1) {
  73. var from = i;
  74. var to = 0;
  75. edges.push({
  76. from: from,
  77. to: to
  78. });
  79. connectionCount[from]++;
  80. connectionCount[to]++;
  81. }
  82. else if (i > 1) {
  83. var conn = edges.length * 2;
  84. var rand = Math.floor(Math.random() * conn);
  85. var cum = 0;
  86. var j = 0;
  87. while (j < connectionCount.length && cum < rand) {
  88. cum += connectionCount[j];
  89. j++;
  90. }
  91. var from = i;
  92. var to = j;
  93. edges.push({
  94. from: from,
  95. to: to
  96. });
  97. connectionCount[from]++;
  98. connectionCount[to]++;
  99. }
  100. }
  101. // create a network
  102. var container = document.getElementById('mynetwork');
  103. var data = {
  104. nodes: nodes,
  105. edges: edges
  106. };
  107. var options = {
  108. physics: {
  109. stabilization: false
  110. },
  111. manipulation: true
  112. };
  113. network = new vis.Network(container, data, options);
  114. // add event listeners
  115. network.on('select', function(params) {
  116. document.getElementById('selection').innerHTML = 'Selection: ' + params.nodes;
  117. });
  118. network.on("resize", function(params) {console.log(params.width,params.height)});
  119. function clearPopUp() {
  120. var saveButton = document.getElementById('saveButton');
  121. var cancelButton = document.getElementById('cancelButton');
  122. saveButton.onclick = null;
  123. cancelButton.onclick = null;
  124. var div = document.getElementById('network-popUp');
  125. div.style.display = 'none';
  126. }
  127. function saveData(data,callback) {
  128. var idInput = document.getElementById('node-id');
  129. var labelInput = document.getElementById('node-label');
  130. var div = document.getElementById('network-popUp');
  131. data.id = idInput.value;
  132. data.label = labelInput.value;
  133. clearPopUp();
  134. callback(data);
  135. }
  136. }
  137. </script>
  138. <script src="../googleAnalytics.js"></script>
  139. </head>
  140. <body onload="draw();">
  141. <h2>Editing the dataset</h2>
  142. <div style="width: 700px; font-size:14px; text-align: justify;">
  143. In this example we have enabled the data manipulation setting. If the dataManipulation option is set to true, the edit button will appear.
  144. If you prefer to have the toolbar visible initially, you can set the initiallyVisible option to true. The exact method is described in the docs.
  145. <br /><br />
  146. The data manipulation allows the user to add nodes, connect them, edit them and delete any selected items. In this example we have created trigger functions
  147. for the add and edit operations. By settings these trigger functions the user can direct the way the data is manipulated. In this example we have created a simple
  148. pop-up that allows us to edit some of the properties.
  149. </div>
  150. <br />
  151. <div id="network-popUp">
  152. <span id="operation">node</span> <br>
  153. <table style="margin:auto;"><tr>
  154. <td>id</td><td><input id="node-id" value="new value"></td>
  155. </tr>
  156. <tr>
  157. <td>label</td><td><input id="node-label" value="new value"> </td>
  158. </tr></table>
  159. <input type="button" value="save" id="saveButton"></button>
  160. <input type="button" value="cancel" id="cancelButton"></button>
  161. </div>
  162. <br />
  163. <div id="mynetwork"></div>
  164. <p id="selection"></p>
  165. </body>
  166. </html>