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.

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