dagre-d3

repository·master·Indexed 25 days ago

https://github.com/dagrejs/dagre-d3

A 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.

Tokens
1.5K
Snippets
1
Records
10
Agent score
84%

What's inside dagre-d3

  1. Import the dagre-d3 library

    master
    The dagre-d3 package exports several modules required to perform graph layout and D3-based rendering. The primary entry point provides access to dagre (the layout engine), graphlib (graph data structures), render (the D3 renderer), and utility modules.
  2. Render a graph using render()

    master

    The render function 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 with dagre). When called, it performs layout calculation via dagre and 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.

  3. Customize the rendering engine with setter methods

    master

    The 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.
  4. Add HTML-based labels to graph elements with addHtmlLabel

    master

    The addHtmlLabel function allows you to render complex HTML content inside a graph node or edge by using a foreignObject element. 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 a node (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.labelStyle are applied to the container div using util.applyStyle.

  5. Render cluster boundaries with createClusters()

    master
    Use createClusters(selection, g) to render subgraph/cluster boundaries in a D3 selection. This function identifies subgraphs within the dagre graph g, creates <g class="cluster"> elements for each, appends a rect for the background, and adds a label group. It also applies styles and transitions defined on the graph nodes to the corresponding DOM elements.
  6. Select arrowhead styles in dagre-d3

    master

    The lib/arrows.js module 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 (or default): 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., if type is 'label', it looks for labelStyle and labelClass).
  7. Configure edge styles for arrowheads

    master

    Arrowhead styles are applied to edges using specific property keys on the edge object. The renderer looks for properties based on the type argument passed to the arrow function.

    If the type is label, the renderer will look for:

    1. labelStyle: An object used to apply styles (via util.applyStyle) to the arrowhead path.
    2. 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'
    };
  8. Reference the dagre-d3 exported modules

    master

    The following modules are exported by the dagre-d3 entry 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")
    };
  9. Configure node and edge attributes in the graph object

    master

    You can control the appearance and spacing of nodes and edges by setting specific properties directly on the nodes and edges within your dagre graph 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: Sets paddingLeft, paddingRight, paddingTop, and paddingBottom to the same value.
    • paddingX: Sets both paddingLeft and paddingRight to this value.
    • paddingY: Sets both paddingTop and paddingBottom to 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 to d3.curveLinear).