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.

269 lines
5.6 KiB

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Graph | DataSet</title>
  5. <style type="text/css">
  6. html, body {
  7. font: 11pt arial;
  8. }
  9. h1 {
  10. font-size: 150%;
  11. margin: 5px 0;
  12. }
  13. h2 {
  14. font-size: 100%;
  15. margin: 5px 0;
  16. }
  17. table.view {
  18. width: 100%;
  19. }
  20. table td {
  21. vertical-align: top;
  22. }
  23. table table {
  24. background-color: #f5f5f5;
  25. border: 1px solid #e5e5e5;
  26. }
  27. table table td {
  28. vertical-align: middle;
  29. }
  30. input[type=text], pre {
  31. border: 1px solid lightgray;
  32. }
  33. pre {
  34. margin: 0;
  35. padding: 5px;
  36. font-size: 10pt;
  37. }
  38. #graph {
  39. width: 100%;
  40. height: 400px;
  41. border: 1px solid lightgray;
  42. }
  43. </style>
  44. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
  45. <script type="text/javascript" src="../../dist/vis.js"></script>
  46. <script type="text/javascript">
  47. var nodes, edges, graph;
  48. // convenience method to stringify a JSON object
  49. function toJSON (obj) {
  50. return JSON.stringify(obj, null, 4);
  51. }
  52. $(window).load(function () {
  53. // attach actions to the node buttons
  54. $('#node-add').click(function () {
  55. try {
  56. nodes.add({
  57. id: $('#node-id').val(),
  58. label: $('#node-label').val()
  59. });
  60. }
  61. catch (err) {
  62. alert(err);
  63. }
  64. });
  65. $('#node-update').click(function () {
  66. try {
  67. nodes.update({
  68. id: $('#node-id').val(),
  69. label: $('#node-label').val()
  70. });
  71. }
  72. catch (err) {
  73. alert(err);
  74. }
  75. });
  76. $('#node-remove').click(function () {
  77. try {
  78. var id = $('#node-id').val();
  79. nodes.remove(id);
  80. }
  81. catch (err) {
  82. alert(err);
  83. }
  84. });
  85. // attach actions to the edge buttons
  86. $('#edge-add').click(function () {
  87. try {
  88. edges.add({
  89. id: $('#edge-id').val(),
  90. from: $('#edge-from').val(),
  91. to: $('#edge-to').val()
  92. });
  93. }
  94. catch (err) {
  95. alert(err);
  96. }
  97. });
  98. $('#edge-update').click(function () {
  99. try {
  100. edges.update({
  101. id: $('#edge-id').val(),
  102. from: $('#edge-from').val(),
  103. to: $('#edge-to').val()
  104. });
  105. }
  106. catch (err) {
  107. alert(err);
  108. }
  109. });
  110. $('#edge-remove').click(function () {
  111. try {
  112. var id = $('#edge-id').val();
  113. edges.remove(id);
  114. }
  115. catch (err) {
  116. alert(err);
  117. }
  118. });
  119. // create an array with nodes
  120. nodes = new vis.DataSet();
  121. nodes.add([
  122. {id: '1', label: 'Node 1'},
  123. {id: '2', label: 'Node 2'},
  124. {id: '3', label: 'Node 3'},
  125. {id: '4', label: 'Node 4'},
  126. {id: '5', label: 'Node 5'}
  127. ]);
  128. // create an array with edges
  129. edges = new vis.DataSet();
  130. edges.add([
  131. {id: '1', from: '1', to: '2'},
  132. {id: '2', from: '1', to: '3'},
  133. {id: '3', from: '2', to: '4'},
  134. {id: '4', from: '2', to: '5'}
  135. ]);
  136. // subscribe to any changes of the dataset
  137. nodes.on('*', function () {
  138. $('#nodes').html(toJSON(nodes.get()));
  139. });
  140. edges.on('*', function () {
  141. $('#edges').html(toJSON(edges.get()));
  142. });
  143. // create a graph
  144. var container = $('#graph').get(0);
  145. var data = {
  146. nodes: nodes,
  147. edges: edges
  148. };
  149. var options = {};
  150. graph = new vis.Graph(container, data, options);
  151. });
  152. </script>
  153. </head>
  154. <body>
  155. <p>
  156. This example demonstrates dynamically adding, updating and removing nodes
  157. and edges using a DataSet.
  158. </p>
  159. <h1>Adjust</h1>
  160. <table>
  161. <tr>
  162. <td>
  163. <h2>Node</h2>
  164. <table>
  165. <tr>
  166. <td></td>
  167. <td><label for="node-id">Id</label></td>
  168. <td><input id="node-id" type="text" value="6"></td>
  169. </tr>
  170. <tr>
  171. <td></td>
  172. <td><label for="node-label">Label</label></td>
  173. <td><input id="node-label" type="text" value="Node 6"></td>
  174. </tr>
  175. <tr>
  176. <td></td>
  177. <td>Action</td>
  178. <td>
  179. <button id="node-add">Add</button>
  180. <button id="node-update">Update</button>
  181. <button id="node-remove">Remove</button>
  182. </td>
  183. </tr>
  184. </table>
  185. </td>
  186. <td>
  187. <h2>Edge</h2>
  188. <table>
  189. <tr>
  190. <td></td>
  191. <td><label for="edge-id">Id</label></td>
  192. <td><input id="edge-id" type="text" value="5"></td>
  193. </tr>
  194. <tr>
  195. <td></td>
  196. <td><label for="edge-from">From</label></td>
  197. <td><input id="edge-from" type="text" value="3"></td>
  198. </tr>
  199. <tr>
  200. <td></td>
  201. <td><label for="edge-to">To</label></td>
  202. <td><input id="edge-to" type="text" value="4"></td>
  203. </tr>
  204. <tr>
  205. <td></td>
  206. <td>Action</td>
  207. <td>
  208. <button id="edge-add">Add</button>
  209. <button id="edge-update">Update</button>
  210. <button id="edge-remove">Remove</button>
  211. </td>
  212. </tr>
  213. </table>
  214. </td>
  215. </tr>
  216. </table>
  217. <h1>View</h1>
  218. <table class="view">
  219. <colgroup>
  220. <col width="25%">
  221. <col width="25%">
  222. <col width="50%">
  223. </colgroup>
  224. <tr>
  225. <td>
  226. <h2>Nodes</h2>
  227. <pre id="nodes"></pre>
  228. </td>
  229. <td>
  230. <h2>Edges</h2>
  231. <pre id="edges"></pre>
  232. </td>
  233. <td>
  234. <h2>Graph</h2>
  235. <div id="graph"></div>
  236. </td>
  237. </tr>
  238. </table>
  239. </body>
  240. </html>