本文介绍了强制graphviz保留节点位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个随时间变化的图(通常会添加新节点).我需要多次重新生成图,并希望所有节点都保持原样.当使用带有fdp-算法的graphviz,设置pin-标志并使用pos-参数指定位置时,这部分起作用.

I have a graph that changes over time (normally, new nodes are added). I need to regenerate the graph several times, and want all of the nodes to stick where they have been. This partially works when using graphviz with the fdp-algorithm, setting the pin-flag and specifying the positions with the pos-argument.

在大多数情况下,这很好用.但是尤其是在添加新节点时,布局有时会发生巨大变化-我需要避免这种情况.是否可以选择实际强制这些固定位置?这样做可能会产生难看的图形,没关系-但他们只需要坚持下去即可.

In most cases, this works pretty well. But especially when new nodes are added, the layout sometimes changes drastically - which I need to avoid. Is there an option to actually force these pinned positions? It could result in an ugly graph, that would be alright - but they just need to stick.

推荐答案

Graphviz很难将Noes保持在同一位置:

Graphviz doesn't make it easy to keep noes in the same position:

  • 添加/删除节点会导致布局完全不同.
  • 添加/删除节点可以更改边界框的尺寸以进行更改.

如果在创建图形时所有节点都已知,那么以下方法将起作用:

The following works if all nodes are known when creating the graphs:

  • 创建一个包含所有个可能节点的图形,然后让graphviz对其进行布局.您可以在图形的开头添加nodes[pin=true];(这样您以后就不必添加它了).
  • 布置图:

  • Create a graph containing all possible nodes, and let graphviz lay it out. You may add nodes[pin=true]; at the beginning of the graph (then you won't have to add it later).
  • Layout the graph:

fdp -Tdot input.gv -o input.pos.gv

您现在有了一个包含所有节点的点文件.您可以将其用作要创建的所有图形的基础文件:

You now have a dot file containing all the nodes. You can use this as the base file for all the graphs to be created:

使用类似这样的内容(neato和选项-n2是重要部分)对它们进行布局:

Lay them out using something like this (neato and option -n2 being the important parts):

neato -n2 -Tpng input.pos.v1.gv -o output.v1.png

示例:

input.gv:

digraph g{
    node[pin=true];
    a -> b;
    a -> c;
    b -> d;
    b -> e;
    b -> f;
    c -> g;
}

input.pos.modified.gv:

input.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未经修改:

input.png without modifications:

隐藏节点:

这篇关于强制graphviz保留节点位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 22:23