Dagre Graph Layout (Legacy)

0.8.5 · abandoned · verified Sun Apr 19

Dagre is a JavaScript library designed for laying out directed graphs programmatically on the client-side. The package `dagre` at version 0.8.5, which is the latest available on the public npm registry under this name, was last published over six years ago and is effectively abandoned. While the GitHub repository shows recent activity and releases (v2.x, v3.x), these are published under the `@dagrejs/dagre` scoped package. Users attempting to install `dagre` directly will receive the unmaintained 0.8.5 version. This library's core functionality relies on `graphlib` for graph data structures, and it is often paired with rendering libraries like D3 for visualization. Its primary purpose is to automatically compute optimal node and edge positions in directed graphs, differentiating it from manual layout tools.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to create a graph using `graphlib`, add nodes and edges, apply the `dagre` layout, and then retrieve the calculated positions for nodes and bend points for edges. It uses CommonJS `require` for compatibility with `dagre@0.8.5`.

const dagre = require('dagre');
const graphlib = require('graphlib');

// Create a new directed graph
const g = new graphlib.Graph().setGraph({
  rankdir: 'LR' // Layout direction: Left to Right
}).setDefaultNodeLabel(() => ({})).setDefaultEdgeLabel(() => ({}));

// Add nodes to the graph
g.setNode('A', { label: 'Node A', width: 60, height: 30 });
g.setNode('B', { label: 'Node B', width: 60, height: 30 });
g.setNode('C', { label: 'Node C', width: 60, height: 30 });
g.setNode('D', { label: 'Node D', width: 60, height: 30 });

// Add edges to the graph
g.setEdge('A', 'B', { label: 'AB' });
g.setEdge('B', 'C', { label: 'BC' });
g.setEdge('A', 'D', { label: 'AD' });
g.setEdge('C', 'D', { label: 'CD' });

// Run the layout algorithm
dagre.layout(g);

// Log node positions
g.nodes().forEach(v => {
  const node = g.node(v);
  console.log(`Node ${v}: x=${node.x}, y=${node.y}`);
});

// Log edge bend points (if any)
g.edges().forEach(e => {
  const edge = g.edge(e);
  if (edge.points && edge.points.length > 0) {
    console.log(`Edge ${e.v}-${e.w} points:`, edge.points);
  }
});

view raw JSON →