interactive-demo.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <!doctype html>
  2. <meta charset="utf-8">
  3. <title>Dagre Interactive Demo</title>
  4. <script src="./graphlib-dot.js"></script>
  5. <script src="../../node_modules/d3/build/d3.js"></script>
  6. <script src="../dagre-d3.js"></script>
  7. <style>
  8. svg {
  9. border: 1px solid #999;
  10. overflow: hidden;
  11. }
  12. .node {
  13. white-space: nowrap;
  14. }
  15. .node rect,
  16. .node circle,
  17. .node ellipse {
  18. stroke: #333;
  19. fill: #fff;
  20. stroke-width: 1.5px;
  21. }
  22. .cluster rect {
  23. stroke: #333;
  24. fill: #000;
  25. fill-opacity: 0.1;
  26. stroke-width: 1.5px;
  27. }
  28. .edgePath path.path {
  29. stroke: #333;
  30. stroke-width: 1.5px;
  31. fill: none;
  32. }
  33. </style>
  34. <style>
  35. h1, h2 {
  36. color: #333;
  37. }
  38. textarea {
  39. width: 800px;
  40. }
  41. label {
  42. margin-top: 1em;
  43. display: block;
  44. }
  45. .error {
  46. color: red;
  47. }
  48. </style>
  49. <body onLoad="tryDraw();">
  50. <h1>Dagre Interactive Demo</h1>
  51. <h2>Input</h2>
  52. <form>
  53. <label for="inputGraph">Graphviz Definition</label>
  54. <textarea id="inputGraph" rows="5" style="display: block" onKeyUp="tryDraw();">
  55. /* Example */
  56. digraph {
  57. /* Note: HTML labels do not work in IE, which lacks support for &lt;foreignObject&gt; tags. */
  58. node [rx=5 ry=5 labelStyle="font: 300 14px 'Helvetica Neue', Helvetica"]
  59. edge [labelStyle="font: 300 14px 'Helvetica Neue', Helvetica"]
  60. A [labelType="html"
  61. label="A <span style='font-size:32px'>Big</span> <span style='color:red;'>HTML</span> Source!"];
  62. C;
  63. E [label="Bold Red Sink" style="fill: #f77; font-weight: bold"];
  64. A -&gt; B -&gt; C;
  65. B -&gt; D [label="A blue label" labelStyle="fill: #55f; font-weight: bold;"];
  66. D -&gt; E [label="A thick red edge" style="stroke: #f77; stroke-width: 2px;" arrowheadStyle="fill: #f77"];
  67. C -&gt; E;
  68. A -&gt; D [labelType="html" label="A multi-rank <span style='color:blue;'>HTML</span> edge!"];
  69. }
  70. </textarea>
  71. <a id="graphLink">Link for this graph</a>
  72. </form>
  73. <h2>Graph Visualization</h2>
  74. <svg width=800 height=600>
  75. <g/>
  76. </svg>
  77. <script>
  78. // Input related code goes here
  79. function graphToURL() {
  80. var elems = [window.location.protocol, '//',
  81. window.location.host,
  82. window.location.pathname,
  83. '?'];
  84. var queryParams = [];
  85. if (debugAlignment) {
  86. queryParams.push('alignment=' + debugAlignment);
  87. }
  88. queryParams.push('graph=' + encodeURIComponent(inputGraph.value));
  89. elems.push(queryParams.join('&'));
  90. return elems.join('');
  91. }
  92. var inputGraph = document.querySelector("#inputGraph");
  93. var graphLink = d3.select("#graphLink");
  94. var oldInputGraphValue;
  95. var graphRE = /[?&]graph=([^&]+)/;
  96. var graphMatch = window.location.search.match(graphRE);
  97. if (graphMatch) {
  98. inputGraph.value = decodeURIComponent(graphMatch[1]);
  99. }
  100. var debugAlignmentRE = /[?&]alignment=([^&]+)/;
  101. var debugAlignmentMatch = window.location.search.match(debugAlignmentRE);
  102. var debugAlignment;
  103. if (debugAlignmentMatch) debugAlignment = debugAlignmentMatch[1];
  104. // Set up zoom support
  105. var svg = d3.select("svg"),
  106. inner = d3.select("svg g"),
  107. zoom = d3.zoom().on("zoom", function() {
  108. inner.attr("transform", d3.event.transform);
  109. });
  110. svg.call(zoom);
  111. // Create and configure the renderer
  112. var render = dagreD3.render();
  113. var g;
  114. function tryDraw() {
  115. if (oldInputGraphValue !== inputGraph.value) {
  116. inputGraph.setAttribute("class", "");
  117. oldInputGraphValue = inputGraph.value;
  118. try {
  119. g = graphlibDot.read(inputGraph.value);
  120. } catch (e) {
  121. inputGraph.setAttribute("class", "error");
  122. throw e;
  123. }
  124. // Save link to new graph
  125. graphLink.attr("href", graphToURL());
  126. // Set margins, if not present
  127. if (!g.graph().hasOwnProperty("marginx") &&
  128. !g.graph().hasOwnProperty("marginy")) {
  129. g.graph().marginx = 20;
  130. g.graph().marginy = 20;
  131. }
  132. g.graph().transition = function(selection) {
  133. return selection.transition().duration(500);
  134. };
  135. // Render the graph into svg g
  136. d3.select("svg g").call(render, g);
  137. }
  138. }
  139. </script>