user-defined.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <!doctype html>
  2. <meta charset="utf-8">
  3. <title>Dagre D3 Demo: User-defined Shapes and Arrows</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. <style id="css">
  9. body {
  10. font: 300 14px 'Helvetica Neue', Helvetica;
  11. }
  12. .node rect,
  13. .node circle,
  14. .node ellipse,
  15. .node polygon {
  16. stroke: #333;
  17. fill: #fff;
  18. stroke-width: 1.5px;
  19. }
  20. .edgePath path.path {
  21. stroke: #333;
  22. fill: none;
  23. stroke-width: 1.5px;
  24. }
  25. </style>
  26. <h1>Dagre D3 Demo: User-defined Shapes and Arrows</h1>
  27. <svg width=960 height=600><g/></svg>
  28. <section>
  29. <p>An example that shows how to create and use user-defined shapes and arrows.
  30. </section>
  31. <script id="js">
  32. // Create a new directed graph
  33. var g = new graphlib.Graph().setGraph({});
  34. g.setNode("house", { shape: "house", label: "house" });
  35. g.setNode("rect", { shape: "rect" });
  36. g.setEdge("house", "rect", { arrowhead: "hollowPoint" });
  37. var svg = d3.select("svg"),
  38. inner = svg.select("g");
  39. // Set up zoom support
  40. var zoom = d3.zoom().on("zoom", function() {
  41. inner.attr("transform", d3.event.transform);
  42. });
  43. svg.call(zoom);
  44. // Create the renderer
  45. var render = new dagreD3.render();
  46. // Add our custom shape (a house)
  47. render.shapes().house = function(parent, bbox, node) {
  48. var w = bbox.width,
  49. h = bbox.height,
  50. points = [
  51. { x: 0, y: 0 },
  52. { x: w, y: 0 },
  53. { x: w, y: -h },
  54. { x: w/2, y: -h * 3/2 },
  55. { x: 0, y: -h }
  56. ];
  57. shapeSvg = parent.insert("polygon", ":first-child")
  58. .attr("points", points.map(function(d) { return d.x + "," + d.y; }).join(" "))
  59. .attr("transform", "translate(" + (-w/2) + "," + (h * 3/4) + ")");
  60. node.intersect = function(point) {
  61. return dagreD3.intersect.polygon(node, points, point);
  62. };
  63. return shapeSvg;
  64. };
  65. // Add our custom arrow (a hollow-point)
  66. render.arrows().hollowPoint = function normal(parent, id, edge, type) {
  67. var marker = parent.append("marker")
  68. .attr("id", id)
  69. .attr("viewBox", "0 0 10 10")
  70. .attr("refX", 9)
  71. .attr("refY", 5)
  72. .attr("markerUnits", "strokeWidth")
  73. .attr("markerWidth", 8)
  74. .attr("markerHeight", 6)
  75. .attr("orient", "auto");
  76. var path = marker.append("path")
  77. .attr("d", "M 0 0 L 10 5 L 0 10 z")
  78. .style("stroke-width", 1)
  79. .style("stroke-dasharray", "1,0")
  80. .style("fill", "#fff")
  81. .style("stroke", "#333");
  82. dagreD3.util.applyStyle(path, edge[type + "Style"]);
  83. };
  84. // Run the renderer. This is what draws the final graph.
  85. render(inner, g);
  86. // Center the graph
  87. var initialScale = 0.75;
  88. svg.call(zoom.transform, d3.zoomIdentity.translate((svg.attr("width") - g.graph().width * initialScale) / 2, 20).scale(initialScale));
  89. svg.attr('height', g.graph().height * initialScale + 40);
  90. </script>
  91. <script src="demo.js"></script>