Search code examples
graphviz

Graphviz - default subgraph / cluster style


Currently I always write subgraphs/clusters like this:

subgraph "cluster_001" {
    rank = TB;
    style = "filled";
    color = "#EEEEEE";
    004[label = "{<1>if\(gt \> 0\)|<2>else if\(gt == 0\)|<3>else if\(gt \< 0\)}"];
    005[label = "..."];
    006[label = "..."];
    007[label = "..."]
}

But I don't like this. I don't want to type first three lines for every subgraph/cluster. Is there a way to define subgraph/cluster styling just once? Like it is done for i.e. arrows:

edge[
    fontname = "IBM Plex Mono Bold",
    style = "dotted",
    fontsize = 8,
    arrowhead = normal,
    arrowsize = 0.4,
    penwidth = 0.1;
    fontcolor = "#444444",
    color = "#777777",
]

I define this once nad then every arrow is styled this way. This becomes the default.

I need something similar for clusters... How can it be done?


Solution

  • Much to my surprise, there is a built-in way. Put this before your cluster definitions:

    graph[ rank = TB;  style = "filled";   color = "#EEEEEE";]  
    

    Like so:

    digraph d {
      a -> b
      graph[ rank = TB;  style = "filled";   color = "#EEEEEE";]
      subgraph cluster1 {
       x1 ->x2->x3
       }
         subgraph cluster2 {
       y1 ->y2->y3
       }
         subgraph cluster3 {
       z1 ->z2->z3
       }
      {rank=same e->f}
    }
    

    Giving:
    enter image description here