What is ts-graphviz?
The ts-graphviz package is a TypeScript library for creating and manipulating Graphviz graphs. It provides a fluent API to build and render graphs programmatically, making it easier to generate complex diagrams and visualizations.
What are ts-graphviz's main functionalities?
Creating a Graph
This feature allows you to create a directed graph with nodes and edges. The code sample demonstrates how to create a simple graph with two nodes, 'A' and 'B', and an edge from 'A' to 'B'.
const { digraph } = require('ts-graphviz');
const g = digraph('G', (g) => {
g.node('A');
g.node('B');
g.edge(['A', 'B']);
});
console.log(g.toDot());
Customizing Nodes and Edges
This feature allows you to customize the appearance of nodes and edges. The code sample shows how to set the color of a node, change the shape of another node, and add a label to an edge.
const { digraph } = require('ts-graphviz');
const g = digraph('G', (g) => {
g.node('A', { color: 'red' });
g.node('B', { shape: 'box' });
g.edge(['A', 'B'], { label: 'A to B' });
});
console.log(g.toDot());
Subgraphs
This feature allows you to create subgraphs within a graph. The code sample demonstrates how to create two subgraphs, each with its own nodes and edges, and then connect nodes from different subgraphs.
const { digraph, subgraph } = require('ts-graphviz');
const g = digraph('G', (g) => {
g.subgraph('cluster_0', (s) => {
s.node('A');
s.node('B');
s.edge(['A', 'B']);
});
g.subgraph('cluster_1', (s) => {
s.node('C');
s.node('D');
s.edge(['C', 'D']);
});
g.edge(['A', 'C']);
});
console.log(g.toDot());
Other packages similar to ts-graphviz
graphviz
The graphviz package is a Node.js wrapper for the Graphviz graph visualization software. It allows you to create and render graphs using the Graphviz DOT language. Compared to ts-graphviz, it is more focused on providing a direct interface to Graphviz's capabilities and may require more manual handling of DOT syntax.
viz.js
The viz.js package is a JavaScript library that provides a way to render Graphviz graphs in the browser using WebAssembly. It allows you to generate and display graphs directly in web applications. Compared to ts-graphviz, viz.js is more suitable for client-side rendering and visualization.
d3-graphviz
The d3-graphviz package is a D3-based library for rendering Graphviz graphs in the browser. It integrates with the D3.js ecosystem, allowing for interactive and dynamic graph visualizations. Compared to ts-graphviz, d3-graphviz is more focused on web-based visualizations and interactivity.
ts-graphviz
Graphviz library for TypeScript.
English | 日本語
Key Features ✨
ts-graphviz
package provides models and ASTs for the Graphviz DOT language fully integrated with TypeScript.
- TypeScript-friendly API
- It provides models in the DOT language. TypeScript type definitions are also provided for attributes and even attribute types.
- Freedom from programming paradigms
- Designed to be object-oriented, it provides APIs that can be adapted to both imperative and declarative APIs. You can choose the paradigm that best fits your project.
- Suitable for any use cases
- Both a high-layer API to provide models and a low-layer API to handle ASTs are provided to address any use cases.
Installation 💽
This package can then be installed using a package manager.
$ npm install -S ts-graphviz
$ yarn add ts-graphviz
$ pnpm add ts-graphviz
Usage 📑
This section provides an overview of the package.
For more detailed API specifications, please refer to the comments in the TypeScript type definitions and the document automatically generated based on them.
ts-graphviz
Module 🚩
This module provides Model, an interface for working with the DOT language in JavaScript/TypeScript.
Object-Oriented ❤️
Model is designed to be object-oriented and provides classes Digraph
, Graph
, Subgraph
, Node
, and Edge
.
Provides a toDot
function to convert Model to DOT (DOT language string).
import { attribute as _, Digraph, Subgraph, Node, Edge, toDot } from 'ts-graphviz';
const G = new Digraph();
const A = new Subgraph('A');
const node1 = new Node('node1', {
[_.color]: 'red'
});
const node2 = new Node('node2', {
[_.color]: 'blue'
});
const edge = new Edge([node1, node2], {
[_.label]: 'Edge Label',
[_.color]: 'pink'
});
G.addSubgraph(A);
A.addNode(node1);
A.addNode(node2);
A.addEdge(edge);
const dot = toDot(G);
Advanced Usage
You can also add your own implementation by inheriting from the class.
import { Digraph, Node, Edge, EdgeTargetTuple, attribute as _, toDot } from 'ts-graphviz';
class MyCustomDigraph extends Digraph {
constructor() {
super('G', {
[_.label]: 'This is Custom Digraph',
});
}
}
class MyCustomNode extends Node {
constructor(id: number) {
super(`node${id}`, {
[_.label]: `This is Custom Node ${id}`
});
}
}
class MyCustomEdge extends Edge {
constructor(targets: EdgeTargetTuple) {
super(targets, {
[_.label]: 'This is Custom Edge'
});
}
}
const digraph = new MyCustomDigraph();
const node1 = new MyCustomNode(1);
const node2 = new MyCustomNode(2);
const edge = new MyCustomEdge([node1, node2]);
digraph.addNode(node1);
digraph.addNode(node2);
digraph.addEdge(edge);
const dot = toDot(g);
Declarative API 😎
When creating Graph
or Digraph
, you can use Model Factory to provide a notation more similar to the DOT language.
Model also has a declarative API, so you can consistently choose a declarative paradigm.
import { attribute as _, digraph, toDot } from 'ts-graphviz';
const G = digraph('G', (g) => {
const a = g.node('aa');
const b = g.node('bb');
const c = g.node('cc');
g.edge([a, b, c], {
[_.color]: 'red'
});
g.subgraph('A', (A) => {
const Aa = A.node('Aaa', {
[_.color]: 'pink'
});
const Ab = A.node('Abb', {
[_.color]: 'violet'
});
const Ac = A.node('Acc');
A.edge([Aa.port('a'), Ab, Ac, 'E'], {
[_.color]: 'red'
});
});
});
const dot = toDot(G);
Note Of course, we also provide an API for creating strict mode graphs.
import { strict, toDot } from 'ts-graphviz';
const G = strict.graph(...);
const dot = toDot(G);
ts-graphviz/ast
Module 🔢
This module status is .
An API is provided to handle ASTs for advanced use.
The following functions are provided as described in the state transition diagram.
- The
fromModel
function converts Model to AST. - The
stringify
function converts AST to DOT. - The
parse
function to convert from DOT to AST.
Note As you can see from the above figure, the toDot
function provided by the ts-graphviz
package is a composite function of fromModel
and stringify
.
Detailed usage is TODO.
Please refer to the TypeScript type definition.
The parse function and AST
import { parse } from 'ts-graphviz/ast';
const ast = parse(`
digraph example {
node1 [
label = "My Node",
]
}
`);
Related Projects 💫
Related projects can be found at ts-graphviz GitHub Organization.
The TypeScript/JavaScript ecosystem provides a variety of OSS with the goal of making Graphviz more connected and easier to use.
Contributors 👥
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors
specification. Contributions of any kind welcome!
How to Contribute 💪
The easiest way to contribute is to use the library and star repository.
Questions 💭
Feel free to ask questions on GitHub Discussions.
Report bugs / request additional features 💡
Please register at GitHub Issues.
Development / Bug Fixes 🧑💻
See CONTRIBUTING.md.
Financial Support 💸
Please support core member kamiazya.
Note Even just a dollar is enough motivation for me to develop 😊
License ⚖️
This software is released under the MIT License, see LICENSE.