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.

193 lines
5.5 KiB

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8"/>
  5. <title>Network | Manipulation</title>
  6. <style type="text/css">
  7. body, select {
  8. font: 10pt sans;
  9. }
  10. #mynetwork {
  11. position:relative;
  12. width: 800px;
  13. height: 600px;
  14. border: 1px solid lightgray;
  15. }
  16. table.legend_table {
  17. font-size: 11px;
  18. border-width:1px;
  19. border-color:#d3d3d3;
  20. border-style:solid;
  21. }
  22. table.legend_table,td {
  23. border-width:1px;
  24. border-color:#d3d3d3;
  25. border-style:solid;
  26. padding: 2px;
  27. }
  28. div.table_content {
  29. width:80px;
  30. text-align:center;
  31. }
  32. div.table_description {
  33. width:100px;
  34. }
  35. #operation {
  36. font-size:28px;
  37. }
  38. #network-popUp {
  39. display:none;
  40. position:absolute;
  41. top:350px;
  42. left:170px;
  43. z-index:299;
  44. width:250px;
  45. height:120px;
  46. background-color: #f9f9f9;
  47. border-style:solid;
  48. border-width:3px;
  49. border-color: #5394ed;
  50. padding:10px;
  51. text-align: center;
  52. }
  53. </style>
  54. <script type="text/javascript" src="../exampleUtil.js"></script>
  55. <script type="text/javascript" src="../../../dist/vis.js"></script>
  56. <link href="../../../dist/vis-network.min.css" rel="stylesheet" type="text/css" />
  57. <script type="text/javascript">
  58. var nodes = null;
  59. var edges = null;
  60. var network = null;
  61. // randomly create some nodes and edges
  62. var data = getScaleFreeNetwork(25);
  63. var seed = 2;
  64. function setDefaultLocale() {
  65. var defaultLocal = navigator.language;
  66. var select = document.getElementById('locale');
  67. select.selectedIndex = 0; // set fallback value
  68. for (var i = 0, j = select.options.length; i < j; ++i) {
  69. if (select.options[i].getAttribute('value') === defaultLocal) {
  70. select.selectedIndex = i;
  71. break;
  72. }
  73. }
  74. }
  75. function destroy() {
  76. if (network !== null) {
  77. network.destroy();
  78. network = null;
  79. }
  80. }
  81. function draw() {
  82. destroy();
  83. nodes = [];
  84. edges = [];
  85. // create a network
  86. var container = document.getElementById('mynetwork');
  87. var options = {
  88. layout: {randomSeed:seed}, // just to make sure the layout is the same when the locale is changed
  89. locale: document.getElementById('locale').value,
  90. manipulation: {
  91. addNode: function (data, callback) {
  92. // filling in the popup DOM elements
  93. document.getElementById('operation').innerHTML = "Add Node";
  94. document.getElementById('node-id').value = data.id;
  95. document.getElementById('node-label').value = data.label;
  96. document.getElementById('saveButton').onclick = saveData.bind(this, data, callback);
  97. document.getElementById('cancelButton').onclick = clearPopUp.bind();
  98. document.getElementById('network-popUp').style.display = 'block';
  99. },
  100. editNode: function (data, callback) {
  101. // filling in the popup DOM elements
  102. document.getElementById('operation').innerHTML = "Edit Node";
  103. document.getElementById('node-id').value = data.id;
  104. document.getElementById('node-label').value = data.label;
  105. document.getElementById('saveButton').onclick = saveData.bind(this, data, callback);
  106. document.getElementById('cancelButton').onclick = cancelEdit.bind(this,callback);
  107. document.getElementById('network-popUp').style.display = 'block';
  108. },
  109. addEdge: function (data, callback) {
  110. if (data.from == data.to) {
  111. var r = confirm("Do you want to connect the node to itself?");
  112. if (r == true) {
  113. callback(data);
  114. }
  115. }
  116. else {
  117. callback(data);
  118. }
  119. }
  120. }
  121. };
  122. network = new vis.Network(container, data, options);
  123. }
  124. function clearPopUp() {
  125. document.getElementById('saveButton').onclick = null;
  126. document.getElementById('cancelButton').onclick = null;
  127. document.getElementById('network-popUp').style.display = 'none';
  128. }
  129. function cancelEdit(callback) {
  130. clearPopUp();
  131. callback(null);
  132. }
  133. function saveData(data,callback) {
  134. data.id = document.getElementById('node-id').value;
  135. data.label = document.getElementById('node-label').value;
  136. clearPopUp();
  137. callback(data);
  138. }
  139. function init() {
  140. setDefaultLocale();
  141. draw();
  142. }
  143. </script>
  144. </head>
  145. <body onload="init();">
  146. <h2>Editing the nodes and edges (localized)</h2>
  147. <p style="width: 700px; font-size:14px; text-align: justify;">
  148. The localization is only relevant to the manipulation buttons.
  149. </p>
  150. <p>
  151. <label for="locale">Select a locale:</label>
  152. <select id="locale" onchange="draw();">
  153. <option value="en">en</option>
  154. <option value="de">de</option>
  155. <option value="es">es</option>
  156. <option value="it">it</option>
  157. <option value="nl">nl</option>
  158. <option value="pt-br">pt</option>
  159. <option value="ru">ru</option>
  160. </select>
  161. </p>
  162. <div id="network-popUp">
  163. <span id="operation">node</span> <br>
  164. <table style="margin:auto;"><tr>
  165. <td>id</td><td><input id="node-id" value="new value" /></td>
  166. </tr>
  167. <tr>
  168. <td>label</td><td><input id="node-label" value="new value" /></td>
  169. </tr></table>
  170. <input type="button" value="save" id="saveButton" />
  171. <input type="button" value="cancel" id="cancelButton" />
  172. </div>
  173. <br />
  174. <div id="mynetwork"></div>
  175. </body>
  176. </html>