graphviz 树形布局

发布于 2024-09-13 10:03:56 字数 679 浏览 4 评论 0原文

我是第一次使用 graphviz。我只需要一个树布局,以便所有子级都处于同一级别。

例如, A→B A→C A->D

那么 B、C 和 D 应该处于同一水平。

以下是我正在使用的代码。

digraph unix {
    size="6,6";
    node [color=lightblue2, style=filled];

    "A:1000" -> "B:300";
    "A:1000" -> "C:300";
    "A:1000" -> "D:200";
    "B:300" -> "E:140";
    "B:300" -> "F:164";
    "B:300" -> "G:75";
    "C:300" -> "H:135";
    "C:300" -> "I:91";
    "D:200" -> "E:140";
    "D:200" -> "F:164";
    "D:200" -> "G:75";
    "E:140" -> "F:164";
    "E:140" -> "G:75";
    "F:164" -> "G:75";
    "G:75" -> "H:135";
    "H:135" -> "I:91";
}

如何确保孩子处于同一水平?

I am using graphviz for the first time. I just need a tree layout so that all the childs are at the same level.

For example,
A->B
A->C
A->D

THEN B, C AND D SHOULD BE AT THE SAME LEVEL.

Following is the code I am using.

digraph unix {
    size="6,6";
    node [color=lightblue2, style=filled];

    "A:1000" -> "B:300";
    "A:1000" -> "C:300";
    "A:1000" -> "D:200";
    "B:300" -> "E:140";
    "B:300" -> "F:164";
    "B:300" -> "G:75";
    "C:300" -> "H:135";
    "C:300" -> "I:91";
    "D:200" -> "E:140";
    "D:200" -> "F:164";
    "D:200" -> "G:75";
    "E:140" -> "F:164";
    "E:140" -> "G:75";
    "F:164" -> "G:75";
    "G:75" -> "H:135";
    "H:135" -> "I:91";
}

How do I make sure that the childs are at the same level?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

|煩躁 2024-09-20 10:03:56

要使节点处于同一级别,例如“B:300”和“C:300”,请添加以下行:

{rank=same; "B:300" "C:300"}

To get nodes on the same level, say "B:300" and "C:300", add the following line:

{rank=same; "B:300" "C:300"}
海未深 2024-09-20 10:03:56

您提供的图并不代表一棵树,而是一个有向无环图(在树中,每对节点之间只有一条不同的路径)。如果您的输入是一棵树,那么只需使用 dot 就会产生您想要的结果。如果您还想添加非树边,例如 "C:300" ->在您的示例中,“H:135” 可以为它们指定较低的权重,以确保 dot 不会尝试针对这些边缘优化布局。

"C:300" -> "H:135" [weight=0];
"C:300" -> "I:91" [weight=0];

请注意,使用此设置,这两条边会变得非常长(并且变成点,丑陋),这就是节点 "C:300" 被按原样放置的原因在你的原始图表中。

The graph you presented does not represent a tree, but a directed acyclic graph (In a tree there is only one distinct path between every pair of nodes). If your input would have been a tree, then just using dot would produce what you want. If you also want to add non-tree edges, like "C:300" -> "H:135" in your example, you could specify a lower weight for them, to make sure that dot doesn't try to optimize the layout with respect to these edges.

"C:300" -> "H:135" [weight=0];
"C:300" -> "I:91" [weight=0];

Note that these two edges become very long (and to dot, ugly) with this setting, and this is the reason why the node "C:300" is placed as it is in your original graph.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文