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.

242 lines
6.7 KiB

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Network | Localization</title>
  5. <style type="text/css">
  6. body, select {
  7. font: 10pt sans;
  8. }
  9. #mynetwork {
  10. position:relative;
  11. width: 800px;
  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 destroy() {
  60. if (network !== null) {
  61. network.destroy();
  62. network = null;
  63. }
  64. }
  65. function draw() {
  66. destroy();
  67. nodes = [];
  68. edges = [];
  69. var connectionCount = [];
  70. // randomly create some nodes and edges
  71. var nodeCount = 25;
  72. for (var i = 0; i < nodeCount; i++) {
  73. nodes.push({
  74. id: i,
  75. label: String(i)
  76. });
  77. connectionCount[i] = 0;
  78. // create edges in a scale-free-network way
  79. if (i == 1) {
  80. var from = i;
  81. var to = 0;
  82. edges.push({
  83. from: from,
  84. to: to
  85. });
  86. connectionCount[from]++;
  87. connectionCount[to]++;
  88. }
  89. else if (i > 1) {
  90. var conn = edges.length * 2;
  91. var rand = Math.floor(Math.random() * conn);
  92. var cum = 0;
  93. var j = 0;
  94. while (j < connectionCount.length && cum < rand) {
  95. cum += connectionCount[j];
  96. j++;
  97. }
  98. var from = i;
  99. var to = j;
  100. edges.push({
  101. from: from,
  102. to: to
  103. });
  104. connectionCount[from]++;
  105. connectionCount[to]++;
  106. }
  107. }
  108. // create a network
  109. var container = document.getElementById('mynetwork');
  110. var data = {
  111. nodes: nodes,
  112. edges: edges
  113. };
  114. var options = {
  115. physics: {
  116. stabilization: false
  117. },
  118. manipulation: true,
  119. onAdd: function(data,callback) {
  120. var span = document.getElementById('operation');
  121. var idInput = document.getElementById('node-id');
  122. var labelInput = document.getElementById('node-label');
  123. var saveButton = document.getElementById('saveButton');
  124. var cancelButton = document.getElementById('cancelButton');
  125. var div = document.getElementById('network-popUp');
  126. span.innerHTML = "Add Node";
  127. idInput.value = data.id;
  128. labelInput.value = data.label;
  129. saveButton.onclick = saveData.bind(this,data,callback);
  130. cancelButton.onclick = clearPopUp.bind();
  131. div.style.display = 'block';
  132. },
  133. onEdit: function(data,callback) {
  134. var span = document.getElementById('operation');
  135. var idInput = document.getElementById('node-id');
  136. var labelInput = document.getElementById('node-label');
  137. var saveButton = document.getElementById('saveButton');
  138. var cancelButton = document.getElementById('cancelButton');
  139. var div = document.getElementById('network-popUp');
  140. span.innerHTML = "Edit Node";
  141. idInput.value = data.id;
  142. labelInput.value = data.label;
  143. saveButton.onclick = saveData.bind(this,data,callback);
  144. cancelButton.onclick = clearPopUp.bind();
  145. div.style.display = 'block';
  146. },
  147. onConnect: function(data,callback) {
  148. if (data.from == data.to) {
  149. var r=confirm("Do you want to connect the node to itself?");
  150. if (r==true) {
  151. callback(data);
  152. }
  153. }
  154. else {
  155. callback(data);
  156. }
  157. }
  158. };
  159. network = new vis.Network(container, data, options);
  160. // add event listeners
  161. network.on('select', function(params) {
  162. document.getElementById('selection').innerHTML = 'Selection: ' + params.nodes;
  163. });
  164. network.on("resize", function(params) {console.log(params.width,params.height)});
  165. function clearPopUp() {
  166. var saveButton = document.getElementById('saveButton');
  167. var cancelButton = document.getElementById('cancelButton');
  168. saveButton.onclick = null;
  169. cancelButton.onclick = null;
  170. var div = document.getElementById('network-popUp');
  171. div.style.display = 'none';
  172. }
  173. function saveData(data,callback) {
  174. var idInput = document.getElementById('node-id');
  175. var labelInput = document.getElementById('node-label');
  176. var div = document.getElementById('network-popUp');
  177. data.id = idInput.value;
  178. data.label = labelInput.value;
  179. clearPopUp();
  180. callback(data);
  181. }
  182. // update the locale when changing the select box value
  183. var select = document.getElementById('locale');
  184. select.onchange = function () {
  185. network.setOptions({
  186. locale: this.value
  187. });
  188. };
  189. select.onchange();
  190. }
  191. </script>
  192. <script src="../googleAnalytics.js"></script>
  193. </head>
  194. <body onload="draw();">
  195. <h2>Editing the dataset (localized)</h2>
  196. <p style="width: 700px; font-size:14px; text-align: justify;">
  197. This is the same example as <a href="21_data_manipulation.html">21_data_manipulation.html</a>, except that there is a select box added which allows to switch locale. The localization is only relevant to the manipulation buttons.
  198. </p>
  199. <p>
  200. <label for="locale">Select a locale:</label>
  201. <select id="locale">
  202. <option value="en" selected>en</option>
  203. <option value="nl">nl</option>
  204. </select>
  205. </p>
  206. <div id="network-popUp">
  207. <span id="operation">node</span> <br>
  208. <table style="margin:auto;"><tr>
  209. <td>id</td><td><input id="node-id" value="new value"></td>
  210. </tr>
  211. <tr>
  212. <td>label</td><td><input id="node-label" value="new value"> </td>
  213. </tr></table>
  214. <input type="button" value="save" id="saveButton"></button>
  215. <input type="button" value="cancel" id="cancelButton"></button>
  216. </div>
  217. <br />
  218. <div id="mynetwork"></div>
  219. <p id="selection"></p>
  220. </body>
  221. </html>