Search code examples
graphvizdiagramdotsubgraph

How to order nodes inside a subgraph in dot


I can't get the nodes to appear in the same order I wrote them

Here is the code I'm trying to use with dot:

digraph G {
        fontname = "Hilda 10"
        rankdir=LR
        splines=line
        nodesep=.08;
        ranksep=1;
        edge [color=black, arrowsize=.5];
        node [fixedsize=true,style=filled,color=none,fillcolor=gray,shape=circle,fontcolor=black]

        subgraph cluster_0 {
                color=none;
                node [style=filled, color=white, penwidth=15,fillcolor=gray shape=circle];
                { l10 l11 }
                label = Input;
        }

        subgraph cluster_1 {
                color=none;
                node [style=filled, color=white, penwidth=15,fillcolor=gray shape=circle];
                { l20 l21 l22 }
                label = Output;
        }

        l10 -> l20;
        l10 -> l21;
        l10 -> l22;
        l11 -> l20;
        l11 -> l21;
        l11 -> l22;
}

In my second column I was hoping to get l20 on top followed by l21 and l22 on the bottom, but instead I'm getting the image below, with l21 on top and l20 on the bottom

Result

I also tried with:

        rankdir=TB
 

...

        rank=same 
        l20 -> l21 -> l22 [style = invis]

...

      edge [constraint=false]
        l10 -> l20;
        l10 -> l21;
        l10 -> l22;
        l11 -> l20;
        l11 -> l21;
        l11 -> l22;

but this way the first column loses its vertical centering 2nd try


Solution

  • A really annoying problem. There are already MRs against this problem. (search https://gitlab.com/graphviz/graphviz/-/issues for "order")

    Here is a solution, but it requires changing the ranking and then adding one invisible node (plus other tweaks).

    digraph G {
            fontname = "Hilda 10"
    //       rankdir=LR  // change ranking
            splines=line
      ranksep=.05
      nodesep=.05
            edge [color=black, arrowsize=.5];
            node [fixedsize=true,style=filled,color=none,fillcolor=gray,shape=circle,fontcolor=black]
    
            subgraph cluster_0 {
                    color=none;
                    node [style=filled, color=white, penwidth=15,fillcolor=gray shape=circle];
            
                    {
            // add an inviz node to drop nodes down 1 rank
            spacer1 [label="" style=invis]
            spacer1 -> l10 [minlen=1 style=invis]
            edge [style=invis minlen=2]  // 2 ranks between
            l10 -> l11
            }
                    label = Input;
            }
    
            subgraph cluster_1 {
                    color=none;
                    node [style=filled, color=white, penwidth=15,fillcolor=gray shape=circle];
                    { XXrank=same
            XXordering=out
            edge [style=invis]
    
            edge[minlen=2]
            l20 -> l21 -> l22
            }
                    label = Output;
            }
    
    edge[constraint=false]
            l10 -> l20;
            l10 -> l21;
            l10 -> l22;
            l11 -> l20;
            l11 -> l21;
            l11 -> l22;
    }
    

    Giving:
    enter image description here