A JS + WASM module for compiling graphs written in DOT to images, using GraphViz in Node.js.
No annoying native build system or native dependencies that need to be compiled.
npm install node-graphviz --save
See The DOT Language for more information about DOT, and GraphViz Pocket Reference for some examples.
const fs = require('fs');
const { graphviz } = require('node-graphviz');
// Define a graph using DOT notation
const graph = `
digraph {
a -> b;
b -> c;
c -> d;
d -> a;
}
`;
// Compile the graph to SVG using the `circo` layout algorithm
graphviz.circo(graph, 'svg').then((svg) => {
// Write the SVG to file
fs.writeFileSync('graph.svg', svg);
});
Running the above produces the following SVG:
The module exports the following API:
declare type Format = 'svg' | 'dot' | 'json' | 'dot_json' | 'xdot_json';
declare type Engine = 'circo' | 'dot' | 'fdp' | 'neato' | 'osage' | 'patchwork' | 'twopi';
export declare const graphviz = {
layout(dotSource: string, outputFormat?: Format, layoutEngine?: Engine): Promise<string>;
circo(dotSource: string, outputFormat?: Format): Promise<string>;
dot(dotSource: string, outputFormat?: Format): Promise<string>;
fdp(dotSource: string, outputFormat?: Format): Promise<string>;
neato(dotSource: string, outputFormat?: Format): Promise<string>;
osage(dotSource: string, outputFormat?: Format): Promise<string>;
patchwork(dotSource: string, outputFormat?: Format): Promise<string>;
twopi(dotSource: string, outputFormat?: Format): Promise<string>;
};
Performs layout for the supplied dotSource
.
Where:
outputFormat
is one of the following (see Output Formats for details):dot
dot_json
json
svg
(default)xdot_json
layoutEngine
is one of the following (see Layout documentation for details):circo
dot
(default)fdp
neato
osage
patchwork
twopi
Convenience function that performs circo layout, is equivalent to layout(dotSource, outputFormat, 'circo')
.
Convenience function that performs dot layout, is equivalent to layout(dotSource, outputFormat, 'dot')
.
Convenience function that performs circo layout, is equivalent to layout(dotSource, outputFormat, 'fdp')
.
Convenience function that performs neato layout, is equivalent to layout(dotSource, outputFormat, 'neato')
.
Convenience function that performs osage layout, is equivalent to layout(dotSource, outputFormat, 'osage')
.
Convenience function that performs patchwork layout, is equivalent to layout(dotSource, outputFormat, 'patchwork')
.
Convenience function that performs twopi layout, is equivalent to layout(dotSource, outputFormat, 'twopi')
.
This module is based on hpcc-systems/hpcc-js-wasm, which is designed for use in a browser, not Node.js. The following changes were made to support Node and simplify the module to include only GraphViz:
- Rewrote WASM binary location and fetching to read from the filesystem
- Added the compiled WASM binary to the source
- Reduced the JS code by half to include only GraphViz, removed Expat and other unrelated code
- Removed build system and TypeScript, in favor of a single source file (based on the compiled dist file from hpcc-systems/hpcc-js-wasm)