26

我有一个以树为骨干的图。例如,我有一个节点 A,其子节点 B、C 和 D。假设图形是自上而下绘制的,A 将在一个级别上,然后是 B、C 和 D。我想强制 graphviz在他们的等级内按 B、C、D 的顺序排列它们。这可能吗?如果是这样,怎么做?

如果只有 A、B、C 和 D,我只需将 B、C 和 D 按顺序放入输入点文件中即可获得此效果。但是,如果在 B、C 和/或 D 之外还有其他边,则有时订单会被打乱。这就是我想避免的。

在此处输入图像描述

4

4 回答 4

38

为了帮助填写@TomServo 的答案(对于那些为“排名”而苦苦挣扎的人),我已经使不可见的边缘可见:

添加 <code>rank1</code> 和 <code>rank2</code> 后。

于 2018-04-09T15:14:04.207 回答
34

这可以通过如图所示的“不可见”边缘来实现。请注意描述它如何工作的评论。

digraph test{

// make invisible ranks
rank1 [style=invisible];
rank2 [style=invisible];

// make "invisible" (white) link between them
rank1 -> rank2 [color=white];

// declare nodes all out of desired order
A -> D;
A -> B;
A -> C;
A -> E;

// even these new connection don't mess up the order
B -> F -> G;
C -> F -> G;

{
rank = same;
// Here you enforce the desired order with "invisible" edges and arrowheads
rank2 -> B -> C -> D -> E [ style=invis ];
rankdir = LR;
}
}

在此处输入图像描述

于 2017-05-31T02:18:58.620 回答
9

你不需要那些魔法rank1rank2.

只是:

  1. 像往常一样制作图表。
  2. 在子图中再次添加节点。
digraph test{

// declare nodes all out of desired order
A -> D;
A -> B;
A -> C;
A -> E;

B;C;D;E;

// even these new connection don't mess up the order
B -> F -> G;
C -> F -> G;

{
rank = same;
// Here you enforce the desired order with "invisible" edges and arrowheads
edge[ style=invis];
B -> C -> D -> E ;
rankdir = LR;
}
}

于 2020-09-22T09:55:13.150 回答
8

我碰到了同样的障碍,发现魔法咒语是ordering=out

我的完整示例如下所示:

digraph game_tree {
node [shape = circle, ordering=out];
f, h [shape=doublecircle, color=red];
k, n [shape=doublecircle, color=blue];
l, m [shape=doublecircle];
a -> b [label=1];
a -> c [label=2];
a -> d [label=3];
b -> e [label=4];
b -> f [label=5];
c -> g [label=4];
c -> h [label=5];
d -> i [label=4];
d -> j [label=5];
e -> k [label=6];
g -> l [label=6];
i -> m [label=7];
j -> n [label=8];
}

图形可视化树

于 2021-01-13T10:50:12.480 回答