clusters.html 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <!doctype html>
  2. <meta charset="utf-8">
  3. <title>Dagre D3 Demo: Clusters</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: Clusters</h1>
  9. <style id="css">
  10. .clusters rect {
  11. fill: #00ffd0;
  12. stroke: #999;
  13. stroke-width: 1.5px;
  14. }
  15. text {
  16. font-weight: 300;
  17. font-family: "Helvetica Neue", Helvetica, Arial, sans-serf;
  18. font-size: 14px;
  19. }
  20. .node rect {
  21. stroke: #999;
  22. fill: #fff;
  23. stroke-width: 1.5px;
  24. }
  25. .edgePath path {
  26. stroke: #333;
  27. stroke-width: 1.5px;
  28. }
  29. </style>
  30. <svg id="svg-canvas" width=960 height=600></svg>
  31. <section>
  32. <p>An example of visualizing clusters. This example shows
  33. how clusters can be applied to a rendered graph.
  34. </section>
  35. <script id="js">
  36. // Create the input graph
  37. var g = new graphlib.Graph({compound:true})
  38. .setGraph({})
  39. .setDefaultEdgeLabel(function() { return {}; });
  40. // Here we're setting the nodes
  41. g.setNode('a', {label: 'A'});
  42. g.setNode('b', {label: 'B'});
  43. g.setNode('c', {label: 'C'});
  44. g.setNode('d', {label: 'D'});
  45. g.setNode('e', {label: 'E'});
  46. g.setNode('f', {label: 'F'});
  47. g.setNode('g', {label: 'G'});
  48. g.setNode('group', {label: 'Group', clusterLabelPos: 'top', style: 'fill: #d3d7e8'});
  49. g.setNode('top_group', {label: 'Top Group', clusterLabelPos: 'bottom', style: 'fill: #ffd47f'});
  50. g.setNode('bottom_group', {label: 'Bottom Group', style: 'fill: #5f9488'});
  51. // Set the parents to define which nodes belong to which cluster
  52. g.setParent('top_group', 'group');
  53. g.setParent('bottom_group', 'group');
  54. g.setParent('b', 'top_group');
  55. g.setParent('c', 'bottom_group');
  56. g.setParent('d', 'bottom_group');
  57. g.setParent('e', 'bottom_group');
  58. g.setParent('f', 'bottom_group');
  59. // Set up edges, no special attributes.
  60. g.setEdge('a', 'b');
  61. g.setEdge('b', 'c');
  62. g.setEdge('b', 'd');
  63. g.setEdge('b', 'e');
  64. g.setEdge('b', 'f');
  65. g.setEdge('b', 'g');
  66. g.nodes().forEach(function(v) {
  67. var node = g.node(v);
  68. // Round the corners of the nodes
  69. node.rx = node.ry = 5;
  70. });
  71. // Create the renderer
  72. var render = new dagreD3.render();
  73. // Set up an SVG group so that we can translate the final graph.
  74. var svg = d3.select("svg"),
  75. svgGroup = svg.append("g");
  76. // Run the renderer. This is what draws the final graph.
  77. render(d3.select("svg g"), g);
  78. // Center the graph
  79. var xCenterOffset = (svg.attr("width") - g.graph().width) / 2;
  80. svgGroup.attr("transform", "translate(" + xCenterOffset + ", 20)");
  81. svg.attr("height", g.graph().height + 40);
  82. </script>
  83. <script src="demo.js"></script>