首页 文章

GraphViz - 如何连接子图?

提问于
浏览
136

DOTDOT 语言中,我试图表示一个依赖关系图 . 我需要能够在容器内部拥有节点,并且能够使节点和/或容器依赖于其他节点和/或容器 .

我正在使用 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

3 回答

  • 74

    为便于参考,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 声明中的 compound=true 至关重要 . 这产生了输出:

    graph with connected clusters

    请注意,我将边缘更改为引用群集中的节点,将ltail和lhead属性添加到每个边缘,指定群集名称,并添加图层级属性“compound = true” .

    关于人们可能想要连接其中没有节点的集群的担心,我的解决方案是始终向每个集群添加一个节点,使用style = plaintext进行渲染 . 使用此节点标记集群(而不是集群的内置"label"属性,该属性应设置为空字符串(在Python中, label='""' ) . 这意味着我不再添加直接连接集群的边,但它可以工作在我的特殊情况下 .

  • 8

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

  • 147

    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

相关问题