graphvizstate-machinedotstatechart

Graphviz - Connect cluster (subgraph) directly to a node


I want to connect a cluster edge to a node. Please find a simple Graphviz code below. Here is the online demo.

digraph G {
    compound=true;
    node1
    subgraph cluster0 {
        label="cluster"
        node2;
     }
    node2 -> node1 [ltail="cluster0"]
}

It produces this output:

enter image description here

So, in the above method the cluster0 is connected to a node1. node2 is inside the cluster.

To connect the cluster itself to node we need compound=true and [ltail="cluster0"]. So we are just connecting node2 to node1 behind the scene and generating the edge from behind the cluster.

node2 -> node1 [ltail="cluster0"]

What I really want is to connect the cluster itself in the code like:

digraph G {
    compound=true;
    node1
    subgraph cluster0 {
        label="cluster"
        node2;
     }
    cluster0 -> node1 [ltail="cluster0"]
}

But unfortunately, it produces this:

enter image description here

Well, the first one produces the image as desired but not the right approach. I'm working with State machines and generating the Graphviz code programmatically. Logically speaking, there is only one transition in the state digram:

cluster -> node1

But to do so we're trasitioning from node2 to node1 which is logically not right. I am curious that there is a way to achieve this and connect the cluster directly to a node instead of using a node inside the cluster.

Thanks in advance!


Solution

  • The ltail attribute is what is connecting it with the cluster edge.

    (It seems like what you say is correct, that it's connecting the nodes on the back end but covering the edge when it gets to the cluster.)

    Removing the ltail attribute should fix it:

    digraph G {
        compound=true;
        node1
        subgraph cluster0 {
            label="cluster"
            node2;
         }
    node2 -> node1
        
    }
    

    enter image description here