Win*_*ith 152 graphics directed-graph dot graphviz subgraph
在DOT语言中GraphViz,我试图表示一个依赖关系图.我需要能够在容器内部拥有节点,并且能够使节点和/或容器依赖于其他节点和/或容器.
我subgraph用来代表我的容器.节点链接工作正常,但我无法弄清楚如何连接子图.
鉴于下面的程序,我需要能够连接cluster_1并cluster_2使用箭头,但我尝试过的任何东西都会创建新节点而不是连接集群:
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";
}
Run Code Online (Sandbox Code Playgroud)

Hig*_*ark 169
DOT用户手册给出了以下具有簇之间具有边缘的簇的图的示例
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;
}
Run Code Online (Sandbox Code Playgroud)
节点和集群之间的边缘.

Jon*_*ley 85
为便于参考,HighPerformanceMark答案中描述的解决方案直接应用于原始问题,如下所示:
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];
}
Run Code Online (Sandbox Code Playgroud)
将compound=true在graph声明中是至关重要的.这产生了输出:

请注意,我将边缘更改为引用群集中的节点,将ltail和lhead属性添加到每个边缘,指定群集名称,并添加图层级属性"compound = true".
关于人们可能想要连接其中没有节点的集群的担心,我的解决方案是始终向每个集群添加一个节点,使用style = plaintext进行渲染.使用此节点标记集群(而不是集群的内置"label"属性,该属性应设置为空字符串(在Python中label='""').这意味着我不再添加直接连接集群的边,但它在我的特殊情况下工作.