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.
- Modular and Extensible
- The library is split into multiple packages, each serving a specific purpose. This modular design allows users to pick and choose the functionality they need, resulting in improved maintainability and flexibility.
- Cross Platform
- It supports both Node.js and Deno and Browser.
- Customizable
- It provides a way to extend the library's type system to customize graph visualization solutions to meet specific needs.
Installation π½
Node.js
This package can then be installed using a package manager.
$ npm install -S ts-graphviz
$ yarn add ts-graphviz
$ pnpm add ts-graphviz
Note Want to try before installing? Check out Runkit to see how it works.
Deno π¦
Deno v1.28 and above supports npm.
You can install and use the package by specifying the following:
import { toDot } from 'npm: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
Custom Class π€
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: string) {
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('A');
const node2 = new MyCustomNode('B');
const edge = new MyCustomEdge([node1, node2]);
digraph.addNode(node1);
digraph.addNode(node2);
digraph.addEdge(edge);
const dot = toDot(digraph);
Models Context API ( with
method) π§
You can also use the Models Context API to create custom classes for objects generated inside of Graph.
The with
methods of Digraph
, Graph
, and Subgraph
, which are implementations of GraphBaseModel
, are provided to predefine custom models.
const g = new Digraph();
g.with({
Node: MyCustomNode,
Edge: MyCustomEdge,
});
const a = g.createNode('A');
const b = g.createNode('B');
g.createEdge([a, b]);
const dot = toDot(g);
fromDot
function βͺ
The status of this function is ! beta.
The main scenario for using this library is to use the toDot
function, but it also supports conversions in the reverse direction.
By converting DOT to Model, a portion of the code can be written in the DOT language.
const G = fromDot(
`digraph {
node_A [
label = "This is a Label of Node A";
];
}`,
);
G.edge(['node_A', 'node_B']);
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);
Advanced Usage
Models Context API ( withContext
function ) π
The withContext
function returns a Model Factory function.
This Model Factory provides a means to replace RootGraphModel
with a custom class, such as Digraph
or Graph
.
This API provides a way to integrate declarative APIs and custom classes.
const { digraph } = withContext({
Digraph: MyCustomDigraph,
Node: MyCustomNode,
Edge: MyCustomEdge,
});
const G = digraph((g) => {
const a = g.node('A');
const b = g.node('B');
g.edge([a, b]);
});
const dot = toDot(g);
@ts-graphviz/adapter
Module π
This module status is .
Provides an interface to run Graphviz dot commands.
Graphviz must be installed so that the dot command can be executed.
Execute the dot command to output a DOT language string to a stream or file.
This module provides the following functions.
- The
toStream
function converts DOT to Stream.
import { toStream } from 'ts-graphviz/adapter';
const dot = `
digraph example {
node1 [
label = "My Node",
]
}
`;
const stream = await toStream(dot, { format: 'svg' });
stream.pipe(process.stdout);
await stream.pipeTo(Deno.stdout.writable);
- Writes DOT to a file at the specified path
toFile
function
import { toFile } from 'ts-graphviz/adapter';
const dot = `
digraph example {
node1 [
label = "My Node",
]
}
`;
await toFile(dot, './result.svg', { format: 'svg' });
Note Designed to work with Node.js and Deno, Stream is runtime native.
@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
toModel
function converts AST to Model. - 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 of fromModel
and stringify
. The fromDot
function is a composite of parse
and toModel
.
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",
]
}
`);
Extending the Type System π§°
The status of this feature is .
With ts-graphviz, you can extend the library's type system to customize graph visualization solutions to meet specific needs.
Note To allow for customization, types are named with the $
symbol.
If you want to extend a type definition in cases not listed below, check the source code to see if you can extend it with $...
.
If not, please create an issue or pull request.
Use Case: Specifying Custom Graph Layout and Output Formats
import { $keywords } from '@ts-graphviz/common';
import { toFile } from '@ts-graphviz/adapter';
declare module '@ts-graphviz/adapter' {
export namespace Layout {
export interface $values extends $keywords<'my-custom-algorithm'> {}
}
export namespace Format {
export interface $values extends $keywords<'mp4'> {}
}
}
toFile('digraph { a -> b }', '/path/to/file', {
layout: 'my-custom-algorithm',
format: 'mp4',
});
Use Case: Adding Custom Attributes
import { $keywords } from '@ts-graphviz/common';
import { digraph, toDot, attribute as _ } from 'ts-graphviz';
declare module '@ts-graphviz/common' {
export namespace GraphAttributeKey {
export interface $values extends $keywords<'hoge'> {}
}
export namespace Attribute {
export interface $keys extends $keywords<'hoge'> {}
export interface $types {
hoge: string;
}
}
}
console.log(
toDot(
digraph((g) => {
g.set(_.hoge, 'fuga');
}),
),
);
Deep dive πββοΈ
Architecture π
See ARCHITECTURE.md for more details.
Security π‘οΈ
See SECURITY.md for more details.
Who's using ts-graphviz
π
Note Let us know that you're using ts-graphviz
on GitHub Discussions π
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 πΈ
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. Become a sponsor
Thank you to all our backers! π Become a backer
Please support ts-graphviz on Open Collective or GitHub Sponsors.
Note Even just a dollar is enough motivation to develop π
ts-graphviz for Enterprise π’
Available as part of the Tidelift Subscription.
The maintainers of ts-graphviz and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open-source dependencies you use to build your applications.
Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use.
Learn more.
License βοΈ
This software is released under the MIT License, see LICENSE.