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.

246 lines
6.9 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: {
  119. //handlerFunctions: {
  120. addNode: function(data,callback) {
  121. var span = document.getElementById('operation');
  122. var idInput = document.getElementById('node-id');
  123. var labelInput = document.getElementById('node-label');
  124. var saveButton = document.getElementById('saveButton');
  125. var cancelButton = document.getElementById('cancelButton');
  126. var div = document.getElementById('network-popUp');
  127. span.innerHTML = "Add Node";
  128. idInput.value = data.id;
  129. labelInput.value = data.label;
  130. saveButton.onclick = saveData.bind(this,data,callback);
  131. cancelButton.onclick = clearPopUp.bind();
  132. div.style.display = 'block';
  133. },
  134. editNode: function(data,callback) {
  135. var span = document.getElementById('operation');
  136. var idInput = document.getElementById('node-id');
  137. var labelInput = document.getElementById('node-label');
  138. var saveButton = document.getElementById('saveButton');
  139. var cancelButton = document.getElementById('cancelButton');
  140. var div = document.getElementById('network-popUp');
  141. span.innerHTML = "Edit Node";
  142. idInput.value = data.id;
  143. labelInput.value = data.label;
  144. saveButton.onclick = saveData.bind(this,data,callback);
  145. cancelButton.onclick = clearPopUp.bind();
  146. div.style.display = 'block';
  147. },
  148. addEdge: function(data,callback) {
  149. if (data.from == data.to) {
  150. var r=confirm("Do you want to connect the node to itself?");
  151. if (r==true) {
  152. callback(data);
  153. }
  154. }
  155. else {
  156. callback(data);
  157. }
  158. }
  159. //}
  160. }
  161. };
  162. network = new vis.Network(container, data, options);
  163. // add event listeners
  164. network.on('select', function(params) {
  165. document.getElementById('selection').innerHTML = 'Selection: ' + params.nodes;
  166. });
  167. network.on("resize", function(params) {console.log(params.width,params.height)});
  168. function clearPopUp() {
  169. var saveButton = document.getElementById('saveButton');
  170. var cancelButton = document.getElementById('cancelButton');
  171. saveButton.onclick = null;
  172. cancelButton.onclick = null;
  173. var div = document.getElementById('network-popUp');
  174. div.style.display = 'none';
  175. }
  176. function saveData(data,callback) {
  177. var idInput = document.getElementById('node-id');
  178. var labelInput = document.getElementById('node-label');
  179. data.id = idInput.value;
  180. data.label = labelInput.value;
  181. clearPopUp();
  182. callback(data);
  183. }
  184. // update the locale when changing the select box value
  185. var select = document.getElementById('locale');
  186. select.onchange = function () {
  187. network.setOptions({
  188. manipulation: {
  189. locale: this.value
  190. }
  191. });
  192. };
  193. select.onchange();
  194. }
  195. </script>
  196. <script src="../googleAnalytics.js"></script>
  197. </head>
  198. <body onload="draw();">
  199. <h2>Editing the dataset (localized)</h2>
  200. <p style="width: 700px; font-size:14px; text-align: justify;">
  201. 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.
  202. </p>
  203. <p>
  204. <label for="locale">Select a locale:</label>
  205. <select id="locale">
  206. <option value="en" selected>en</option>
  207. <option value="nl">nl</option>
  208. </select>
  209. </p>
  210. <div id="network-popUp">
  211. <span id="operation">node</span> <br>
  212. <table style="margin:auto;"><tr>
  213. <td>id</td><td><input id="node-id" value="new value" /></td>
  214. </tr>
  215. <tr>
  216. <td>label</td><td><input id="node-label" value="new value" /></td>
  217. </tr></table>
  218. <input type="button" value="save" id="saveButton" />
  219. <input type="button" value="cancel" id="cancelButton" />
  220. </div>
  221. <br />
  222. <div id="mynetwork"></div>
  223. <p id="selection"></p>
  224. </body>
  225. </html>