Convert Figma logo to code with AI

dagrejs logodagre-d3

A D3-based renderer for Dagre

2,929
593
2,929
217

Top Related Projects

Graph theory (network) library for visualisation and analysis

:dizzy: Display dynamic, automatically organised, customizable network views.

11,554

♾ A Graph Visualization Framework in JavaScript.

Force-directed graph rendered on HTML5 canvas

7,835

⚠️ This project is not maintained anymore! Please go to https://github.com/visjs

Force-directed graph layout using velocity Verlet integration.

Quick Overview

Dagre-D3 is a JavaScript library that combines the layout capabilities of Dagre with the rendering power of D3.js to create directed graphs. It provides an easy way to generate and display hierarchical or flow-based diagrams in web applications.

Pros

  • Seamless integration with D3.js for powerful and flexible graph visualization
  • Automatic layout calculation for nodes and edges, simplifying complex graph creation
  • Supports custom styling and interactivity for nodes and edges
  • Well-documented API with examples and demos

Cons

  • Limited layout options compared to more specialized graph libraries
  • Performance may degrade with very large graphs (hundreds of nodes)
  • Requires knowledge of both Dagre and D3.js for advanced customization
  • Not actively maintained, with the last update in 2018

Code Examples

  1. Creating a simple graph:
// Create a new graph
var g = new dagreD3.graphlib.Graph().setGraph({});

// Add nodes to the graph
g.setNode("A", { label: "Node A" });
g.setNode("B", { label: "Node B" });

// Add an edge to the graph
g.setEdge("A", "B", { label: "A to B" });
  1. Rendering the graph:
// Create an SVG element
var svg = d3.select("svg");
var inner = svg.append("g");

// Create the renderer
var render = new dagreD3.render();

// Run the renderer
render(inner, g);
  1. Customizing node appearance:
g.setNode("C", {
  label: "Node C",
  shape: "circle",
  style: "fill: #f77; stroke: #000;"
});
  1. Adding interactivity:
// Add click event to nodes
svg.selectAll("g.node")
  .on("click", function(d) {
    console.log("Clicked node:", d);
  });

Getting Started

To use Dagre-D3 in your project:

  1. Include the necessary libraries in your HTML:
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://dagrejs.github.io/project/dagre-d3/latest/dagre-d3.min.js"></script>
  1. Create an SVG element in your HTML:
<svg width="600" height="400"></svg>
  1. Initialize and render a graph in your JavaScript:
// Create a new graph
var g = new dagreD3.graphlib.Graph().setGraph({});

// Add nodes and edges
g.setNode("A", { label: "Node A" });
g.setNode("B", { label: "Node B" });
g.setEdge("A", "B");

// Render the graph
var svg = d3.select("svg");
var inner = svg.append("g");
var render = new dagreD3.render();
render(inner, g);

This will create a simple graph with two connected nodes. You can then customize and expand upon this basic example to create more complex graphs.

Competitor Comparisons

Graph theory (network) library for visualisation and analysis

Pros of Cytoscape.js

  • More versatile and feature-rich, supporting various graph types and layouts
  • Extensive API and plugin ecosystem for advanced customization
  • Better performance for large graphs and complex visualizations

Cons of Cytoscape.js

  • Steeper learning curve due to its comprehensive nature
  • Larger file size, which may impact load times for simpler projects
  • May be overkill for basic directed graph visualizations

Code Comparison

Dagre-D3:

var g = new dagreD3.graphlib.Graph().setGraph({});
g.setNode("A", { label: "Node A" });
g.setNode("B", { label: "Node B" });
g.setEdge("A", "B");

Cytoscape.js:

var cy = cytoscape({
  elements: [
    { data: { id: 'A', label: 'Node A' } },
    { data: { id: 'B', label: 'Node B' } },
    { data: { source: 'A', target: 'B' } }
  ]
});

Both libraries offer ways to create and manipulate graphs, but Cytoscape.js provides a more object-oriented approach with its comprehensive API. Dagre-D3 focuses specifically on directed graphs and integrates seamlessly with D3.js, while Cytoscape.js offers a standalone solution with broader capabilities for various graph types and layouts.

:dizzy: Display dynamic, automatically organised, customizable network views.

Pros of vis-network

  • More feature-rich with a wider range of network visualization options
  • Better performance for large networks with thousands of nodes
  • Active development and regular updates

Cons of vis-network

  • Steeper learning curve due to more complex API
  • Less focused on directed acyclic graphs (DAGs) compared to dagre-d3

Code Comparison

vis-network:

var nodes = new vis.DataSet([
  {id: 1, label: 'Node 1'},
  {id: 2, label: 'Node 2'},
]);
var edges = new vis.DataSet([
  {from: 1, to: 2},
]);
var container = document.getElementById('mynetwork');
var data = {nodes: nodes, edges: edges};
var options = {};
var network = new vis.Network(container, data, options);

dagre-d3:

var g = new dagreD3.graphlib.Graph().setGraph({});
g.setNode(1, { label: "Node 1" });
g.setNode(2, { label: "Node 2" });
g.setEdge(1, 2);
var render = new dagreD3.render();
var svg = d3.select("svg");
render(d3.select("svg g"), g);

The code examples show that vis-network uses a more object-oriented approach with separate datasets for nodes and edges, while dagre-d3 uses a graph-based API. vis-network's setup is more verbose but offers more flexibility, while dagre-d3's approach is more concise and focused on graph structures.

11,554

♾ A Graph Visualization Framework in JavaScript.

Pros of G6

  • More comprehensive and feature-rich graph visualization library
  • Supports a wider range of graph types and layouts
  • Better performance for large-scale graph rendering

Cons of G6

  • Steeper learning curve due to more complex API
  • Less focused on directed graphs compared to dagre-d3

Code Comparison

G6 example:

const graph = new G6.Graph({
  container: 'mountNode',
  width: 800,
  height: 600,
});

graph.data(data);
graph.render();

dagre-d3 example:

const svg = d3.select("svg"),
    inner = svg.select("g");

const render = new dagreD3.render();
render(inner, g);

Key Differences

  • G6 is a more general-purpose graph visualization library, while dagre-d3 focuses specifically on directed graphs
  • G6 offers more customization options and built-in features, but dagre-d3 is simpler to use for basic directed graph layouts
  • G6 has better documentation and community support, while dagre-d3 has a smaller but more specialized user base

Use Cases

  • Choose G6 for complex, large-scale graph visualizations with diverse layout requirements
  • Opt for dagre-d3 when working primarily with directed graphs and prefer a simpler API

Community and Maintenance

  • G6 has more active development and frequent updates
  • dagre-d3 has a stable codebase but fewer recent updates

Force-directed graph rendered on HTML5 canvas

Pros of force-graph

  • More flexible and customizable for complex graph visualizations
  • Supports 3D graph rendering out of the box
  • Offers smoother animations and interactions with large datasets

Cons of force-graph

  • Steeper learning curve for beginners
  • May require more manual configuration for specific layouts
  • Performance can degrade with extremely large graphs (10,000+ nodes)

Code Comparison

force-graph:

const Graph = ForceGraph3D()
  (document.getElementById('3d-graph'))
    .graphData(myData)
    .nodeLabel('id')
    .nodeAutoColorBy('group')
    .linkDirectionalParticles("value");

dagre-d3:

var g = new dagreD3.graphlib.Graph().setGraph({});
g.setNode("a", { label: "A" });
g.setNode("b", { label: "B" });
g.setEdge("a", "b");
var render = new dagreD3.render();
render(d3.select("svg g"), g);

force-graph offers a more concise API for creating complex 3D graphs with particle effects, while dagre-d3 provides a more traditional approach to 2D graph rendering with explicit node and edge definitions.

7,835

⚠️ This project is not maintained anymore! Please go to https://github.com/visjs

Pros of vis

  • More comprehensive library with support for various types of visualizations (networks, timelines, graphs)
  • Active development and larger community support
  • Extensive documentation and examples

Cons of vis

  • Larger file size and potentially heavier performance impact
  • Steeper learning curve due to more features and options
  • Less specialized for directed graphs compared to dagre-d3

Code Comparison

dagre-d3:

var g = new dagreD3.graphlib.Graph().setGraph({});
g.setNode("A", { label: "Node A" });
g.setNode("B", { label: "Node B" });
g.setEdge("A", "B");

vis:

var nodes = new vis.DataSet([
  { id: 1, label: "Node A" },
  { id: 2, label: "Node B" }
]);
var edges = new vis.DataSet([{ from: 1, to: 2 }]);
var network = new vis.Network(container, { nodes, edges }, options);

Both libraries offer ways to create and manipulate graphs, but vis provides a more object-oriented approach with separate DataSet objects for nodes and edges. dagre-d3 focuses on a simpler graph structure, while vis offers more flexibility for complex visualizations.

Force-directed graph layout using velocity Verlet integration.

Pros of d3-force

  • More flexible and customizable for general force-directed layouts
  • Integrates seamlessly with other D3 modules
  • Supports a wider range of graph types and visualizations

Cons of d3-force

  • Steeper learning curve for beginners
  • Requires more manual configuration for hierarchical layouts
  • May need additional code for edge routing and node positioning

Code Comparison

d3-force:

const simulation = d3.forceSimulation(nodes)
  .force("link", d3.forceLink(links).id(d => d.id))
  .force("charge", d3.forceManyBody())
  .force("center", d3.forceCenter(width / 2, height / 2));

dagre-d3:

const g = new dagre.graphlib.Graph();
g.setGraph({});
g.setDefaultEdgeLabel(() => ({}));
nodes.forEach(node => g.setNode(node.id, node));
links.forEach(link => g.setEdge(link.source, link.target));
dagre.layout(g);

Summary

d3-force is more versatile for general force-directed layouts, while dagre-d3 excels at hierarchical layouts. d3-force offers greater customization but requires more setup, whereas dagre-d3 provides easier out-of-the-box hierarchical layouts. Choose d3-force for complex, custom visualizations, and dagre-d3 for quick, structured hierarchical graphs.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

dagre-d3 - A D3-based renderer for dagre

Build Status npm

Dagre is a JavaScript library that makes it easy to lay out directed graphs on the client-side. The dagre-d3 library acts as a front-end to dagre, providing actual rendering using D3.

For more details, including examples and configuration options, please see our wiki.

License

dagre-d3 is licensed under the terms of the MIT License. See the LICENSE file for details.

NPM DownloadsLast 30 Days