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.

208 lines
4.9 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. #mygraph {
  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="mygraph"></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. txtError.innerHTML = '';
  83. // parse the data from DOT-notation
  84. data = vis.util.DOTToGraph(txtData.value);
  85. // create a graph
  86. var container = document.getElementById('mygraph');
  87. var options = {
  88. width: '100%',
  89. height: '100%'
  90. };
  91. graph = new vis.Graph(container, data, options);
  92. }
  93. catch (err) {
  94. // set the cursor at the position where the error occurred
  95. var match = /\(char (.*)\)/.exec(err);
  96. if (match) {
  97. var pos = Number(match[1]);
  98. if(txtData.setSelectionRange) {
  99. txtData.focus();
  100. txtData.setSelectionRange(pos, pos);
  101. }
  102. }
  103. // show an error message
  104. txtError.innerHTML = err.toString();
  105. }
  106. }
  107. /**
  108. * Draw an example
  109. * @param {String} id HTML id of the textarea containing the example code
  110. */
  111. function drawExample(id) {
  112. txtData.value = document.getElementById(id).value;
  113. draw();
  114. }
  115. </script>
  116. <textarea id="example1" class="example">
  117. digraph {
  118. node [style=circle fontSize=16]
  119. edge [length=100, color=gray, fontColor=black]
  120. A -> A[text=0.5];
  121. B -> B[text=1.2] -> C[text=0.7] -- A;
  122. B -> D;
  123. D -> B;
  124. D -> C;
  125. D -> E[text=0.2];
  126. F -> F;
  127. A [
  128. fontColor=red,
  129. borderColor=red,
  130. backgroundColor=white,
  131. highlightColor=white
  132. ]
  133. }
  134. </textarea>
  135. <textarea id="example2" class="example">
  136. digraph topology
  137. {
  138. node[shape=circle fontSize=12]
  139. edge[length=170 fontSize=12]
  140. "10.0.255.1" -> "10.0.255.3"[label="1.000"];
  141. "10.0.255.1" -> "10.0.255.2"[label="1.000"];
  142. "10.0.255.1" -> "10.0.255.2"[label="1.000"];
  143. "10.0.255.1" -> "10.0.255.3"[label="1.000"];
  144. "10.0.255.2" -> "10.0.255.1"[label="1.000"];
  145. "10.0.255.2" -> "10.0.255.3"[label="1.000"];
  146. "10.0.255.3" -> "10.0.255.1"[label="1.000"];
  147. "10.0.255.3" -> "10.0.255.2"[label="1.000"];
  148. "10.0.255.3" -> "10.0.3.0/24"[label="HNA", style=solid];
  149. "10.0.3.0/24"[shape=rect];
  150. "10.0.255.2" -> "10.0.2.0/24"[label="HNA"];
  151. "10.0.2.0/24"[shape=rect];
  152. "10.0.255.1" -> "10.0.1.0/24"[label="HNA"];
  153. "10.0.1.0/24"[shape=rect];
  154. }
  155. </textarea>
  156. <textarea id="example3" class="example">
  157. digraph G {
  158. // note: not all attributes are recognized and supported by Network
  159. // unrecognized attributes are ignored
  160. node[width=.25,height=.375,fontsize=15]
  161. node [style=filled fillcolor=#F1AAF0]
  162. 0-> 0 ;
  163. 1-> 1 ;
  164. 2-> 2 ;
  165. 3-> 3 ;
  166. 4-> 4 ;
  167. 5-> 5 ;
  168. 6-> 6 ;
  169. 7-> 5 ;
  170. 8-> 8 ;
  171. 9-> 9 ;
  172. 10-> 10 ;
  173. 11-> 10 ;
  174. 12-> 12 ;
  175. 13-> 5 ;
  176. 14-> 10 ;
  177. 15-> 0 ;
  178. }
  179. </textarea>
  180. <script>
  181. // draw example1 now
  182. drawExample('example1');
  183. </script>
  184. </body>
  185. </html>