Graphviz layout options

2020-02-15

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
}

graphviz_layout_initial open in playground

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
}

graphviz_layout_node_size open in playground

splines - change arrow line shapes

digraph Graph {
  node[shape=rect]
  splines=ortho
  gateway[width=3 height=0.3]
  gateway -> users
  gateway -> companies
  gateway -> groups
}

graphviz_layout_arrow open in playground

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
}

graphviz_layout_node_space open in playground

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
}

graphviz_layout_node_space open in playground

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
}

graphviz_layout_unranked 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
  }
}

open playground graphviz_layout_ranked

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
}

open playground graphviz_layout_ranked


Please checkout some of our useful resources:

  1. Graphviz cheatsheet
  2. Graphviz Playground
  3. Graphviz interactive tutorial
  4. Graphviz vs MermaidJS comparison

This article was originally published in DevToolsDaily blog