English 中文(简体)
GraphViz - How to connect subgraphs?
原标题:

In the DOT language for GraphViz, I m trying to represent a dependency diagram. I need to be able to have nodes inside a container and to be able to make nodes and/or containers dependent on other nodes and/or containers.

I m using subgraph to represent my containers. Node linking works just fine, but I can t figure out how to connect subgraphs.

Given the program below, I need to be able to connect cluster_1 and cluster_2 with an arrow, but anything I ve tried creates new nodes instead of connecting the clusters:

digraph G {

    graph [fontsize=10 fontname="Verdana"];
    node [shape=record fontsize=10 fontname="Verdana"];

    subgraph cluster_0 {
        node [style=filled];
        "Item 1" "Item 2";
        label = "Container A";
        color=blue;
    }

    subgraph cluster_1 {
        node [style=filled];
        "Item 3" "Item 4";
        label = "Container B";
        color=blue;
    }

    subgraph cluster_2 {
        node [style=filled];
        "Item 5" "Item 6";
        label = "Container C";
        color=blue;
    }

    // Renders fine
    "Item 1" -> "Item 2";
    "Item 2" -> "Item 3";

    // Both of these create new nodes
    cluster_1 -> cluster_2;
    "Container A" -> "Container C";
}

enter image description here

最佳回答

The DOT user manual gives the following example of a graph with clusters with edges between clusters:

IMPORTANT: The initial compound=true statement is required.

digraph G {
  compound=true;
  subgraph cluster0 {
    a -> b;
    a -> c;
    b -> d;
    c -> d;
  }
  subgraph cluster1 {
    e -> g;
    e -> f;
  }
  b -> f [lhead=cluster1];
  d -> e;
  c -> g [ltail=cluster0,lhead=cluster1];
  c -> e [ltail=cluster0];
  d -> h;
}

... and edges between nodes and clusters:

enter image description here

问题回答

For ease of reference the solution described in HighPerformanceMark s answer, applied directly to the original question, looks like this:

digraph G {

    graph [fontsize=10 fontname="Verdana" compound=true];
    node [shape=record fontsize=10 fontname="Verdana"];

    subgraph cluster_0 {
        node [style=filled];
        "Item 1" "Item 2";
        label = "Container A";
        color=blue;
    }

    subgraph cluster_1 {
        node [style=filled];
        "Item 3" "Item 4";
        label = "Container B";
        color=blue;
    }

    subgraph cluster_2 {
        node [style=filled];
        "Item 5" "Item 6";
        label = "Container C";
        color=blue;
    }

    // Edges between nodes render fine
    "Item 1" -> "Item 2";
    "Item 2" -> "Item 3";

    // Edges that directly connect one cluster to another
    "Item 1" -> "Item 3" [ltail=cluster_0 lhead=cluster_1];
    "Item 1" -> "Item 5" [ltail=cluster_0 lhead=cluster_2];
}

It is vital to:

  • Use compound=true in the graph declaration
  • Name the subgraphs cluster_*
  • According to a comment below, not start the cluster name with a capital letter. I m not precisely sure what that means (eg. does it forbid cluster_A?), but wanted to highlight it here in case it is a gotcha.

That produces output:

graph with connected clusters

Note that I changed the edges to reference nodes within the cluster, added the ltail and lhead attributes to each edge, specifying the cluster name, and added the graph-level attribute compound=true .

Regarding the worry that one might want to connect a cluster with no nodes inside it, my solution (not shown above) has been to always add a node to every cluster, rendered with style=plaintext. Use this node to label the cluster (instead of the cluster s built-in "label" attribute, which should be set to the empty string (in Python, label= "" ). This means I m no longer adding edges that connect clusters directly, but it works in my particular situation.

Make sure you are using fdp layout for the file. I don t think neato supports clusters.

Make sure that compound=true in the digraph options (reference):

digraph {
  compound=true;

  subgraph cluster_a {
    label="Cluster A";
    node1; node3; node5; node7;
  }
  subgraph cluster_b {
    label="Cluster B";
    node2; node4; node6; node8;
  }

  node1 -> node2 [label="1"];
  node3 -> node4 [label="2" ltail="cluster_a"];
  
  node5 -> node6 [label="3" lhead="cluster_b"];
  node7 -> node8 [label="4" ltail="cluster_a" lhead="cluster_b"];
}

enter image description here





相关问题
OpenGL 3D Selection

I am trying to create a 3D robot that should perform certain actions when certain body parts are clicked. I have successfully (sort of) implemented picking in that if you click on any x-plane part, it ...

Why and when should you use hoops 3d graphics? [closed]

My company needs 3D visualization for our commercial applications (CAD, mesh manipulation, computational geometry). We are tired of true vision 3D (tv3d), which we ve been using for years (poor ...

Prerequisite for learning directx

I am from .net C# background and I want to learn DirectX. I have knowledge of C++ but I am fairly new to graphic world. I am little confused about how to start learning directx, should I start ...

Radial plotting algorithm

I have to write an algorithm in AS3.0 that plots the location of points radially. I d like to input a radius and an angle at which the point should be placed. Obviously I remember from geometry ...

Determine VRAM size on windows

I need to determine roughly how much VRAM a system s graphics card has. I know all the reasons why I shouldn t but I do. It doesn t need to be perfect (some cards lie etc.) but I need a ballpark. On ...

Mask white color out of PNG image on iPhone

I know that there are already 2 relevant posts on that, but it s not clear... So the case is this...: I have a UIImage which consists of a png file requested from a url on the net. Is it possible to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签