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.

214 lines
7.6 KiB

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Graph | Navigation</title>
  5. <style type="text/css">
  6. body {
  7. font: 10pt sans;
  8. }
  9. #mygraph {
  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. #graph-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. <link type="text/css" rel="stylesheet" charset="UTF-8" href="../../dist/css/graph-manipulation.css">
  54. <link type="text/css" rel="stylesheet" charset="UTF-8" href="../../dist/css/graph-navigation.css">
  55. <script type="text/javascript" src="../../dist/vis.js"></script>
  56. <script type="text/javascript">
  57. var nodes = null;
  58. var edges = null;
  59. var graph = null;
  60. function draw() {
  61. nodes = [];
  62. edges = [];
  63. var connectionCount = [];
  64. // randomly create some nodes and edges
  65. var nodeCount = 25;
  66. for (var i = 0; i < nodeCount; i++) {
  67. nodes.push({
  68. id: i,
  69. label: String(i)
  70. });
  71. connectionCount[i] = 0;
  72. // create edges in a scale-free-graph way
  73. if (i == 1) {
  74. var from = i;
  75. var to = 0;
  76. edges.push({
  77. from: from,
  78. to: to
  79. });
  80. connectionCount[from]++;
  81. connectionCount[to]++;
  82. }
  83. else if (i > 1) {
  84. var conn = edges.length * 2;
  85. var rand = Math.floor(Math.random() * conn);
  86. var cum = 0;
  87. var j = 0;
  88. while (j < connectionCount.length && cum < rand) {
  89. cum += connectionCount[j];
  90. j++;
  91. }
  92. var from = i;
  93. var to = j;
  94. edges.push({
  95. from: from,
  96. to: to
  97. });
  98. connectionCount[from]++;
  99. connectionCount[to]++;
  100. }
  101. }
  102. // create a graph
  103. var container = document.getElementById('mygraph');
  104. var data = {
  105. nodes: nodes,
  106. edges: edges
  107. };
  108. var options = {
  109. edges: {
  110. length: 50
  111. },
  112. stabilize: false,
  113. dataManipulation: true,
  114. onAdd: function(data,callback) {
  115. var span = document.getElementById('operation');
  116. var idInput = document.getElementById('node-id');
  117. var labelInput = document.getElementById('node-label');
  118. var saveButton = document.getElementById('saveButton');
  119. var cancelButton = document.getElementById('cancelButton');
  120. var div = document.getElementById('graph-popUp');
  121. span.innerHTML = "Add Node";
  122. idInput.value = data.id;
  123. labelInput.value = data.label;
  124. saveButton.onclick = saveData.bind(this,data,callback);
  125. cancelButton.onclick = clearPopUp.bind();
  126. div.style.display = 'block';
  127. },
  128. onEdit: function(data,callback) {
  129. var span = document.getElementById('operation');
  130. var idInput = document.getElementById('node-id');
  131. var labelInput = document.getElementById('node-label');
  132. var saveButton = document.getElementById('saveButton');
  133. var cancelButton = document.getElementById('cancelButton');
  134. var div = document.getElementById('graph-popUp');
  135. span.innerHTML = "Edit Node";
  136. idInput.value = data.id;
  137. labelInput.value = data.label;
  138. saveButton.onclick = saveData.bind(this,data,callback);
  139. cancelButton.onclick = clearPopUp.bind();
  140. div.style.display = 'block';
  141. }
  142. };
  143. graph = new vis.Graph(container, data, options);
  144. // add event listeners
  145. graph.on('select', function(params) {
  146. document.getElementById('selection').innerHTML = 'Selection: ' + params.nodes;
  147. });
  148. graph.on("frameResize", function(params) {console.log(params.width,params.height)});
  149. function clearPopUp() {
  150. var saveButton = document.getElementById('saveButton');
  151. var cancelButton = document.getElementById('cancelButton');
  152. saveButton.onclick = null;
  153. cancelButton.onclick = null;
  154. var div = document.getElementById('graph-popUp');
  155. div.style.display = 'none';
  156. }
  157. function saveData(data,callback) {
  158. var idInput = document.getElementById('node-id');
  159. var labelInput = document.getElementById('node-label');
  160. var div = document.getElementById('graph-popUp');
  161. data.id = idInput.value;
  162. data.label = labelInput.value;
  163. clearPopUp();
  164. callback(data);
  165. }
  166. }
  167. </script>
  168. </head>
  169. <body onload="draw();">
  170. <h2>Editing the dataset</h2>
  171. <div style="width: 700px; font-size:14px;">
  172. In this example we have enabled the data manipulation setting. If the dataManipulation option is set to true, the edit button will appear.
  173. 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.
  174. <br /><br />
  175. 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
  176. 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
  177. pop-up that allows us to edit some of the properties.
  178. </div>
  179. <br />
  180. <div id="graph-popUp">
  181. <span id="operation">node</span> <br>
  182. <table style="margin:auto;"><tr>
  183. <td>id</td><td><input id="node-id" value="new value"></td>
  184. </tr>
  185. <tr>
  186. <td>label</td><td><input id="node-label" value="new value"> </td>
  187. </tr></table>
  188. <input type="button" value="save" id="saveButton"></button>
  189. <input type="button" value="cancel" id="cancelButton"></button>
  190. </div>
  191. <br />
  192. <div id="mygraph"></div>
  193. <p id="selection"></p>
  194. </body>
  195. </html>