81

I'm trying to create a graph with two subgraphs in dot. The code is as follows:

digraph G {
        subgraph step1 {
                style=filled;
                node [label="Compiler"] step1_Compiler;
                node [label="Maschine"] step1_Maschine;
                color=lightgrey;
        }

        subgraph step2 {
                style=filled;
                color=lightgrey;
                node [label="Interpretierer"] step2_Interpretierer;
                node [label="Maschine"] step2_Maschine;
                label="Virtuelle Maschine";
        }

        "Programm (Java)" -> step1_Compiler;
        step1_Compiler -> step1_Maschine;
        step1_Maschine -> "Bytecode";
        "Bytecode" -> step2_Interpretierer;
        step2_Interpretierer -> step2_Maschine;
        step2_Maschine -> "Ergebnis";
}

The result I am getting looks like the following:

Result of above code

I expected to see a box around both subgraphs. What am I missing here?

halfdan
  • 33,545
  • 8
  • 78
  • 87

1 Answers1

163

You'll have to prefix the name of your subgraphs with cluster:

subgraph clusterstep1 {

and

subgraph clusterstep2 {

in order to get the style and label.

From the graphiz documentation, section "Subgraphs and Clusters":

The third role for subgraphs directly involves how the graph will be laid out by certain layout engines. If the name of the subgraph begins with cluster, Graphviz notes the subgraph as a special cluster subgraph. If supported, the layout engine will do the layout so that the nodes belonging to the cluster are drawn together, with the entire drawing of the cluster contained within a bounding rectangle. Note that, for good and bad, cluster subgraphs are not part of the DOT language, but solely a syntactic convention adhered to by certain of the layout engines.

marapet
  • 54,856
  • 12
  • 170
  • 184
  • So will subgraph and subgraph cluster* both result in a visual grouping under the layout algorithms? – Chris Nov 24 '13 at 11:29
  • 16
    Prefixing the name with `cluster` is such a stupid design choice... Why isn's keyword `subgraph` enough??? – 71GA Apr 11 '21 at 18:12
  • @71GA: You are absolutely right, but why ask this here? File a bug against graphviz. – einpoklum Feb 03 '22 at 13:17
  • 3
    @71GA: Filed it myself... https://gitlab.com/graphviz/graphviz/-/issues/2187 – einpoklum Feb 03 '22 at 13:40
  • 2
    Thank you for this! I had a file with two subgraphs, one of which *happened to be called* `cluster` and I couldn't figure out why only one of them was visible ... – Joachim Sauer Mar 02 '22 at 16:47