PackagesGraph
DOT
Converter for the DOT language used by Graphviz.
Converter for the DOT language used by Graphviz.
Requires peer dependency: dotparser
npm install dotparserResources
API
import { toDOT, fromDOT, dotConverter } from '@statelyai/graph/dot';
import { createGraph } from '@statelyai/graph';
// Export
const graph = createGraph({
id: 'etl',
direction: 'right',
nodes: [
{ id: 'extract', label: 'Extract', shape: 'rectangle', color: '#2196f3' },
{ id: 'transform', label: 'Transform', shape: 'diamond' },
{ id: 'load', label: 'Load', shape: 'rectangle', color: '#4caf50' },
],
edges: [
{ id: 'e0', sourceId: 'extract', targetId: 'transform', label: 'raw data' },
{ id: 'e1', sourceId: 'transform', targetId: 'load', label: 'cleaned' },
],
});
const dot = toDOT(graph);
// digraph etl {
// rankdir=LR;
// extract [label="Extract", shape=box, fillcolor="#2196f3", style=filled];
// transform [label="Transform", shape=diamond];
// load [label="Load", shape=box, fillcolor="#4caf50", style=filled];
// extract -> transform [label="raw data"];
// transform -> load [label="cleaned"];
// }
// Import
const imported = fromDOT(`digraph deploy {
rankdir=LR;
build [label="Build" shape=box];
test [label="Test" shape=box];
staging [label="Staging" shape=ellipse];
prod [label="Production" shape=box fillcolor="#4caf50" style=filled];
build -> test;
test -> staging [label="passed"];
staging -> prod [label="approved"];
}`);
// a graph containing:
// nodes: - build - test - staging - prod
// edges: - (build -> test) - (test -> staging) - (staging -> prod)toDOT(graph): string
Uses digraph/-> for directed graphs, graph/-- for undirected. Preserves: label, shape (mapped to DOT names), color (as fillcolor), direction (as rankdir), edge color.
fromDOT(dot): Graph
Parses:
digraph/graph→ directed/undirected- Node attributes:
label,shape(mapped back),fillcolor/color - Edge attributes:
label,color - Edge chains (
a -> b -> c→ 2 edges) subgraph→ compound node withparentIdrankdir→directionnode [...]/edge [...]default attributes- Auto-creates nodes referenced in edges
Not mapped:
- HTML labels (
<...>) — stored as raw string - Port syntax (
:port:compass) rank=sameand layout hints beyondrankdirstyleattribute (beyond color extraction)
dotConverter
Pre-built GraphFormatConverter<string>:
const dot = dotConverter.to(graph);
const graph = dotConverter.from(dot);