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.

224 lines
6.5 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. <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 graph = 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-graph 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 graph
  102. var container = document.getElementById('mygraph');
  103. var data = {
  104. nodes: nodes,
  105. edges: edges
  106. };
  107. var options = {
  108. edges: {
  109. length: 50
  110. },
  111. stabilize: false,
  112. dataManipulation: true,
  113. onAdd: function(data,callback) {
  114. var span = document.getElementById('operation');
  115. var idInput = document.getElementById('node-id');
  116. var labelInput = document.getElementById('node-label');
  117. var saveButton = document.getElementById('saveButton');
  118. var cancelButton = document.getElementById('cancelButton');
  119. var div = document.getElementById('graph-popUp');
  120. span.innerHTML = "Add Node";
  121. idInput.value = data.id;
  122. labelInput.value = data.label;
  123. saveButton.onclick = saveData.bind(this,data,callback);
  124. cancelButton.onclick = clearPopUp.bind();
  125. div.style.display = 'block';
  126. },
  127. onEdit: function(data,callback) {
  128. var span = document.getElementById('operation');
  129. var idInput = document.getElementById('node-id');
  130. var labelInput = document.getElementById('node-label');
  131. var saveButton = document.getElementById('saveButton');
  132. var cancelButton = document.getElementById('cancelButton');
  133. var div = document.getElementById('graph-popUp');
  134. span.innerHTML = "Edit Node";
  135. idInput.value = data.id;
  136. labelInput.value = data.label;
  137. saveButton.onclick = saveData.bind(this,data,callback);
  138. cancelButton.onclick = clearPopUp.bind();
  139. div.style.display = 'block';
  140. },
  141. onConnect: function(data,callback) {
  142. if (data.from == data.to) {
  143. var r=confirm("Do you want to connect the node to itself?");
  144. if (r==true) {
  145. callback(data);
  146. }
  147. }
  148. else {
  149. callback(data);
  150. }
  151. }
  152. };
  153. graph = new vis.Graph(container, data, options);
  154. // add event listeners
  155. graph.on('select', function(params) {
  156. document.getElementById('selection').innerHTML = 'Selection: ' + params.nodes;
  157. });
  158. graph.on("resize", function(params) {console.log(params.width,params.height)});
  159. function clearPopUp() {
  160. var saveButton = document.getElementById('saveButton');
  161. var cancelButton = document.getElementById('cancelButton');
  162. saveButton.onclick = null;
  163. cancelButton.onclick = null;
  164. var div = document.getElementById('graph-popUp');
  165. div.style.display = 'none';
  166. }
  167. function saveData(data,callback) {
  168. var idInput = document.getElementById('node-id');
  169. var labelInput = document.getElementById('node-label');
  170. var div = document.getElementById('graph-popUp');
  171. data.id = idInput.value;
  172. data.label = labelInput.value;
  173. clearPopUp();
  174. callback(data);
  175. }
  176. }
  177. </script>
  178. </head>
  179. <body onload="draw();">
  180. <h2>Editing the dataset</h2>
  181. <div style="width: 700px; font-size:14px;">
  182. In this example we have enabled the data manipulation setting. If the dataManipulation option is set to true, the edit button will appear.
  183. 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.
  184. <br /><br />
  185. 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
  186. 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
  187. pop-up that allows us to edit some of the properties.
  188. </div>
  189. <br />
  190. <div id="graph-popUp">
  191. <span id="operation">node</span> <br>
  192. <table style="margin:auto;"><tr>
  193. <td>id</td><td><input id="node-id" value="new value"></td>
  194. </tr>
  195. <tr>
  196. <td>label</td><td><input id="node-label" value="new value"> </td>
  197. </tr></table>
  198. <input type="button" value="save" id="saveButton"></button>
  199. <input type="button" value="cancel" id="cancelButton"></button>
  200. </div>
  201. <br />
  202. <div id="mygraph"></div>
  203. <p id="selection"></p>
  204. </body>
  205. </html>