How can I center nodes in a Graphviz cluster?

  • here is the place to make enhancement requests & bug reports Issues · graphviz / graphviz · GitLab. But there is quite a backlog, do not hold your breath for an enhancement
  • nodesep is probably the worst way to produce a “good” graph, all the nodes are affected
  • margin applied to the two clusters might be a bit better, but not much
  • the underlying problem is caused by the wide cluster labels. They cause cluster bounding-boxes that are much wider that required by the enclosed graph. Quite “legal”, but Graphviz does not have a “center graph within the cluster bounding box” option.
  • Below is a “pretty good” solution that :
    • adds 2 new levels of clusters
    • explicitly sets rank for a new subgraph containing root
    • sets constraint=false for some edges
      Note that the two side-by-side clusters have flipped left & right. If that is a problem, try reversing the order within the file or adding another enclosing cluster
digraph G {
bgcolor="gray"

node [
style=filled
shape="circle"
fillcolor="green4"
penwidth="2"
color="green1"
fontcolor="white"
]

{
rank=source
root [
   shape="box"
   color="white"
   fontcolor="white"
   label="hello"
]
}

subgraph clusterXXX{
peripheries=0
AAA_1 [
    label="AAA"
]

BBB_2 [
    label="BBB"
]

subgraph cluster_ccc {
    color=gray
    fillcolor="blue"
    style=filled
    label="ccccc"
    labelloc=b
    fontcolor="white"
    labeljust=l
peripheries=1

    subgraph cluster_south_a {
      label="aaaaaaaaaaaaaaaaaaaaaa"
      subgraph cluster_south_a1 {
        label="" peripheries=0
        5 [ label="" fillcolor="red" ]
        6 [ label="" fillcolor="red" ]

        5 -> 6
      }
    }


    subgraph cluster_south_b {
      label="bbbbbbbbbbbbbbbbbbbbbb"
      subgraph cluster_south_b1 {
        label="" peripheries=0
        7 [ label="" fillcolor="red" ]
        8 [ label="" fillcolor="red" ]
        9 [ label="" fillcolor="red" ]
        10 [ label="" fillcolor="red" ]

        7 -> 8
        7 -> 8
        7 -> 9
        8 -> 9
        8 -> 10
        9 -> 10
      }
    }
  }
}

{
edge [constraint=false]
root -> AAA_1
root -> BBB_2
}
AAA_1 -> 7
BBB_2 -> 5
}

Giving: