Skip to content

Instantly share code, notes, and snippets.

@saintedlama
Created December 18, 2021 08:39
Show Gist options
  • Save saintedlama/8a29ee720665f8b7295c662617e04601 to your computer and use it in GitHub Desktop.
Save saintedlama/8a29ee720665f8b7295c662617e04601 to your computer and use it in GitHub Desktop.
const graphviz = require("graphviz");
const defaultOptions = {
G: {
overlap: false,
pad: 0.3,
rankdir: "LR",
layout: "dot",
bgcolor: "#111111",
},
E: {
color: "#757575",
},
N: {
fontname: "Arial",
fontsize: "14px",
color: "#c6c5fe",
shape: "box",
style: "rounded",
height: 0,
fontcolor: "#c6c5fe",
},
type: "dot",
};
function createGraph(graph, options) {
const g = graphviz.digraph("G");
const nodes = {};
Object.keys(graph).forEach((id) => {
nodes[id] = nodes[id] || g.addNode(id);
graph[id].forEach((depId) => {
nodes[depId] = nodes[depId] || g.addNode(depId);
g.addEdge(nodes[id], nodes[depId]);
});
});
return new Promise((resolve, reject) => g.output(options, resolve, (code, out, err) => (err ? reject(err) : resolve(code))));
}
const graph = {
a: ["b", "c"],
b: ["c"],
c: [],
d: ["a", "b", "c"],
};
createGraph(graph, defaultOptions).then((code) => console.log(code.toString()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment