How can I center nodes in a Graphviz cluster

47 views Asked by At

I have the follow dot definition of a graph:

digraph G {
bgcolor="gray"

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

root [
   shape="box"
   color="white"
   fontcolor="white"
   label="hello"
]

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

    subgraph cluster_south_a {
        label="aaaaaaaaaaa"

        5 [ label="" fillcolor="red" ]
        6 [ label="" fillcolor="red" ]

        5 -> 6
    }

    subgraph cluster_south_b {
        label="bbbbbbbbbbb"

        7 [ label="" fillcolor="red" ]
        8 [ label="" fillcolor="red" ]

        7 -> 8
    }
}

root -> AAA_1
root -> BBB_2

AAA_1 -> 7
BBB_2 -> 5
}

Which produces the output:

result image

The red nodes are not horizontally centered in their cluster.

It would also need to work if cluster_south_b was updated to

    subgraph cluster_south_b {
        label="bbbbbbbbbbbbbbbbbbbbbb"

        7 [ label="" fillcolor="red" ]
        8 [ label="" fillcolor="red" ]
        9 [ label="" fillcolor="red" ]
        10 [ label="" fillcolor="red" ]

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

which produces the output of:

result image

What comes close is adding the graph attribute nodesep=.75 just below bgcolor="gray" (that was suggested on the Graphviz forum). But manually fine-tuning that value is not a practical solution for me.

Is it possible to have them horizontally centered? If so, how?

1

There are 1 answers

2
sroush On

There is no guaranteed way to center a set of nodes within a larger (wider) cluster, but this seems to work pretty well

  • put the nodes in a separate, embedded cluster - now somewhat disconnected from the wide label
  • use constraint=false to help keep AAA and BBB from "pulling" the new clusters away from the center
  • use **rank=source" to keep root at the top
  • whoops, and use peripheries=0 to visually hide the cluster box
        label=""
    peripheries=1
        7 [ label="" fillcolor="red" ]
        8 [ label="" fillcolor="red" ]

        7 -> 8
    }
    }

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

and

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=1
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=1
        5 [ label="" fillcolor="red" ]
        6 [ label="" fillcolor="red" ]

        5 -> 6
      }
    }


    subgraph cluster_south_b {
      label="bbbbbbbbbbbbbbbbbbbbbb"
      subgraph cluster_south_b1 {
        label="" peripheries=1
        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:
enter image description here

and
enter image description here