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.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. }
  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=white,
  131. color=red,
  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", shape=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 [shape=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>