react-diagrams
a super simple, no-nonsense diagramming library written in react that just works
Top Related Projects
🚀 JavaScript diagramming library that uses SVG and HTML for rendering.
mxGraph is a fully client side JavaScript diagramming library
JavaScript framework for visual programming
Visual connectivity for webapps
Graph theory (network) library for visualisation and analysis
Directed graph layout for JavaScript
Quick Overview
React Diagrams is a powerful diagramming library for React applications. It allows developers to create interactive, customizable diagrams and flowcharts with ease. The library provides a flexible and extensible architecture for building complex diagramming solutions.
Pros
- Highly customizable and extensible
- Supports both mouse and touch interactions
- Offers a wide range of built-in node and link types
- Provides a robust event system for handling user interactions
Cons
- Steep learning curve for advanced customizations
- Documentation could be more comprehensive
- Performance may degrade with very large diagrams
- Limited out-of-the-box styling options
Code Examples
- Creating a simple diagram:
import createEngine, { DiagramModel, DefaultNodeModel } from '@projectstorm/react-diagrams';
const engine = createEngine();
const model = new DiagramModel();
const node1 = new DefaultNodeModel('Node 1', 'rgb(0,192,255)');
node1.setPosition(100, 100);
const port1 = node1.addOutPort('Out');
const node2 = new DefaultNodeModel('Node 2', 'rgb(192,255,0)');
node2.setPosition(400, 100);
const port2 = node2.addInPort('In');
const link = port1.link(port2);
model.addAll(node1, node2, link);
engine.setModel(model);
- Adding a custom node:
import { NodeModel, NodeModelGenerics } from '@projectstorm/react-diagrams';
class CustomNodeModel extends NodeModel<NodeModelGenerics> {
constructor() {
super({
type: 'custom-node'
});
}
}
// In your diagram setup:
const customNode = new CustomNodeModel();
model.addNode(customNode);
- Handling events:
import { DiagramEngine } from '@projectstorm/react-diagrams';
const engine = createEngine();
engine.getStateMachine().registerListener({
eventDidFire: (event) => {
if (event.function === 'nodeClicked') {
console.log('Node clicked:', event.entity);
}
}
});
Getting Started
-
Install the package:
npm install @projectstorm/react-diagrams
-
Create a basic diagram:
import React from 'react'; import createEngine, { DiagramModel, DefaultNodeModel } from '@projectstorm/react-diagrams'; import { CanvasWidget } from '@projectstorm/react-canvas-core'; const App = () => { const engine = createEngine(); const model = new DiagramModel(); const node1 = new DefaultNodeModel('Node 1', 'rgb(0,192,255)'); node1.setPosition(100, 100); model.addNode(node1); engine.setModel(model); return ( <div style={{ height: '100vh' }}> <CanvasWidget engine={engine} /> </div> ); }; export default App;
-
Render the App component in your React application.
Competitor Comparisons
🚀 JavaScript diagramming library that uses SVG and HTML for rendering.
Pros of X6
- More comprehensive and feature-rich, offering a wider range of graph types and customization options
- Better performance for large-scale diagrams with thousands of nodes
- Extensive documentation and examples, making it easier for developers to get started
Cons of X6
- Steeper learning curve due to its extensive API and features
- Larger bundle size, which may impact load times for smaller applications
- Less React-specific, requiring more setup for integration with React projects
Code Comparison
react-diagrams:
import { DiagramEngine, DiagramModel, DefaultNodeModel } from '@projectstorm/react-diagrams';
const node1 = new DefaultNodeModel('Node 1', 'rgb(0,192,255)');
node1.setPosition(100, 100);
const port1 = node1.addOutPort('Out');
X6:
import { Graph } from '@antv/x6';
const graph = new Graph({
container: document.getElementById('container'),
width: 800,
height: 600,
});
const node = graph.addNode({
x: 100,
y: 100,
label: 'Node 1',
});
Both libraries offer powerful diagramming capabilities, but X6 provides more extensive features and better performance for large-scale diagrams. react-diagrams, on the other hand, offers a more React-centric approach and may be easier to integrate into existing React projects. The choice between the two depends on the specific requirements of your project and the level of complexity needed in your diagrams.
mxGraph is a fully client side JavaScript diagramming library
Pros of mxgraph
- More comprehensive and feature-rich, offering a wider range of diagram types and functionalities
- Better documentation and extensive examples, making it easier for developers to get started
- Supports multiple frameworks and can be used with vanilla JavaScript
Cons of mxgraph
- Larger file size and potentially heavier performance impact
- Steeper learning curve due to its extensive API and features
- Less modern architecture compared to React-based alternatives
Code Comparison
mxgraph:
var graph = new mxGraph(container);
var parent = graph.getDefaultParent();
graph.getModel().beginUpdate();
try {
var v1 = graph.insertVertex(parent, null, 'Hello,', 20, 20, 80, 30);
var v2 = graph.insertVertex(parent, null, 'World!', 200, 150, 80, 30);
var e1 = graph.insertEdge(parent, null, '', v1, v2);
} finally {
graph.getModel().endUpdate();
}
react-diagrams:
const engine = new DiagramEngine();
const node1 = new DefaultNodeModel('Hello,', 'rgb(0,192,255)');
node1.setPosition(100, 100);
const node2 = new DefaultNodeModel('World!', 'rgb(192,255,0)');
node2.setPosition(400, 100);
const link = node1.getPort('out').link(node2.getPort('in'));
engine.getModel().addAll(node1, node2, link);
JavaScript framework for visual programming
Pros of Rete
- More flexible and customizable, allowing for a wider range of node-based editor applications
- Better performance with large and complex diagrams
- Supports both 2D and 3D rendering
Cons of Rete
- Steeper learning curve due to its more complex architecture
- Less out-of-the-box functionality, requiring more setup and configuration
- Smaller community and fewer resources compared to React Diagrams
Code Comparison
React Diagrams:
import createEngine, { DiagramModel, DefaultNodeModel } from '@projectstorm/react-diagrams';
const engine = createEngine();
const model = new DiagramModel();
const node1 = new DefaultNodeModel('Node 1', 'rgb(0,192,255)');
model.addNode(node1);
engine.setModel(model);
Rete:
import Rete from 'rete';
import ReactRenderPlugin from 'rete-react-render-plugin';
const editor = new Rete.NodeEditor('demo@0.1.0', container);
editor.use(ReactRenderPlugin);
const node = new Rete.Node('Node');
await editor.addNode(node);
Both libraries offer powerful node-based diagramming capabilities, but React Diagrams is more React-focused and easier to get started with, while Rete provides greater flexibility and performance at the cost of complexity.
Visual connectivity for webapps
Pros of jsPlumb
- Framework-agnostic, works with vanilla JavaScript and various libraries/frameworks
- Extensive documentation and examples
- Mature project with a large community and long-term support
Cons of jsPlumb
- Steeper learning curve due to more complex API
- Requires more manual setup and configuration
- Commercial license required for some features
Code Comparison
jsPlumb:
jsPlumb.ready(function() {
var instance = jsPlumb.getInstance();
instance.connect({
source: "element1",
target: "element2",
anchor: ["Top", "Bottom"]
});
});
react-diagrams:
import { DiagramEngine, DiagramModel, DefaultNodeModel } from '@projectstorm/react-diagrams';
const node1 = new DefaultNodeModel('Node 1', 'rgb(0,192,255)');
const node2 = new DefaultNodeModel('Node 2', 'rgb(192,255,0)');
const link = node1.getPort('out').link(node2.getPort('in'));
model.addAll(node1, node2, link);
Summary
jsPlumb offers more flexibility and framework independence, making it suitable for a wider range of projects. However, it may require more setup and has a steeper learning curve. react-diagrams is more React-focused and easier to integrate into React applications, but may be less suitable for non-React projects. The choice between the two depends on the specific project requirements and the development team's expertise.
Graph theory (network) library for visualisation and analysis
Pros of Cytoscape.js
- More versatile and feature-rich, supporting a wide range of graph visualization types
- Larger and more active community, resulting in better documentation and support
- Better performance for large-scale graphs and complex visualizations
Cons of Cytoscape.js
- Steeper learning curve due to its extensive API and features
- Less React-specific, requiring more setup and integration work for React projects
Code Comparison
React-diagrams:
import createEngine, { DiagramModel } from '@projectstorm/react-diagrams';
const engine = createEngine();
const model = new DiagramModel();
engine.setModel(model);
Cytoscape.js:
import cytoscape from 'cytoscape';
const cy = cytoscape({
container: document.getElementById('cy'),
elements: [ /* ... */ ],
style: [ /* ... */ ]
});
React-diagrams is more React-centric and easier to integrate into React applications, while Cytoscape.js offers a more general-purpose approach to graph visualization. React-diagrams focuses on diagramming and flowcharts, whereas Cytoscape.js provides a broader range of graph types and layouts. The choice between the two depends on the specific requirements of your project, such as the complexity of the graphs, performance needs, and integration with React.
Directed graph layout for JavaScript
Pros of dagre
- Language-agnostic: Can be used with various JavaScript frameworks or even non-JavaScript environments
- Focused on graph layout algorithms, providing more advanced and customizable layout options
- Lightweight and has fewer dependencies
Cons of dagre
- Requires more manual setup and integration with rendering libraries
- Less out-of-the-box functionality for interactive diagrams
- Steeper learning curve for beginners
Code Comparison
react-diagrams:
import { DiagramEngine, DiagramModel, DefaultNodeModel } from '@projectstorm/react-diagrams';
const engine = new DiagramEngine();
const model = new DiagramModel();
const node1 = new DefaultNodeModel('Node 1', 'rgb(0,192,255)');
model.addNode(node1);
engine.setModel(model);
dagre:
var g = new dagre.graphlib.Graph();
g.setNode("kspacey", { label: "Kevin Spacey", width: 144, height: 100 });
g.setNode("swilliams", { label: "Saul Williams", width: 160, height: 100 });
g.setEdge("kspacey", "swilliams");
dagre.layout(g);
Summary
react-diagrams is a React-specific library that provides a more complete solution for interactive diagrams with built-in components and event handling. dagre, on the other hand, is a more flexible and powerful graph layout library that can be used across different frameworks but requires more setup and integration with rendering libraries. The choice between the two depends on the specific project requirements, desired level of customization, and the development environment.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Introduction
DEMO: http://projectstorm.cloud/react-diagrams
DOCS (wip) https://projectstorm.gitbook.io/react-diagrams
Docs are currently being worked on, along with a migration path.
What
A flow & process orientated diagramming library inspired by Blender, Labview and Unreal engine.
- Modern Codebase written entirely in Typescript and React, the library makes use of powerful generics, advanced software engineering principles and is broken up into multiple modules.
- Hackable and extensible the entire library including its core can be extended, rewired and re-assembled into fundamentally different software to suit your own software needs.
- HTML nodes as a first class citizen the library was originally written to represent advanced dynamic nodes, that are difficult to represent as SVG's due to complex input requirements ux requirements.
- Designed for process the library is aimed for software engineers that want to rewire their programs at runtime, and that want to make their software more dynamic.
- Fast diagram editing the defaults provided give the highest priority to editing diagrams as fast as possible.
Gallery
Example implementation using custom models: (Dylan's personal code)
Get started with the default models right out of the box:
Installing
For all the bells and whistles:
yarn add @projectstorm/react-diagrams
This includes all the packages listed below (and works (mostly and conceptually) like it used to in version 5.0)
A more modular approach
This library now has a more modular design and you can import just the core (contains no default factories or routing)
yarn add @projectstorm/react-diagrams-core
this is built ontop of the evolving react-canvas-core library
yarn add @projectstorm/react-canvas-core
which makes use of
yarn add @projectstorm/geometry
and of course, you can add some extras:
yarn add @projectstorm/react-diagrams-defaults
yarn add @projectstorm/react-diagrams-routing
How to use
Before running any of the examples, please run pnpm build
in the root. This project is a monorepo, and the packages (including the demos) require the packages to first be built.
Take a look at the diagram demos
or
Take a look at the demo project which contains an example for ES6 as well as Typescript
or
Run the demos
After running pnpm install
and pnpm build
, you must then run: cd diagrams-demo-gallery && pnpm run start
Building from source
Simply run pnpm
then pnpm build
or pnpm build:prod
in the root directory and it will spit out the transpiled code and typescript definitions into the dist directory as a single file.
Built with react-diagrams
Do you have an interesting project built with react-diagrams? PR it into this section for others to see.
Top Related Projects
🚀 JavaScript diagramming library that uses SVG and HTML for rendering.
mxGraph is a fully client side JavaScript diagramming library
JavaScript framework for visual programming
Visual connectivity for webapps
Graph theory (network) library for visualisation and analysis
Directed graph layout for JavaScript
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot