svg-labels.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <!doctype html>
  2. <meta charset="utf-8">
  3. <title>Dagre D3 Demo: SVG Labels</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. <h1>Dagre D3 Demo: SVG Labels</h1>
  9. <style id="css">
  10. text {
  11. font-weight: 300;
  12. font-family: "Helvetica Neue", Helvetica, Arial, sans-serf;
  13. font-size: 14px;
  14. }
  15. .node rect {
  16. stroke: #999;
  17. fill: #fff;
  18. stroke-width: 1.5px;
  19. }
  20. .edgePath path {
  21. stroke: #333;
  22. stroke-width: 1.5px;
  23. }
  24. </style>
  25. <svg id="svg-canvas" width=960 height=600></svg>
  26. <section>
  27. <p>An example of adding SVG labels. This allows the user to add any svg
  28. elements to the label. This allows for links to works in IE.</p>
  29. </section>
  30. <script id="js">
  31. // Create the input graph
  32. var g = new graphlib.Graph()
  33. .setGraph({})
  34. .setDefaultEdgeLabel(function() { return {}; });
  35. // Create the SVG label to pass in
  36. // Must create in SVG namespace
  37. // http://stackoverflow.com/questions/7547117/add-a-new-line-in-svg-bug-cannot-see-the-line
  38. // This mimics the same way string labels get added in Dagre-D3
  39. svg_label = document.createElementNS('http://www.w3.org/2000/svg', 'text');
  40. tspan = document.createElementNS('http://www.w3.org/2000/svg','tspan');
  41. tspan.setAttributeNS('http://www.w3.org/XML/1998/namespace', 'xml:space', 'preserve');
  42. tspan.setAttribute('dy', '1em');
  43. tspan.setAttribute('x', '1');
  44. link = document.createElementNS('http://www.w3.org/2000/svg', 'a');
  45. link.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', 'http://google.com/');
  46. link.setAttribute('target', '_blank');
  47. link.textContent = 'IE Capable link';
  48. tspan.appendChild(link);
  49. svg_label.appendChild(tspan);
  50. g.setNode(0, { label: svg_label, labelType: 'svg' });
  51. g.setNode(1, { label: "A" });
  52. g.setNode(2, { label: "B" });
  53. g.nodes().forEach(function(v) {
  54. var node = g.node(v);
  55. // Round the corners of the nodes
  56. node.rx = node.ry = 5;
  57. });
  58. svg_edge_label = document.createElementNS('http://www.w3.org/2000/svg', 'text');
  59. edge_tspan = document.createElementNS('http://www.w3.org/2000/svg','tspan');
  60. edge_tspan.setAttributeNS('http://www.w3.org/XML/1998/namespace', 'xml:space', 'preserve');
  61. edge_tspan.setAttribute('dy', '1em');
  62. edge_tspan.setAttribute('x', '1');
  63. edge_link = document.createElementNS('http://www.w3.org/2000/svg', 'a');
  64. edge_link.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', 'http://google.com/');
  65. edge_link.setAttribute('target', '_blank');
  66. edge_link.textContent = 'IE Capable Edge link';
  67. edge_tspan.appendChild(edge_link);
  68. svg_edge_label.appendChild(edge_tspan);
  69. // Set up edges, no special attributes.
  70. g.setEdge(0, 1, { labelType: "svg", label: svg_edge_label });
  71. g.setEdge(0, 2);
  72. g.setEdge(1, 2);
  73. // Create the renderer
  74. var render = new dagreD3.render();
  75. // Set up an SVG group so that we can translate the final graph.
  76. var svg = d3.select("svg"),
  77. svgGroup = svg.append("g");
  78. // Run the renderer. This is what draws the final graph.
  79. render(d3.select("svg g"), g);
  80. // Center the graph
  81. var xCenterOffset = (svg.attr("width") - g.graph().width) / 2;
  82. svgGroup.attr("transform", "translate(" + xCenterOffset + ", 20)");
  83. svg.attr("height", g.graph().height + 40);
  84. </script>
  85. <script src="demo.js"></script>