Graphviz dot align nodes vertically
Asked Answered
H

1

8

I have the following graphviz dot input file:

digraph structs {
rankdir = LR;
node [shape=record];
hashTable [label="<f0>0|<f1>1|<f2>2|<f3>3|<f4>4|<f5>5|<f6>6|<f7>7|<f8>8"];
node_1_0 [label="<f0> one|<f1> two |<f2> three"];
node_1_1 [label="<f0> un |<f1> deux|<f2> trois"];
struct3 [label="<f0> einz|<f1> swei|<f2> drei"];
hashTable:f1 -> node_1_0:f0;
node_1_0:f2  -> node_1_1:f0;
hashTable:f4 -> struct3:f0;
}

which gets rendered like this:

enter image description here

How can I have the [one|two|three] node be aligned vertically to the [un|deux|trois] node? Thanks!

Haunch answered 3/12, 2017 at 4:53 Comment(0)
R
7

Edit after clarification in the comment: Here an invisible edge with a strong weight helps to keep the nodes aligned:

digraph structs2 
{
    rankdir = LR;
    node [shape=record];
    splines=false;       // optional; gives straight lines

    hashTable [label="<f0>0|<f1>1|<f2>2|<f3>3|<f4>4|<f5>5|<f6>6|<f7>7|<f8>8"];
    node_1_0 [label="<f0> one|<f1> two |<f2> three" ];
    node_1_1 [label="<f0> un |<f1> deux|<f2> trois"];
    struct3 [label="<f0> einz|<f1> swei|<f2> drei"];

    //
    node_1_0 -> node_1_1[ style = invis, weight= 10 ];
    //                    ^^^^^^^^^^^^^^^^^^^^^^^^^

    hashTable:f1 -> node_1_0:f0;
    node_1_0:f2  -> node_1_1:f0;
    hashTable:f4 -> struct3:f0;
}

Gives you what (I believe) you want:

enter image description here

...............

Original answer was:

Answering your question at face value, this can quickly be achieved by putting them into the same rank:

digraph structs 
{
    rankdir = LR;
    node [shape=record];

    hashTable [label="<f0>0|<f1>1|<f2>2|<f3>3|<f4>4|<f5>5|<f6>6|<f7>7|<f8>8"];
    node_1_0 [label="<f0> one|<f1> two |<f2> three"];
    node_1_1 [label="<f0> un |<f1> deux|<f2> trois"];
    struct3 [label="<f0> einz|<f1> swei|<f2> drei"];

    {rank = same; node_1_0 node_1_1 }

    hashTable:f1 -> node_1_0:f0;
    node_1_0:f2  -> node_1_1:f0;
    hashTable:f4 -> struct3:f0;
}

yielding

enter image description here

Rudolfrudolfo answered 3/12, 2017 at 6:26 Comment(2)
Why can rankdir = LR; combining with rank = same; produce vertical alignment, not horizontal alignment?Ludhiana
With rankdir = LR horizontal means order of dependence, so hierarchically, right is lower than left. rank = same means hierarchically the same level, which then necessarily is vertically aligned.Rudolfrudolfo

© 2022 - 2024 — McMap. All rights reserved.