我有一个随时间变化的图表(通常会添加新节点)。我需要多次重新生成图表,并希望所有节点都保持在原来的位置。这在使用带有fdp
-algorithm 的 graphviz、设置pin
-flag 并使用 -argument 指定位置时部分pos
有效。
在大多数情况下,这工作得很好。但特别是当添加新节点时,布局有时会发生巨大变化——我需要避免这种情况。是否可以选择实际强制这些固定位置?这可能会导致一个丑陋的图表,这没关系 - 但他们只需要坚持。
Graphviz 并不容易将 noes 保持在同一位置:
如果在创建图形时所有节点都已知,则以下工作:
nodes[pin=true];
在图表的开头添加(以后不必添加)。布局图表:
fdp -Tdot input.gv -o input.pos.gv
您现在有一个包含所有节点的点文件。您可以将其用作要创建的所有图形的基本文件:
input.pos.gv
隐藏不需要的节点和边。style=invis
这样可以确保它们不显示,而且它们在布局中使用的位置不会被裁剪掉(例如,如果在图表的最顶部)。使用类似这样的东西布置它们(neato
并且选项-n2
是重要部分):
neato -n2 -Tpng input.pos.v1.gv -o output.v1.png
例子:
输入.gv:
digraph g{
node[pin=true];
a -> b;
a -> c;
b -> d;
b -> e;
b -> f;
c -> g;
}
输入.pos.modified.gv:
digraph g {
node [label="\N", pin=true];
graph [bb="0,0,187,207"];
a [pos="60.846,70.555", width="0.75", height="0.5", style=invis];
b [pos="94.351,128.04", width="0.75", height="0.5"];
c [pos="76.868,18.459", width="0.75", height="0.5"];
d [pos="119.08,188.8", width="0.75", height="0.5",style=invis];
e [pos="157.97,106.75", width="0.75", height="0.5"];
f [pos="27.319,158.05", width="0.75", height="0.5"];
g [pos="160.1,20.585", width="0.75", height="0.5"];
a -> b [pos="e,84.434,111.03 70.717,87.493 73.42,92.13 76.411,97.263 79.332,102.27", style=invis];
a -> c [pos="e,71.34,36.433 66.27,52.918 66.934,50.759 67.624,48.514 68.32,46.252", style=invis];
b -> d [pos="e,111.86,171.05 101.5,145.62 103.54,150.61 105.8,156.17 108.01,161.59", style=invis];
b -> e [pos="e,133.72,114.86 118.76,119.87 120.45,119.31 122.17,118.73 123.89,118.16"];
b -> f [pos="e,49.99,147.9 71.657,138.2 67.726,139.96 63.572,141.82 59.447,143.67"];
c -> g [pos="e,133.07,19.895 104.12,19.155 110.07,19.307 116.47,19.471 122.74,19.631"];
}
input.png 未经修改:
隐藏节点: