如何使用graphviz绘制存储在节点中的值的树?

时间:2014-06-22 17:10:00

标签: graphviz dot

我需要绘制这样的树(与某些节点关联的值):

Requested view for tree

但我发现只有下一个绘制树的方法: enter image description here

我尝试使用子图和嵌套子图,但不需要效果。目前,我使用下一个gv脚本:

digraph "test-graph" {
    graph [rankdir=TB dpi=96]
    subgraph n0 {
        rank=same;
        "0_0" [ label = "a" ];
    }
    subgraph n1 {
        rank=same;
        "1_0" [ label = "b" ];
        "1_1" [ label = "d" ];
        "1_2" [ label = "e" ];
        subgraph {
            "v3" [ label = "3", shape = "box" ];
            "1_1"->"v3" [ dir = none, constraint = false ];
            }
    }
    subgraph n2 {
        rank=same;
        "2_0" [ label = "g" ];
    }
    subgraph n3 {
        rank=same;
        "3_0" [ label = "a" ];
        "3_1" [ label = "b" ];
        "3_2" [ label = "c" ];
        subgraph {
            "v2" [ label = "2", shape = "box" ];
            "3_0"->"v2" [ dir = none, constraint = false ];
            "v0" [ label = "0", shape = "box" ];
            "3_1"->"v0" [ dir = none, constraint = false ];
            "v5" [ label = "5", shape = "box" ];
            "3_2"->"v5" [ dir = none, constraint = false ];
           }
    }
    subgraph n4 {
        rank=same;
        "4_0" [ label = "f" ];
        subgraph {
            "v1" [ label = "1", shape = "box" ];
            "4_0"->"v1" [ dir = none, constraint = false ];
            }
    }
    subgraph n5 {
        rank=same;
        "5_0" [ label = "c" ];
        subgraph {
            "v4" [ label = "4", shape = "box" ];
            "5_0"->"v4" [ dir = none, constraint = false ];
            }
    }
    "start"->"0_0";
    "0_0"->"1_0";
    "0_0"->"1_1";
    "0_0"->"1_2";
    "1_0"->"5_0";
    "1_1"->"2_0";
    "1_2"->"4_0";
    "2_0"->"3_0";
    "2_0"->"3_1";
    "2_0"->"3_2";

}

使用“cluster_”前缀可以解决一些问题(感谢Anne),但是点序列中的节点和值的重要性很重要。

毕竟我遇到了新的问题 - 节点没有在图上正确排序:a,b,c节点(节点'g'子节点)的顺序错误:enter image description here

另外,有没有办法告诉节点右侧的点位值?当然,最好的是将值放在节点下,但是,我怎么能找到,这是无法解决的问题。

点脚本是:

digraph "test-graph" {
    graph [rankdir=TB dpi=96]
    subgraph cluster_n0 {
        rank=same;
        style=invis;
        "0_0" [ label = "a" ];
    }
    subgraph cluster_n1 {
        rank=same;
        style=invis;
        "1_0" [ label = "b" ];
        "1_1" [ label = "d" ];
            "v3" [ label = "3", shape = "box", width=.01, height=.01 ];
            "1_1"->"v3" [ dir = none, constraint = false ];
        "1_2" [ label = "e" ];
    }
    subgraph cluster_n2 {
        rank=same;
        style=invis;
        "2_0" [ label = "g" ];
    }
    subgraph cluster_n3 {
        rank=same;
        style=invis;
        "3_0" [ label = "a" ];
            "v2" [ label = "2", shape = "box", width=.01, height=.01 ];
            "3_0"->"v2" [ dir = none, constraint = false ];
        "3_1" [ label = "b" ];
            "v0" [ label = "0", shape = "box", width=.01, height=.01 ];
            "3_1"->"v0" [ dir = none, constraint = false ];
        "3_2" [ label = "c" ];
            "v5" [ label = "5", shape = "box", width=.01, height=.01 ];
            "3_2"->"v5" [ dir = none, constraint = false ];
    }
    subgraph cluster_n4 {
        rank=same;
        style=invis;
        "4_0" [ label = "f" ];
            "v1" [ label = "1", shape = "box", width=.01, height=.01 ];
            "4_0"->"v1" [ dir = none, constraint = false ];
    }
    subgraph cluster_n5 {
        rank=same;
        style=invis;
        "5_0" [ label = "c" ];
            "v4" [ label = "4", shape = "box", width=.01, height=.01 ];
            "5_0"->"v4" [ dir = none, constraint = false ];
    }
    "start"->"0_0";
    "0_0"->"1_0";
    "0_0"->"1_1";
    "0_0"->"1_2";
    "1_0"->"5_0";
    "1_1"->"2_0";
    "1_2"->"4_0";
    "2_0"->"3_0";
    "2_0"->"3_1";
    "2_0"->"3_2";

}

2 个答案:

答案 0 :(得分:1)

将"值"分组如果节点有节点,则必须使用名称以cluster_开头的子图。例如:

subgraph cluster_n4_0 {
  rank=same;
  "4_0" [ label = "f" ];
  "v1" [ label = "1", shape = "box" ];
  "4_0"->"v1" [ dir = none];
}

要为值设置较小的节点,您可以使用widthheight属性。

我不认为你在这里需要几个级别的子图,使用群集对节点应该是enougt。

答案 1 :(得分:0)

从框中添加一个不可见的边到下一级节点,如下面的示例

digraph "test-graph" {
    graph [rankdir=TB,dpi=96,splines=false]
    "1_1" [ label = "d" ];
    "v3" [ label = "3", shape = "box", width = 0, height = 0];
    "2_0" [ label = "g" ];

    "1_1"->"v3" [ dir = none];
    v3->"2_0"[style=invisible,dir = none];
    "1_1"->"2_0";
}