保持Graphviz中节点的水平顺序
我有以下点文件内容:保持Graphviz中节点的水平顺序,graphviz,dot,directed-acyclic-graphs,Graphviz,Dot,Directed Acyclic Graphs,我有以下点文件内容: digraph G { start -> {a0, b0} -> end; start -> c0 -> c1 -> c2 -> end; start -> d0 -> d1 -> d2 -> end; start -> {e0, f0} -> end; subgraph cluster_test { { rank = s
digraph G {
start -> {a0, b0} -> end;
start -> c0 -> c1 -> c2 -> end;
start -> d0 -> d1 -> d2 -> end;
start -> {e0, f0} -> end;
subgraph cluster_test {
{
rank = same;
a0; b0; c0; d0; e0; f0;
}
{
rank = same;
c1; d1;
}
{
rank = same;
c2; d2;
}
}
}
结果图如下所示:
我想要的是保持0级节点的顺序,也就是说,我想要a0,b0在水平方向上位于c0,d0之前
如何实现这一点?空节点、带权重的边以及集群中最上面一行的显式排序都有帮助。请参见下面带有注释的代码:
digraph so
{
// we define all nodes in the beginning, before edges and clusters
// may not be essential but I think it's good practice
start
a0 b0 c0 d0 e0 f0
c1 d1
c2 d2
end
// we define "empty" nodes that can be used to route the edges
node[ shape = point, height = 0 ];
ax bx ex fx
subgraph cluster_test
{
// we need to keep explicit order of the top nodes in the cluster
{ rank = same; a0 -> b0 -> c0 -> d0 -> e0 -> f0[ style = invis ] }
// the original layout in the cluster, empty nodes added at the bottom
{ rank = same; c1 d1 }
{ rank = same; ax bx c2 d2 ex fx }
c0 -> c1 -> c2;
d0 -> d1 -> d2;
// routing through invisible nodes keeps the position of all other nodes
// edges with no arrowheads, strong weight to keep it vertical
edge[ dir = none, weight = 10 ]
a0 -> ax;
b0 -> bx;
e0 -> ex;
f0 -> fx;
}
// connecting to the start and end node, normal edges again
edge[ weight = 1, dir = forw ];
start -> { a0 b0 c0 d0 e0 f0 }
{ ax bx c2 d2 ex fx } -> end;
}
这给了你
虽然在技术上是正确的,但对于一般用例来说,这种实现有点过头了。大多数StackOverflowers可能需要,而不是。tl;dr:只需在(可能的)不可见列中有问题的水平节点之间使用不可见的定向边来强制排序。你是对的@Cecil Curry-我回答了OP没有问的一个问题(如何在子图中保持边垂直)。你提到的解决方案是我答案的一部分,但被不必要的噪音弄糊涂了。