Search code examples
graphviz

Arranging subclusters with rank


I have this graph:

digraph G{

  rankdir = TB;
    hyb[label="Hybrid calculation"];   

    k1[label=<k<SUB>1</SUB>>];
    k2[label=<k<SUB>2</SUB>>];
    kn[label=<k<SUB>n</SUB>>];

    // subgraph cluster_k1q {
        k1q1[label=<q<SUB>1</SUB>>];
        k1q2[label=<q<SUB>2</SUB>>];
        k1qn[label=<q<SUB>n</SUB>>];

        graph[style=dotted];
        {rank=same; k1q1;k1q2;k1qn}
    // }
    
    // subgraph cluster_k2q {
        k2q1[label=<q<SUB>1</SUB>>];
        k2q2[label=<q<SUB>2</SUB>>];
        k2qm[label=<q<SUB>m</SUB>>];

        graph[style=dotted];
        {rank=same; k2q1;k2q2;k2qm}
    // }

    // subgraph cluster_knq {
        knq1[label=<q<SUB>1</SUB>>];
        knq2[label=<q<SUB>2</SUB>>];
        knql[label=<q<SUB>l</SUB>>];
        
        graph[style=dotted];
        {rank=same; knq1;knq2;knql}
    // }


    hyb -> k1;
    hyb -> k2;
    hyb -> kn;


    k1 -> k1q1;
    k1 -> k1q2;
    k1 -> k1qn;

    k2 -> k2q1;
    k2 -> k2q2;
    k2 -> k2qm;

    kn -> knq1;
    kn -> knq2;
    kn -> knql;

    bands1 -> k1q2;
    bands1 -> k2qm;
    bands1 -> knq1
    bands2 -> k1qn;
    bands2 -> knq1;
    bands3 -> knql;
    bands3 -> k1q1;

    {edge[ style=invis];
        k1->k2->kn;
        k1q1->k1q2->k1qn->k2q1->k2q2->k2qm->knq1->knq2->knql;
    }

    { rank=min; hyb}
    { rank=same; k1;k2;kn}
    { rank=same; k1q1;k1q2;k1qn;k2q1;k2q1;k2qm;knq1;knq2;knql}
    { rank=max; bands1;bands2;bands3}
}

Resulting in this graph: enter image description here

All the ks are on one level, all the qs are one level and so are the bands. Then I would like to draw some boxes around the qs using clusters. So if I uncomment the subgraph in above code I have to comment this line:

{ rank=same; k1q1;k1q2;k1qn;k2q1;k2q1;k2qm;knq1;knq2;knql}

and I get:

enter image description here Here the bands get thrown in same level with the qs. How can I get the levels from the top graph with the nice boxes from the bottom graph?


Solution

  • add this: newrank=true
    See: https://graphviz.org/docs/attrs/newrank/
    (you could also probably accomplish it by changing the direction of the bandX->qY edges, like so: k1q2->bands1 [dir=back])