How to draw three squares, one inside another, with Graphviz
Asked Answered
S

1

5

I'm using Graphviz tool in dot language using Linux. I want to draw three squares, one inside another. The below code is incorrect:

graph A
 { label="a";

   subgraph cluster_A
     {
        b [shape=box,label="b"];
           subgraph cluster_b
                { 
                 c[label="c",shape=box];
                }
     }

 }
Slack answered 28/4, 2015 at 9:2 Comment(1)
I'm not sure why this is downvoted. It's a valid question with attempted code.Impulsive
E
9

you have multiple possibilities to do that

  • box node in cluster in cluster
  • plaintext node in cluster in cluster in cluster
  • node with HTML like label and HTML tables

box in clusters:

graph "graph A"
{
    label="\G"
    subgraph "cluster A"
    {
        subgraph "cluster B"
        {
            c[shape=box];
        }
    }
}

enter image description here

plaintext in clusters:

graph "graph A"
{
    label="\G"
    subgraph "cluster A"
    {
        subgraph "cluster B"
        {
            subgraph "cluster C"
            {
                d[shape=none];
            }
        }
    }
}

enter image description here

both variants have the labels set to their names which is default for nodes but not for graphs (and all included subgraphs). as the graph label is inherited you can either set all labels manually or use the name palceholder as I did.

for HTML like lables

graph "graph A"
{
    label="\G"
    a [shape=none label=<<table><tr><td><table><tr><td><table><tr><td>node a</td></tr></table></td></tr></table></td></tr></table>>];
}

you have a lot more of freedom in formatting (margin, padding, border, ...)

enter image description here

Exemplar answered 29/4, 2015 at 9:8 Comment(2)
I am super confused, but it looks like the sub-graph's name needs to start with cluster, otherwise, this does not work?Chinchin
@Chinchin A cluster is defined as a subgraph where the name starts wirth "cluster". It may have a different label. graphviz.org/Gallery/directed/cluster.htmlExemplar

© 2022 - 2024 — McMap. All rights reserved.