Graphviz subgraph doesn't get visualized

halfdan picture halfdan · Sep 28, 2011 · Viewed 17.9k times · Source

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?

Answer

marapet picture marapet · Sep 28, 2011

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.