我正在尝试使用 DOT 语言绘制家谱。我使用辅助节点来使树看起来整齐,但在某些情况下,由于未知的原因,其中一个节点被放置在根本不合逻辑的地方。在下面的示例中,节点 J 的位置应该在 I 旁边,但正如您所看到的,它位于图的最右侧。应该做什么来解决这个问题
这是我的代码:
digraph FamilyTree {
edge [dir=none, penwidth=2];
rankdir=TB;
"A" [shape=BOX, ];
"B" [shape=BOX,];
"C" [shape=BOX, ];
"D" [shape=BOX,];
"E" [shape=BOX, ];
"F" [shape=BOX, ];
"c1" [shape=point, label="",width=0.01, height=0.01];
"c1" -> "F" [arrowhead=none];
"G" [shape=BOX, ];
"c2" [shape=point, label="",width=0.01, height=0.01];
"c2" -> "G" ;
"H" [shape=BOX, ];
"I" [shape=BOX, ];
"c3" [shape=point, label="",width=0.01, height=0.01];
"c3" -> "I" ;
"J" [shape=BOX, ];
"c4" [shape=point, label="",width=0.01, height=0.01];
"c4" -> "J" ;
"K" [shape=BOX,];
"c5" [shape=point, label="",width=0.01, height=0.01];
"c5" -> "K" ;
"L" [shape=BOX, ];
"c6" [shape=point, label="",width=0.01, height=0.01];
"c6" -> "L" ;
"M" [shape=BOX, ];
"c7" [shape=point, label="",width=0.01, height=0.01];
"c7" -> "M" ;
"Couple_5780_1" [shape=point, color=gray, label=""];
"Couple_5355_1" [shape=point, color=gray, label=""];
"Couple_8222_1" [shape=point, color=gray, label=""];
"Couple_8222_2" [shape=point, color=gray, label=""];
"Couple_8222_3" [shape=point, color=gray, label=""];
{ rank=same; "A" -> "Couple_5780_1" -> "B" }
{ rank=same; "C" -> "Couple_5355_1" -> "D" }
{ rank=same;"E"->"Couple_8222_1"->"F"->"Couple_8222_2"->"G"->"Couple_8222_3"->"H"}
"c3" -> "c4" [arrowhead=none,constraint=false];
{ rank=same; "c3"; "c4"; }
"c6" -> "c7" [arrowhead=none,constraint=false];
{ rank=same; "c6"; "c7"; }
"Couple_5780_1" -> "c1" ;
"Couple_5355_1" -> "c2";
"Couple_8222_1" -> "c3" ;
"Couple_8222_2" -> "c5";
"Couple_8222_3" -> "c6" ;
}
Graphviz 算法很复杂,有时甚至很古怪。
无法获得 dot 来保持所需的节点序列是一个已知问题 (https://gitlab.com/graphviz/graphviz/-/issues/1569),没有保证的解决方案,但以下是您想要的结果。
它添加组(https://www.graphviz.org/docs/attrs/group/)来(尝试)强制垂直对齐,并将“J”和“c4”添加到其中一个组以强制所需的位置.
digraph FamilyTree {
edge [dir=none, penwidth=2];
rankdir=TB;
"A" [shape=BOX, ];
"B" [shape=BOX,];
"C" [shape=BOX, ];
"D" [shape=BOX,];
"E" [shape=BOX, ];
"F" [shape=BOX, group=F1];
"c1" [shape=point, label="",width=0.01, height=0.01 group=F1 ];
"c1" -> "F" [arrowhead=none];
"G" [shape=BOX, group=G1];
"c2" [shape=point, label="",width=0.01, height=0.01 group=G1];
"c2" -> "G" ;
"H" [shape=BOX, ];
"I" [shape=BOX, group=I1];
"c3" [shape=point, label="",width=0.01, height=0.01 group=I1];
"c3" -> "I" ;
"J" [shape=BOX, group=F1];
"c4" [shape=point, label="",width=0.01, height=0.01 group=F1];
"c4" -> "J" ;
"K" [shape=BOX,];
"c5" [shape=point, label="",width=0.01, height=0.01];
"c5" -> "K" ;
"L" [shape=BOX, ];
"c6" [shape=point, label="",width=0.01, height=0.01];
"c6" -> "L" ;
"M" [shape=BOX, ];
"c7" [shape=point, label="",width=0.01, height=0.01];
"c7" -> "M" ;
"Couple_5780_1" [shape=point, color=gray, label="" group=F1];
"Couple_5355_1" [shape=point, color=gray, label="" group=G1];
"Couple_8222_1" [shape=point, color=gray, label="" group=I1];
"Couple_8222_2" [shape=point, color=gray, label=""];
"Couple_8222_3" [shape=point, color=gray, label=""];
{ rank=same; "A" -> "Couple_5780_1" -> "B" }
{ rank=same; "C" -> "Couple_5355_1" -> "D" }
{ rank=same;"E"->"Couple_8222_1"->"F"->"Couple_8222_2"->"G"->"Couple_8222_3"->"H"}
"c3" -> "c4" [arrowhead=none, constraint=false]
{ rank=same; "c3"; "c4"; }
"c6" -> "c7" [arrowhead=none,constraint=false];
{ rank=same; "c6"; "c7"; }
"Couple_5780_1" -> "c1" ;
"Couple_5355_1" -> "c2";
"Couple_8222_1" -> "c3" ;
"Couple_8222_2" -> "c5";
"Couple_8222_3" -> "c6" ;
F->c4 [style=invis] // for F1 group
}