Maintain horizontal ordering of nodes in Graphviz
Asked Answered
T

1

8

I have the following the dot file contents:

digraph G {
    start -> {a0, b0} -> end;
    start -> c0 -> c1 -> c2 -> end;
    start -> d0 -> d1 -> d2 -> end;
    start -> {e0, f0} -> end;

    subgraph cluster_test {
        {
            rank = same;
            a0; b0; c0; d0; e0; f0;
        }

        {
            rank = same;
            c1; d1;
        }

        {
            rank = same;
            c2; d2;
        }
    }
}

The resulting graph is as follows: enter image description here

What I want is for the ordering of level 0 nodes to be maintained, i.e, I want a0, b0 to come before c0, d0 in the horizontal direction.

How do I achieve this?

Telesis answered 12/4, 2018 at 9:37 Comment(0)
Y
4

Empty nodes, edges with weight and explicit ordering of the top row in the cluster helps. See code below with annotations:

digraph so 
{
    // we define all nodes in the beginning, before edges and clusters
    // may not be essential but I think it's good practice
    start
    a0 b0 c0 d0 e0 f0
    c1 d1 
    c2 d2
    end
    // we define "empty" nodes that can be used to route the edges
    node[ shape = point, height = 0 ];
    ax bx ex fx

    subgraph cluster_test 
    {
        // we need to keep explicit order of the top nodes in the cluster
        { rank = same; a0 -> b0 -> c0 -> d0 -> e0 ->  f0[ style = invis ] }
        // the original layout in the cluster, empty nodes added at the bottom
        { rank = same; c1 d1 }
        { rank = same; ax bx c2 d2 ex fx }
        c0 -> c1 -> c2;
        d0 -> d1 -> d2;
        // routing through invisible nodes keeps the position of all other nodes
        // edges with no arrowheads, strong weight to keep it vertical
        edge[ dir = none, weight = 10 ]
        a0 -> ax;
        b0 -> bx;
        e0 -> ex;
        f0 -> fx;
    }

    // connecting to the start and end node, normal edges again
    edge[ weight = 1, dir = forw ];
    start -> { a0 b0 c0 d0 e0 f0 }
    { ax bx  c2 d2 ex fx } -> end;
}

which gives you

enter image description here

Yoshikoyoshio answered 12/4, 2018 at 11:56 Comment(2)
While technically correct, this implementation is more than a little overkill for the average use case. Most StackOverflowers probably want this minimal-length example and the expository graphs that follow instead. tl;dr: Just use invisible directed edges between problematic horizontal nodes in (possibly) invisible ranks to enforce ordering.Overfly
You are right @Cecil Curry - I answered a question to OP didn't ask (how to keep the edges vertical within the subgraph). The solution you refer to is part of my answer but obfuscated by unnecessary noise.Yoshikoyoshio

© 2022 - 2024 — McMap. All rights reserved.