Graphviz layout options
This article is for people who are already familiar with GraphViz and are looking at ways to make their graphs look prettier.
Here we'll look into several less known but very important layout options. We are skipping well known LR/TD directions, and clusters.
That would be our starting point, let's say we are creating a simple system architecture diagram.
digraph Graph {
node[shape=rect]
gateway -> users
gateway -> companies
gateway -> groups
}
specify Width and Heigh of nodes
explicitly set width and height of nodes
digraph Graph {
node[shape=rect]
gateway[width=3 height=0.3]
gateway -> users
gateway -> companies
gateway -> groups
}
splines - change arrow line shapes
digraph Graph {
node[shape=rect]
splines=ortho
gateway[width=3 height=0.3]
gateway -> users
gateway -> companies
gateway -> groups
}
ranksep - change spacing between ranks
digraph Graph {
node[shape=rect]
splines=ortho
gateway[width=3 height=0.3]
gateway -> users
gateway -> companies
gateway -> groups
ranksep=1.5
}
nodesep - change spacing between nodes
digraph Graph {
node[shape=rect]
splines=ortho
gateway[width=3 height=0.3]
gateway -> users
gateway -> companies
gateway -> groups
nodesep=1
}
Now let's say I add one more edge, and it changes the weights of nodes and makes graph looks like that
digraph Graph {
node[shape=rect]
splines=ortho
gateway[width=3 height=0.3]
gateway -> users
users -> companies
gateway -> companies
gateway -> groups
nodesep=1
}
there are 2 ways to address that
apply rank to subgraph items
you can apply same 'rank' to a subgraph
digraph Graph {
node[shape=rect]
splines=ortho
gateway[width=3 height=0.3]
gateway -> users
users -> companies
gateway -> companies
gateway -> groups
nodesep=1
{
rank=same
users
companies
}
}
constraint
if you see that some arrow adds to much weight to a node, you can exclude it from ranking
digraph Graph {
node[shape=rect]
splines=ortho
gateway[width=3 height=0.3]
gateway -> users
users -> companies[constraint=false]
gateway -> companies
gateway -> groups
nodesep=1
}
Please checkout some of our useful resources:
- Graphviz cheatsheet
- Graphviz Playground
- Graphviz interactive tutorial
- Graphviz vs MermaidJS comparison
This article was originally published in DevToolsDaily blog