user3313834
user3313834

Reputation: 7837

Order boxes left to right in dot graphviz

I have this gv code,

I want to have the horizontal order : 12 13 14 15 23 24

but I get:

23 13 12 24 14 15

graph "tree" {
node [shape=plaintext];

1  [label = "1"]
2  [label = "2"]
3  [label = "3"]
4  [label = "4"]
5  [label = "5"]
12 [label = "12"]
13 [label = "13"]
14 [label = "14"]
15 [label = "15"]
23 [label = "23"]
24 [label = "24"]

1 -- 12;
2 -- 12;
1 -- 13;
3 -- 13;
1 -- 14;
4 -- 14;
1 -- 15;
5 -- 15;
2 -- 23;
3 -- 23;
2 -- 24;
4 -- 24;}  

the solution : Force the left to right order of nodes in graphviz? did not work for this case (ordering nodes not edges).

enter image description here

If I add :

{rank = same; 12 13 14 15 23 24; rankdir=LR;}

We get the same unordered nodes:

png file made with:

dot -T png test.gv > test.png

Upvotes: 4

Views: 1702

Answers (2)

nachocab
nachocab

Reputation: 14454

Or more simply:

graph "tree" {
    node[shape=plaintext]

    1 -- {12 13 14 15}
    2 -- {12 23 24}
    3 -- {13 23}
    4 -- {14 24}
    5 -- 15;

    {
        rank = same; 
        12 -- 13 -- 14 -- 15 -- 23 -- 24 [color=invis]
    }
}

enter image description here

Upvotes: 3

Keith Davies
Keith Davies

Reputation: 215

Simplest is to add 'invisible' (white on white, no arrows) edges.

This will encourage dot to align the nodes in order.

graph "tree" {
node [shape=plaintext];

1  [label = "1"]
2  [label = "2"]
3  [label = "3"]
4  [label = "4"]
5  [label = "5"]
12 [label = "12"]
13 [label = "13"]
14 [label = "14"]
15 [label = "15"]
23 [label = "23"]
24 [label = "24"]

1 -- 12;
2 -- 12;
1 -- 13;
3 -- 13;
1 -- 14;
4 -- 14;
1 -- 15;
5 -- 15;
2 -- 23;
3 -- 23;
2 -- 24;
4 -- 24;
// 'white' (invisible on white background) edges, weight to encourage order
// results in tidiest graph with horizontal nodes in desired order.
edge [color=white,weight=4,arrowhead=none,arrowtail=none];
12 -- 13 -- 14 -- 15 -- 23 -- 24 -- 25 -- 34 -- 35 -- 45;
{rank = same; 12 13 14 15 23 24 25 34 35 45; rankdir=LR;}
}

Upvotes: 1

Related Questions