2010-01-06 29 views
116

DOT语言GraphViz,我试图表示一个依赖关系图。我需要能够在容器内部有节点,并且能够使节点和/或容器依赖于其他节点和/或容器。GraphViz - 如何连接子图?

我使用subgraph来表示我的容器。节点链接工作得很好,但我无法弄清楚如何连接子图。

下面给出的方案,我需要能够有一个箭头连接cluster_1cluster_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"; 
} 

enter image description here

+2

我有同样的问题,但他们有一个自然的例子,其中子图像节点一样,http://www.graphviz.org/content/fdpclust。 – nlucaroni 2011-06-17 18:26:53

+1

@nlucaroni我不知道这个问题是否已经解决,这个例子给了我一个错误的图:边连接子图的中心,你不知道如何让它像在例子中一样工作? – k102 2012-02-14 09:14:28

+1

@ k102,我知道。再次检查该页面;它说你需要使用'fdp'。链接的例子和上面的例子都工作(这里例子中的最后一行需要使用子图名称而不是标签,并且可能会很好地包含图表的行长);它有点紧)。 – nlucaroni 2012-02-14 18:24:39

回答

124

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; 
} 

和节点和群集之间的边缘。

enter image description here

+8

谢谢 - 这有效,但它真的感觉像一个丑陋的黑客。我**希望**我没有一个场景,我有一个没有节点的容器。 – 2010-01-06 11:24:54

+4

如果有人感兴趣,如果您标记了链接(边缘),则可能导致定位问题。虽然边缘的头部或尾部可能隐藏在群集下,但标签仍然位于中点,这意味着某些边缘标签似乎浮在群集上,而不是由边缘本身定位。 – 2010-01-08 11:12:38

+0

这个答案确实帮了我很大的忙,现在我只需要研究一下我是否可以使用neato来创建群集:) – 2011-03-03 02:07:06

66

为了便于参考在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]; 
} 

在“化合物=真”中的“图表”声明至关重要。产生输出:

graph with connected clusters

注意,我改变边缘到集群中引用节点,加入ltail和lhead属性每个边缘,指定群集名称,并添加了图形级属性“化合物=真”。

关于人们可能想要连接一个没有节点的集群的担心,我的解决方案是总是为每个集群添加一个节点,使用style = plaintext呈现。使用此节点来标记群集(而不是群集的内置“标签”属性,该属性应该设置为空字符串(在Python中为label='""')。这意味着我不再添加直接连接群集的边缘,但是它在我的特殊情况。

+18

注意:'graph [fontsize = 10 fontname =“Verdana”compound = true];'是必不可少的 - 如果你错过了链接到ltail/lhead不起作用。 – 2012-12-17 16:34:30

+1

@JonathanHartley,根据你最后的段落,是否有任何方法可以将该节点置于集群中间? – Pacerier 2014-07-16 18:29:59

+0

集群的名称也不应该以大写字母 – JCLL 2016-09-09 08:29:59

8

确保您使用fdp布局的文件。我不认为neato支持集群。

+2

我也经验地发现'neato'引擎不支持集群..我不确定这是否是一个错误.. – 2013-07-30 22:43:05