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.

206 lines
4.8 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. height: 100%;
  22. }
  23. #error {
  24. color: red;
  25. }
  26. #data {
  27. width: 100%;
  28. height: 100%;
  29. border: 1px solid #d3d3d3;
  30. }
  31. #mygraph {
  32. float: left;
  33. width: 100%;
  34. height: 100%;
  35. border: 1px solid #d3d3d3;
  36. }
  37. textarea.example {
  38. display: none;
  39. }
  40. </style>
  41. </head>
  42. <body>
  43. <table id="frame">
  44. <col width="50%">
  45. <col width="50%">
  46. <tr>
  47. <td colspan="2" style="height: 50px;">
  48. <h1>Directed graph from data in DOT language</h1>
  49. <div>
  50. <a href="javascript: drawExample('example1')">example 1</a>
  51. <a href="javascript: drawExample('example2')">example 2</a>
  52. <a href="javascript: drawExample('example3')">example 3</a>
  53. </div>
  54. <div>
  55. <br>
  56. <button id="draw">Draw</button>
  57. <span id="error"></span>
  58. </div>
  59. </td>
  60. </tr>
  61. <tr>
  62. <td>
  63. <textarea id="data"></textarea>
  64. </td>
  65. <td>
  66. <div id="mygraph"></div>
  67. </td>
  68. </tr>
  69. </table>
  70. <script type="text/javascript">
  71. var graph, data;
  72. var btnDraw = document.getElementById('draw');
  73. var txtData = document.getElementById('data');
  74. var txtError = document.getElementById('error');
  75. btnDraw.onclick = draw;
  76. // resize the graph when window resizes
  77. window.onresize = function () {
  78. graph.redraw()
  79. };
  80. // parse and draw the data
  81. function draw () {
  82. try {
  83. txtError.innerHTML = '';
  84. // Provide a string with data in DOT language
  85. data = {
  86. dot: txtData.value
  87. };
  88. // create a graph
  89. var container = document.getElementById('mygraph');
  90. var options = {};
  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 [shape=circle fontSize=16]
  119. edge [length=100, color=gray, fontColor=black]
  120. A -> A[label=0.5];
  121. B -> B[label=1.2] -> C[label=0.7] -- A;
  122. B -> D;
  123. D -> B;
  124. D -> C;
  125. D -> E[label=0.2];
  126. F -> F;
  127. A [
  128. fontColor=white,
  129. color=red,
  130. ]
  131. }
  132. </textarea>
  133. <textarea id="example2" class="example">
  134. digraph topology
  135. {
  136. node[shape=circle fontSize=12]
  137. edge[length=170 fontSize=12]
  138. "10.0.255.1" -> "10.0.255.3"[label="1.000"];
  139. "10.0.255.1" -> "10.0.255.2"[label="1.000"];
  140. "10.0.255.1" -> "10.0.255.2"[label="1.000"];
  141. "10.0.255.1" -> "10.0.255.3"[label="1.000"];
  142. "10.0.255.2" -> "10.0.255.1"[label="1.000"];
  143. "10.0.255.2" -> "10.0.255.3"[label="1.000"];
  144. "10.0.255.3" -> "10.0.255.1"[label="1.000"];
  145. "10.0.255.3" -> "10.0.255.2"[label="1.000"];
  146. "10.0.255.3" -> "10.0.3.0/24"[label="HNA", shape=solid];
  147. "10.0.3.0/24"[shape=rect];
  148. "10.0.255.2" -> "10.0.2.0/24"[label="HNA"];
  149. "10.0.2.0/24"[shape=rect];
  150. "10.0.255.1" -> "10.0.1.0/24"[label="HNA"];
  151. "10.0.1.0/24"[shape=rect];
  152. }
  153. </textarea>
  154. <textarea id="example3" class="example">
  155. digraph G {
  156. // note: not all attributes are recognized and supported by Network
  157. // unrecognized attributes are ignored
  158. node[width=.25,height=.375,fontsize=15]
  159. node [shape=filled fillcolor=#F1AAF0]
  160. 0-> 0 ;
  161. 1-> 1 ;
  162. 2-> 2 ;
  163. 3-> 3 ;
  164. 4-> 4 ;
  165. 5-> 5 ;
  166. 6-> 6 ;
  167. 7-> 5 ;
  168. 8-> 8 ;
  169. 9-> 9 ;
  170. 10-> 10 ;
  171. 11-> 10 ;
  172. 12-> 12 ;
  173. 13-> 5 ;
  174. 14-> 10 ;
  175. 15-> 0 ;
  176. }
  177. </textarea>
  178. <script>
  179. // draw example1 now
  180. drawExample('example1');
  181. </script>
  182. </body>
  183. </html>