在graphviz中避免边缘交集

时间:2013-12-08 18:25:44

标签: graphviz

假设我们有以下简单的图表:

digraph Test {

    Start[shape = doublecircle];
    Foo[shape = square];
    Bar[shape = diamond];
    Baz[shape = square];
    Xyz[shape = square];

    Start -> Foo;
    Foo:s -> Bar:n;
    Bar:w -> Baz:n;
    Bar:e -> Xyz:n;
    Baz:s -> Foo:n;

}

呈现方式如下:

Rendered graph

有没有办法告诉graphviz绘制边Baz -> Foo而不与Foo -> BarBar -> Baz交叉?

3 个答案:

答案 0 :(得分:4)

当涉及到graphviz布局时,你能做的最好的事情就是尽量不要干扰:)

从你的档案中取出compass_pt:

digraph Test {

    Start[shape = doublecircle];
    Foo[shape = square];
    Bar[shape = diamond];
    Baz[shape = square];
    Xyz[shape = square];

    Start -> Foo;
    Foo -> Bar;
    Bar -> Baz;
    Bar -> Xyz;
    Baz -> Foo;

}

你得到:

enter image description here

答案 1 :(得分:1)

添加一个不可见的节点应该可以解决这个问题:

enter image description here

digraph Test {

    Start[shape = doublecircle];
    Foo[shape = square];
    Bar[shape = diamond];
    Baz[shape = square];
    Xyz[shape = square];

    // "invisible" node to connect baz to foo
    BazToFoo [shape=none, label="", height=0, width=0]

    Start -> Foo;
    Foo:s -> Bar:n;
    Bar:w -> Baz:n;
    Bar:e -> Xyz:n;
    Baz:s -> BazToFoo [dir=none] // remove the arrowhead
    BazToFoo -> Foo:n;

}

答案 2 :(得分:1)

虽然我同意Guy's answer保持简单,但这是一种替代方法,可以通过一些更改来消除边缘交叉。

digraph Test {
    Start[shape = doublecircle, group=a];
    Foo[shape = square, group=a];
    Bar[shape = diamond, group=a];
    Xyz[shape = square];
    Baz[shape = square];

    Start -> Foo;
    Foo:s -> Bar:n;
    Bar:e -> Baz:n;
    Bar:w -> Xyz:n;
    Baz:s -> Foo:n[constraint=false];
}

modified graphviz graph with no edge crossings

我发现graphviz的布局不容易沿着图的左外侧向后路径边缘。可能是布局算法的工作方式,允许图形仅扩展到右侧和底部。

这就是为什么我切换底部节点的位置,让节点的返回边缘在右侧。