Search code examples
graphviz

Is it possible to define subgraphs (clusters) in Graphviz with a specified, fixed layout


How can I create subgraphs/clusters in Graphviz, with nodes at specific positions, and then have Graphviz rotate and arrange these subgraphs according to a set of interconnecting edges (i.e. edges not within the subgraphs)?

Here's my current attempt. Problem is that I want to be able to define the internal ordering of the nodes, such that, as an example, the nodes in subgraph "3" are aligned in a row (and always ordered A-B-C):

graph {
    forcelabels=true
    color=lightgrey

    subgraph cluster_1 {
        node[shape=point]  
        {rank=same r1A r1B}
        label="1"
        r1A [xlabel="A", fontsize=7]
        r1B [xlabel="B", fontsize=7]
    }
    subgraph cluster_2 {
        node[shape=point]
        {rank=same r2A r2B}
        label="2"
        r2A [xlabel="A", fontsize=7]
        r2B [xlabel="B", fontsize=7]
    }

    subgraph cluster_3 {
        node[shape=point]
        {rank=same volA volB volC}
        label="3"
        volA [xlabel="A", fontsize=7]
        volB [xlabel="B", fontsize=7]
        volC [xlabel="C", fontsize=7]
    }
}

enter image description here

If I add edges, I want Graphviz to be able to rotate the subgraphs, but while preserving that internal layout and ordering. Here, using engine fdp, subgraph "3" gets all rearranged (for obvious reasons, since I haven't got any way of fixing the layout):

enter image description here

And what about more advanced "layouts"? Is it possible/feasible to make a subgraph resembling a microcontroller, something like this:

enter image description hereenter image description here


Solution

    • Unfortunately, none of the Graphviz engines do node rotation. You would have to create a complete engine for node rotation (the architecture supports this) or a pre/post processor just for node rotation (more later)
    • Also Graphviz only seems to support text rotation at a Root graph level. This might be a killer, depending on your requirements.
    • If you want to use clusters (suggestion: nope), you probably need to use the dot engine to line up your nodes.
    • Html-records (possibly with included images) would probably be a "better" choice for the ICs. Again, none of the engines will rotate them, but ...
    • Cycles are cheap, so if there are not "too many" ICs, generate all possibilities (each IC rotated & not) and then eyeball evaluate or write a post-processing program to determine the "best" result.
    • Also note that Graphviz edge routing might not produce what you want.