英文:
dot graphviz Different ranking of nodes
问题
我需要绘制一个复杂的控制流程图,其中水平和垂直轴的划分很重要。Dot 只允许设置 rankdir(LR,TB)和一个垂直轴(使用 rank = same ),如何实现更复杂的分支?
添加的当前代码:
digraph G {
rankdir = "LR";
A1 -> A2 -> A3 -> A4 -> A5;
{rank=same; A2 B2 C2}
A2 -> B2 -> C2;
C2 -> C3 -> C4;
{rank=same; C3 D3}
C3 -> D3;
{rank=same; A3 E3}
A3 -> E3;
E3 -> E4 -> E5 -> E6;
{rank=same; E4 F4}
E4 -> F4;
{rank=same; E5 F5}
E5 -> F5;
{rank=same; A4 B4}
A4 -> B4;
}
英文:
I need to draw a complex control flow where the divisions into horizontal and vertical axies are important. Dot only allows you to set rankdir (LR, TB) and one perpendicular axis (using rank = same ) how do I achieve more complex branching?
Truncated version of the target graph:
Added current code:
digraph G {
rankdir = "LR";
A1 -> A2 -> A3 -> A4 -> A5;
{rank=same; A2 B2 C2}
A2 -> B2 -> C2;
C2 -> C3 -> C4;
{rank=same; C3 D3}
C3 -> D3;
{rank=same; A3 E3}
A3 -> E3;
E3 -> E4 -> E5 -> E6;
{rank=same; E4 F4}
E4 -> F4;
{rank=same; E5 F5}
E5 -> F5;
{rank=same; A4 B4}
A4 -> B4;
}
答案1
得分: 1
要产生一个“向上”箭头,交换头部和尾部,然后设置dir=back(见下文)。不幸的是,向上的箭头似乎会导致行的不雅观重新排列,以及滑稽的边缘。解决这个问题的方法似乎是按照所需的行(非等级)顺序声明节点(再次见下文)。
digraph G {
rankdir = "LR";
nodesep=.5 // 将事物扩展开一点
// 按照从上到下(行)的顺序定义节点
// 定义中的顺序并不重要,rank=same会将其整理好
// 这些都可以放在一行上,只要顺序正确即可
D3
C3 C2 C4
B4 B2
A4 A3 A1 A2
E5 E6 E3 E4
F5 F4
{rank=same; A2 [group=g1] B2[group=g2] C2[group=g3]}
{rank=same; A4 [group=g1] B4[group=g2]}
{rank=same; C3 [group=g3] D3}
{rank=same; A3 [group=g1] E3[group=g5]}
{rank=same; E4 [group=g5] F4[group=g6]}
{rank=same; E5[ group=g5] F5[group=g6]}
// 边缘
A1 -> A2 -> A3 -> A4 -> A5;
C2 -> C3 -> C4;
E3 -> E4 -> E5 -> E6;
// 向上箭头边缘
{ edge[dir=back]
C2 -> B2 -> A2;
}
{edge[dir=back]
D3:s -> C3:n;
}
{edge[dir=back]
B4:s -> A4:n;
}
A3:s -> E3:n
E4 -> F4;
E5 -> F5;
}
英文:
To produce an "up" arrow, swap the head & tail, then set dir=back (see below).
Unfortunately, a side-effect of the up arrows seems to be an ugly rearrangement of the rows, along with goofy edges.
The fix for this seems to be declaring the nodes in the desired row (non rank) order (again see below)
digraph G {
rankdir = "LR";
nodesep=.5 // spread things out a bit
// define the nodes in top-to-bottom (row) order
// the sequence within the definition is not important,
// rank=same will straighten that out
// these could all be on one line, as long as the sequence is correct
D3
C3 C2 C4
B4 B2
A4 A3 A1 A2
E5 E6 E3 E4
F5 F4
{rank=same; A2 [group=g1] B2[group=g2] C2[group=g3]}
{rank=same; A4 [group=g1] B4[group=g2]}
{rank=same; C3 [group=g3] D3}
{rank=same; A3 [group=g1] E3[group=g5]}
{rank=same; E4 [group=g5] F4[group=g6]}
{rank=same; E5[ group=g5] F5[group=g6]}
// edges
A1 -> A2 -> A3 -> A4 -> A5;
C2 -> C3 -> C4;
E3 -> E4 -> E5 -> E6;
// up arrow edge
{ edge[dir=back]
C2 -> B2 -> A2;
}
{edge[dir=back]
D3:s -> C3:n;
}
{edge[dir=back]
B4:s -> A4:n;
}
A3:s -> E3:n
E4 -> F4;
E5 -> F5;
}
答案2
得分: 0
我设法使用分组来整理树枝。但如果您建议如何将 A3 -> E3 移动到顶部,我将不胜感激。
digraph G {
rankdir = "LR";
A1 -> A2 -> A3 -> A4 -> A5;
{rank=same; A2 [group=g1] B2[group=g2] C2[group=g3]}
A2 -> B2 -> C2;
C2 -> C3 -> C4 ;
C4 [group=g3];
{rank=same; C3 [group=g3] D3[group=g4]}
C3 -> D3;
{rank=same; A3 [group=g1] E3[group=g5]}
A3 -> E3;
E3 -> E4 -> E5 -> E6;
{rank=same; E4[group=g5] F4[group=g6]}
E4 -> F4;
{rank=same; E5[group=g5] F5[group=g6]}
E5 -> F5;
{rank=same; A4[group=g1] B4[group=g2]}
A4 -> B4;
}
英文:
I managed to straighten the branches using groups. But I will be glad if you suggest how to move A3 -> E3 to the top.
digraph G {
rankdir = "LR";
A1 -> A2 -> A3 -> A4 -> A5;
{rank=same; A2 [group=g1] B2[group=g2] C2[group=g3]}
A2 -> B2 -> C2;
C2 -> C3 -> C4 ;
C4 [group=g3];
{rank=same; C3 [group=g3] D3[group=g4]}
C3 -> D3;
{rank=same; A3 [group=g1] E3[group=g5]}
A3 -> E3;
E3 -> E4 -> E5 -> E6;
{rank=same; E4[group=g5] F4[group=g6]}
E4 -> F4;
{rank=same; E5[group=g5] F5[group=g6]}
E5 -> F5;
{rank=same; A4[group=g1] B4[group=g2]}
A4 -> B4;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论