hover.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <!doctype html>
  2. <meta charset="utf-8">
  3. <title>Dagre D3 Demo: Tooltip on Hover</title>
  4. <link rel="stylesheet" href="demo.css">
  5. <script src="../../node_modules/graphlibrary/dist/graphlib.js"></script>
  6. <script src="../../node_modules/d3/build/d3.js"></script>
  7. <script src="../dagre-d3.js"></script>
  8. <!-- Pull in JQuery dependencies -->
  9. <link rel="stylesheet" href="tipsy.css">
  10. <script src="./jquery-1.9.1.min.js"></script>
  11. <script src="tipsy.js"></script>
  12. <h1>Dagre D3 Demo: Tooltip on Hover</h1>
  13. <style id="css">
  14. text {
  15. font-weight: 300;
  16. font-family: "Helvetica Neue", Helvetica, Arial, sans-serf;
  17. font-size: 14px;
  18. }
  19. .node rect {
  20. stroke: #333;
  21. fill: #fff;
  22. }
  23. .edgePath path {
  24. stroke: #333;
  25. fill: #333;
  26. stroke-width: 1.5px;
  27. }
  28. .node text {
  29. pointer-events: none;
  30. }
  31. /* This styles the title of the tooltip */
  32. .tipsy .name {
  33. font-size: 1.5em;
  34. font-weight: bold;
  35. color: #60b1fc;
  36. margin: 0;
  37. }
  38. /* This styles the body of the tooltip */
  39. .tipsy .description {
  40. font-size: 1.2em;
  41. }
  42. </style>
  43. <svg width=960 height=600></svg>
  44. <section>
  45. <p>The TCP state diagram
  46. (<a href="http://www.rfc-editor.org/rfc/rfc793.txt">source: RFC 793</a>) with
  47. hover support. Uses <a href="http://bl.ocks.org/ilyabo/1373263">tipsy JS and CSS</a>
  48. for the tooltip.
  49. </section>
  50. <script id="js">
  51. // Create a new directed graph
  52. var g = new graphlib.Graph().setGraph({});
  53. // States and transitions from RFC 793
  54. var states = {
  55. CLOSED: {
  56. description: "represents no connection state at all.",
  57. style: "fill: #f77"
  58. },
  59. LISTEN: {
  60. description: "represents waiting for a connection request from any " +
  61. "remote TCP and port."
  62. },
  63. "SYN SENT": {
  64. description: "represents waiting for a matching connection " +
  65. "request after having sent a connection request."
  66. },
  67. "SYN RCVD": {
  68. description: "represents waiting for a confirming connection " +
  69. "request acknowledgment after having both received and sent a " +
  70. "connection request."
  71. },
  72. ESTAB: {
  73. description: "represents an open connection, data received " +
  74. "can be delivered to the user. The normal state for the data " +
  75. "transfer phase of the connection.",
  76. style: "fill: #7f7"
  77. },
  78. "FINWAIT-1": {
  79. description: "represents waiting for a connection termination " +
  80. "request from the remote TCP, or an acknowledgment of the " +
  81. "connection termination request previously sent."
  82. },
  83. "FINWAIT-2": {
  84. description: "represents waiting for a connection termination " +
  85. "request from the remote TCP."
  86. },
  87. "CLOSE WAIT": {
  88. description: "represents waiting for a connection termination " +
  89. "request from the local user."
  90. },
  91. CLOSING: {
  92. description: "represents waiting for a connection termination " +
  93. "request acknowledgment from the remote TCP."
  94. },
  95. "LAST-ACK": {
  96. description: "represents waiting for an acknowledgment of the " +
  97. "connection termination request previously sent to the remote " +
  98. "TCP (which includes an acknowledgment of its connection " +
  99. "termination request)."
  100. },
  101. "TIME WAIT": {
  102. description: "represents waiting for enough time to pass to be " +
  103. "sure the remote TCP received the acknowledgment of its " +
  104. "connection termination request."
  105. }
  106. };
  107. // Add states to the graph, set labels, and style
  108. Object.keys(states).forEach(function(state) {
  109. var value = states[state];
  110. value.label = state;
  111. value.rx = value.ry = 5;
  112. g.setNode(state, value);
  113. });
  114. // Set up the edges
  115. g.setEdge("CLOSED", "LISTEN", { label: "open" });
  116. g.setEdge("LISTEN", "SYN RCVD", { label: "rcv SYN" });
  117. g.setEdge("LISTEN", "SYN SENT", { label: "send" });
  118. g.setEdge("LISTEN", "CLOSED", { label: "close" });
  119. g.setEdge("SYN RCVD", "FINWAIT-1", { label: "close" });
  120. g.setEdge("SYN RCVD", "ESTAB", { label: "rcv ACK of SYN" });
  121. g.setEdge("SYN SENT", "SYN RCVD", { label: "rcv SYN" });
  122. g.setEdge("SYN SENT", "ESTAB", { label: "rcv SYN, ACK" });
  123. g.setEdge("SYN SENT", "CLOSED", { label: "close" });
  124. g.setEdge("ESTAB", "FINWAIT-1", { label: "close" });
  125. g.setEdge("ESTAB", "CLOSE WAIT", { label: "rcv FIN" });
  126. g.setEdge("FINWAIT-1", "FINWAIT-2", { label: "rcv ACK of FIN" });
  127. g.setEdge("FINWAIT-1", "CLOSING", { label: "rcv FIN" });
  128. g.setEdge("CLOSE WAIT", "LAST-ACK", { label: "close" });
  129. g.setEdge("FINWAIT-2", "TIME WAIT", { label: "rcv FIN" });
  130. g.setEdge("CLOSING", "TIME WAIT", { label: "rcv ACK of FIN" });
  131. g.setEdge("LAST-ACK", "CLOSED", { label: "rcv ACK of FIN" });
  132. g.setEdge("TIME WAIT", "CLOSED", { label: "timeout=2MSL" });
  133. // Create the renderer
  134. var render = new dagreD3.render();
  135. // Set up an SVG group so that we can translate the final graph.
  136. var svg = d3.select("svg"),
  137. inner = svg.append("g");
  138. // Set up zoom support
  139. var zoom = d3.zoom()
  140. .on("zoom", function() {
  141. inner.attr("transform", d3.event.transform);
  142. });
  143. svg.call(zoom);
  144. // Simple function to style the tooltip for the given node.
  145. var styleTooltip = function(name, description) {
  146. return "<p class='name'>" + name + "</p><p class='description'>" + description + "</p>";
  147. };
  148. // Run the renderer. This is what draws the final graph.
  149. render(inner, g);
  150. inner.selectAll("g.node")
  151. .attr("title", function(v) { return styleTooltip(v, g.node(v).description) })
  152. .each(function(v) { $(this).tipsy({ gravity: "w", opacity: 1, html: true }); });
  153. // Center the graph
  154. var initialScale = 0.75;
  155. svg.call(zoom.transform, d3.zoomIdentity.translate((svg.attr("width") - g.graph().width * initialScale) / 2, 20).scale(initialScale));
  156. svg.attr('height', g.graph().height * initialScale + 40);
  157. </script>
  158. <script src="demo.js"></script>