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.

212 lines
5.1 KiB

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Graph | DOT language playground</title>
  5. <script type="text/javascript" src="../../vis.js"></script>
  6. <style type="text/css">
  7. body, html {
  8. font: 10pt sans;
  9. width: 100%;
  10. height: 100%;
  11. padding: 0;
  12. margin: 0;
  13. color: #4d4d4d;
  14. }
  15. #frame {
  16. width: 100%;
  17. height: 99%;
  18. }
  19. #frame td {
  20. padding: 10px;
  21. }
  22. #error {
  23. color: red;
  24. }
  25. #data {
  26. float: left;
  27. width: 100%;
  28. height: 100%;
  29. border: 1px solid #d3d3d3;
  30. }
  31. #graph {
  32. float: left;
  33. width: 100%;
  34. height: 100%;
  35. }
  36. textarea.example {
  37. display: none;
  38. }
  39. </style>
  40. </head>
  41. <body>
  42. <table id="frame">
  43. <col width="50%">
  44. <col width="50%">
  45. <tr>
  46. <td colspan="2" style="height: 50px;">
  47. <h1>Directed graph from data in DOT language</h1>
  48. <div>
  49. <a href="javascript: drawExample('example1')">example 1</a>
  50. <a href="javascript: drawExample('example2')">example 2</a>
  51. <a href="javascript: drawExample('example3')">example 3</a>
  52. </div>
  53. <div>
  54. <br>
  55. <button id="draw">Draw</button>
  56. <span id="error"></span>
  57. </div>
  58. </td>
  59. </tr>
  60. <tr>
  61. <td>
  62. <textarea id="data"></textarea>
  63. </td>
  64. <td>
  65. <div id="graph"></div>
  66. </td>
  67. </tr>
  68. </table>
  69. <script type="text/javascript">
  70. var graph, data;
  71. var btnDraw = document.getElementById('draw');
  72. var txtData = document.getElementById('data');
  73. var txtError = document.getElementById('error');
  74. btnDraw.onclick = draw;
  75. // resize the graph when window resizes
  76. window.onresize = function () {
  77. graph.redraw()
  78. };
  79. // parse and draw the data
  80. function draw () {
  81. try {
  82. // create a graph
  83. graph = new vis.Graph(document.getElementById('graph'));
  84. txtError.innerHTML = '';
  85. // parse the data from DOT-notation
  86. data = vis.Graph.util.DOTToGraph(txtData.value);
  87. // draw the data in the graph view
  88. var options = data.options;
  89. options.width = '100%';
  90. //options.links.length = options.links.length;
  91. options.height = '100%';
  92. //options.nodes.radius = 20;
  93. //options.links.width = 2;
  94. graph.draw(data.nodes, data.edges, options);
  95. }
  96. catch (err) {
  97. // set the cursor at the position where the error occurred
  98. var match = /\(char (.*)\)/.exec(err);
  99. if (match) {
  100. var pos = Number(match[1]);
  101. if(txtData.setSelectionRange) {
  102. txtData.focus();
  103. txtData.setSelectionRange(pos, pos);
  104. }
  105. }
  106. // show an error message
  107. txtError.innerHTML = err.toString();
  108. }
  109. }
  110. /**
  111. * Draw an example
  112. * @param {String} id HTML id of the textarea containing the example code
  113. */
  114. function drawExample(id) {
  115. txtData.value = document.getElementById(id).value;
  116. draw();
  117. }
  118. </script>
  119. <textarea id="example1" class="example">
  120. digraph {
  121. node [style=circle fontSize=16]
  122. edge [length=100, color=gray, fontColor=black]
  123. A -> A[text=0.5];
  124. B -> B[text=1.2] -> C[text=0.7] -- A;
  125. B -> D;
  126. D -> B;
  127. D -> C;
  128. D -> E[text=0.2];
  129. F -> F;
  130. A [
  131. fontColor=red,
  132. borderColor=red,
  133. backgroundColor=white,
  134. highlightColor=white
  135. ]
  136. }
  137. </textarea>
  138. <textarea id="example2" class="example">
  139. digraph topology
  140. {
  141. node[shape=circle fontSize=12]
  142. edge[length=170 fontSize=12]
  143. "10.0.255.1" -> "10.0.255.3"[label="1.000"];
  144. "10.0.255.1" -> "10.0.255.2"[label="1.000"];
  145. "10.0.255.1" -> "10.0.255.2"[label="1.000"];
  146. "10.0.255.1" -> "10.0.255.3"[label="1.000"];
  147. "10.0.255.2" -> "10.0.255.1"[label="1.000"];
  148. "10.0.255.2" -> "10.0.255.3"[label="1.000"];
  149. "10.0.255.3" -> "10.0.255.1"[label="1.000"];
  150. "10.0.255.3" -> "10.0.255.2"[label="1.000"];
  151. "10.0.255.3" -> "10.0.3.0/24"[label="HNA", style=solid];
  152. "10.0.3.0/24"[shape=rect];
  153. "10.0.255.2" -> "10.0.2.0/24"[label="HNA"];
  154. "10.0.2.0/24"[shape=rect];
  155. "10.0.255.1" -> "10.0.1.0/24"[label="HNA"];
  156. "10.0.1.0/24"[shape=rect];
  157. }
  158. </textarea>
  159. <textarea id="example3" class="example">
  160. digraph G {
  161. // note: not all attributes are recognized and supported by Network
  162. // unrecognized attributes are ignored
  163. node[width=.25,height=.375,fontsize=15]
  164. node [style=filled fillcolor=#F1AAF0]
  165. 0-> 0 ;
  166. 1-> 1 ;
  167. 2-> 2 ;
  168. 3-> 3 ;
  169. 4-> 4 ;
  170. 5-> 5 ;
  171. 6-> 6 ;
  172. 7-> 5 ;
  173. 8-> 8 ;
  174. 9-> 9 ;
  175. 10-> 10 ;
  176. 11-> 10 ;
  177. 12-> 12 ;
  178. 13-> 5 ;
  179. 14-> 10 ;
  180. 15-> 0 ;
  181. }
  182. </textarea>
  183. <script>
  184. // draw example1 now
  185. drawExample('example1');
  186. </script>
  187. </body>
  188. </html>