如何在graphviz的点中控制级别节点顺序?

时间:2017-05-31 02:06:27

标签: graphviz dot graph-layout

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

如果只有A,B,C和D,我可以通过在输入点文件中按顺序放入B,C和D来获得此效果。但是如果B,C和/或D中有其他边缘,有时订单会被扰乱。这就是我想避免的。

enter image description here

4 个答案:

答案 0 :(得分:12)

这可以通过所示的“不可见”边缘来实现。请注意描述其工作原理的评论。

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

enter image description here

答案 1 :(得分:11)

为了帮助填写@ TomServo的答案(对于那些在"排名"中苦苦挣扎的人),我已经看到了隐形边缘:

After adding <code>rank1</code> and <code>rank2</code>.

答案 2 :(得分:3)

您不需要那些神奇的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;
}
}

答案 3 :(得分:3)

我遇到了同样的问题,发现魔法咒语是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];
}

graphviz tree