dagre-d3
repository·master·Indexed 25 days ago
https://github.com/dagrejs/dagre-d3A D3-based rendering library for visualizing directed graphs laid out by the dagre engine. It provides a front-end to the dagre layout engine, converting graph data into visual SVG elements. Key features include support for custom node shapes, cluster boundaries via createClusters(), HTML-based labels using foreignObject, and configurable arrowhead styles (normal, vee, undirected). The library exports modules for graphlib, dagre, and a customizable render function.
What's inside dagre-d3
- dagre-d3 is a D3-based renderer for dagre. While dagre handles the client-side layout of directed graphs, dagre-d3 provides the actual rendering layer using the D3 library. It acts as a front-end to the dagre layout engine.
Import the dagre-d3 library
masterThedagre-d3package exports several modules required to perform graph layout and D3-based rendering. The primary entry point provides access todagre(the layout engine),graphlib(graph data structures),render(the D3 renderer), and utility modules.Render a graph using render()
masterThe
renderfunction is the primary entry point for drawing a graph using D3. It returns a function that accepts an SVG element and a graph object (compatible withdagre). When called, it performs layout calculation viadagreand renders nodes, clusters, edge paths, and edge labels into the provided SVG.To use it, call
render()to get the rendering function, then pass your D3 selection and the graph object to that function.Customize the rendering engine with setter methods
masterThe function returned by
render()can be configured with custom implementations for various rendering components. These methods use a fluent interface: calling a setter returns the rendering function itself, allowing you to chain configurations.Available customization methods:
createNodes(value): Replace the node creation logic.createClusters(value): Replace the cluster creation logic.createEdgeLabels(value): Replace the edge label creation logic.createEdgePaths(value): Replace the edge path creation logic.shapes(value): Replace the node shapes definitions.arrows(value): Replace the arrow definitions.
Add HTML-based labels to graph elements with addHtmlLabel
masterThe
addHtmlLabelfunction allows you to render complex HTML content inside a graph node or edge by using aforeignObjectelement. This is useful when standard SVG text is insufficient for your labeling needs.To use this function, you must provide a
root(the D3 selection where the label will be appended) and anode(the graph element containing the label data).Label Data Types Supported:
- Function: A function that returns a D3 selection or DOM element. It will be inserted into the label container.
- Object: Assumed to be a DOM object. It will be inserted into the label container.
- Other (String/Number): The content will be set using
.html(), allowing for basic HTML strings.
Styling: Styles defined in
node.labelStyleare applied to the containerdivusingutil.applyStyle.Render cluster boundaries with createClusters()
masterUsecreateClusters(selection, g)to render subgraph/cluster boundaries in a D3 selection. This function identifies subgraphs within the dagre graphg, creates<g class="cluster">elements for each, appends arectfor the background, and adds a label group. It also applies styles and transitions defined on the graph nodes to the corresponding DOM elements.Select arrowhead styles in dagre-d3
masterThe
lib/arrows.jsmodule provides different arrowhead rendering functions that can be used to customize the appearance of edges. When configuring edges, you can choose from the following styles:normal(ordefault): A standard triangular arrowhead.vee: A 'V' shaped arrowhead.undirected: A simple line segment used for undirected edges (no arrowhead).
Each function accepts the following parameters:
parent: The D3 selection (usually the SVG container) where the marker will be appended.id: A unique identifier for the marker.edge: The edge object containing styling information.type: A string prefix used to look up style properties on the edge object (e.g., iftypeis'label', it looks forlabelStyleandlabelClass).
Configure edge styles for arrowheads
masterArrowhead styles are applied to edges using specific property keys on the edge object. The renderer looks for properties based on the
typeargument passed to the arrow function.If the
typeislabel, the renderer will look for:labelStyle: An object used to apply styles (viautil.applyStyle) to the arrowhead path.labelClass: A string representing a CSS class to be applied to the arrowhead path.
Example edge object structure:
const edge = { labelStyle: { stroke: 'red', 'stroke-width': 2 }, labelClass: 'my-custom-arrow-class' };Reference the dagre-d3 exported modules
masterThe following modules are exported by the
dagre-d3entry point:graphlib: Graph data structures and manipulation.dagre: The layout engine used to calculate node and edge positions.intersect: Intersection calculations.render: The D3-based renderer that converts graph data into visual elements.util: Utility functions.version: The current version of the package.
module.exports = { graphlib: require("./lib/graphlib"), dagre: require("./lib/dagre"), intersect: require("./lib/intersect"), render: require("./lib/render"), util: require("./lib/util"), version: require("./lib/version") };Configure node and edge attributes in the graph object
masterYou can control the appearance and spacing of nodes and edges by setting specific properties directly on the nodes and edges within your
dagregraph object before rendering.Node Attributes
label: The text to display (defaults to node ID if not provided).shape: The shape of the node (e.g.,"rect").padding: SetspaddingLeft,paddingRight,paddingTop, andpaddingBottomto the same value.paddingX: Sets bothpaddingLeftandpaddingRightto this value.paddingY: Sets bothpaddingTopandpaddingBottomto this value.paddingLeft,paddingRight,paddingTop,paddingBottom: Individual padding controls.rx,ry: Corner radius for shapes.
Edge Attributes
label: The text to display on the edge (defaults to empty string).arrowhead: The type of arrowhead to use (defaults to"normal").curve: The D3 curve function to use for the edge path (defaults tod3.curveLinear).