Search code examples
graphvizdot

How to align columns of nodes and wrap edges?


I can use this code to generate a graph:

file test.dot:

digraph g {
    {rank=same;  1 -> 2 -> 3 -> 4}
    {rank=same;  5 -> 6 -> 7 -> 8}
    {rank=same;  9 -> 10 -> 11 -> 12}

    4 -> 5
    8 -> 9
}

dot test.dot -Tpng -o test.png

output:

enter image description here

However, I want the ranks of nodes to be aligned more like this:

enter image description here

Is it possible to make a graph shaped like this in graphviz dot?


Solution

  • Use an invisible edge with a strong weight:

    digraph g 
    {
        splines="ortho"
        
        // connect the left most nodes and keep them one below the other
        1 -> 5 -> 9[ style = invis, weight = 10 ];
    
        // do your stuff
        { rank = same;  1 -> 2 -> 3 -> 4 }
        { rank = same;  5 -> 6 -> 7 -> 8 }
        { rank = same;  9 -> 10 -> 11 -> 12 }
    
        4 -> 5;
        8 -> 9;
    }
    

    yields:

    enter image description here