Graphviz / Dot Language, Snippets
At times when I need to visually show how the components of a system work I reach out and use a very handy tool called dot language.
There are many, many tools to convert from dot language to svg or other image formats. The basics are simple, yet It’s hard to have a consistent style and flow across multiple digrams.
Here I try to go over the basics and show a few snippets that I found usefull over time.
Use arrow (->) to connect a node to another.
digraph foo {
client -> api;
api -> postgres;
api -> redis;
api -> client;
}
Use rank to force some nodes be on the same line.
digraph foo {
{ rank = same; client api }
client -> api;
api -> postgres;
api -> redis;
}
Use label to rename a node, or comment on an arrow.
digraph foo {
pg [ label = "postgresql"];
api -> pg [ label=" writes to" ];
}
Style your nodes using style, fillcolor, color, fontcolor, shape, and fontsize.
digraph foo {
api [
shape="box",
style="filled, rounded",
fillcolor=lightyellow
];
postgres [
shape="pentagon",
style="rounded",
color="orange",
fontcolor="blue"
];
api -> postgres;
}
It’s possible to style all nodes at the same time.
digraph {
node [
style="filled",
fillcolor="#EECDA3:#EF629F",
fontsize=16
];
api -> redis;
api -> postgres;
}
You can also apply the same style on multiple nodes.
digraph {
browser, mobile [
style="filled",
fillcolor="#c2e59c:#64b3f4",
fontcolor="#203060",
];
redis, postgres, api [
style="filled, rounded",
];
redis, postgres [
shape="rectange",
fillcolor="#FFB75E:#ED8F03",
];
api [
color="#7b4397:#dc2430"
fontcolor=white,
shape="pentagon"
];
browser -> api;
api -> redis;
api -> postgres;
mobile -> api;
}