我可以使Graphviz根据颜色集中边缘吗?

huangapple go评论57阅读模式
英文:

Can I make graphviz concentrate edges based on color?

问题

我有一个有向图,边的颜色很重要:

当前图示

digraph {
    splines=ortho
    node [shape=box];
    1 -> 4 [color="#51dbf4"];
    4 -> 7 [color="#51dbf4"];
    7 -> 1 [color="#ac043e"];
    7 -> 1 [color="#51dbf4"];
    1 -> 7 [color="#ac043e"];
}

我可以使Graphviz根据颜色集中边缘吗?

我想要将栗色箭头合并,以使图更清晰。

期望图示

digraph {
    splines=ortho
    node [shape=box];
    1 -> 4 [color="#51dbf4"];
    4 -> 7 [color="#51dbf4"];
    7 -> 1 [color="#ac043e" dir=both];
    7 -> 1 [color="#51dbf4"];
}

我可以使Graphviz根据颜色集中边缘吗?

但是,这需要我手动更改需要合并为双头箭头的每个箭头。我希望自动合并它们。我尝试使用 concentrate=true 来使箭头合并,但这有点破坏了图的含义:

不正确的图示

digraph {
    splines=ortho
    concentrate=true
    node [shape=box];
    1 -> 4 [color="#51dbf4"];
    4 -> 7 [color="#51dbf4"];
    7 -> 1 [color="#ac043e"];
    7 -> 1 [color="#51dbf4"];
    1 -> 7 [color="#ac043e"];
}

我可以使Graphviz根据颜色集中边缘吗?

Graphviz 坚持要合并所有箭头,而不考虑它们的颜色,这在这种情况下是不正确的,因为它在很大程度上破坏了大部分边的含义。

问题

对于我的用例,我有一个由代码生成的大型图,有大量的边,我不想手动更改那么多边成为双头箭头。有没有一种方法可以告诉 Graphviz 我只想合并相同颜色的边?也许我可以为每个边附上一个组ID,然后它只会合并具有相同ID的边?欢迎任何想法。

英文:

I have a directed graph where the color of the edges matters:

Current Diagram

digraph {
    splines=ortho
    node [shape=box];
    1 -> 4 [color="#51dbf4"];
    4 -> 7 [color="#51dbf4"];
    7 -> 1 [color="#ac043e"];
    7 -> 1 [color="#51dbf4"];
    1 -> 7 [color="#ac043e"];
}

我可以使Graphviz根据颜色集中边缘吗?

I want to combine the maroon colored arrows to make the graph more clear.

Desired Diagram

digraph {
    splines=ortho
    node [shape=box];
    1 -> 4 [color="#51dbf4"];
    4 -> 7 [color="#51dbf4"];
    7 -> 1 [color="#ac043e" dir=both];
    7 -> 1 [color="#51dbf4"];
}

我可以使Graphviz根据颜色集中边缘吗?

However, this requires me to manually change each arrow that needs to be combined into a double-headed arrow. I wanted to merge these automatically. I tried using concentrate=true to make the arrows merge, but that kind of destroys the meaning of the graph:

Incorrect Diagram

digraph {
    splines=ortho
    concentrate=true
    node [shape=box];
    1 -> 4 [color="#51dbf4"];
    4 -> 7 [color="#51dbf4"];
    7 -> 1 [color="#ac043e"];
    7 -> 1 [color="#51dbf4"];
    1 -> 7 [color="#ac043e"];
}

我可以使Graphviz根据颜色集中边缘吗?

Graphviz insists on merging the arrows regardless of their color, which is not correct in this case because it kind of just obliterates most of the edges.

Question

For my use case, I have a large, code-generated graph with tons of edges and I don't want to manually change so many edges to be double-ended. Is there a way to specify to graphviz that I only want to merge edges of the same color? Like maybe I could tag each edge with a group ID and then it would only merge edges that have the same ID? Any ideas are appreciated.

答案1

得分: 1

这里有一个更简单的方法,但这个 gvpr (http://www.graphviz.org/pdf/gvpr.1.pdf) 程序可以实现你想要的功能。对于每条边,它会检查是否存在一个"反向"边,如果是的话,就会检查它们是否具有相同的颜色。如果是的话,就会设置 dir=both 并删除"反向"边。

E{
  edge_t N2;
  N2=isEdge($.head, $.tail,"");
  if (N2!=NULL && N2.color==$.color){
    print("// bingo : ", $.name, " <--> ", N2.name);
    $.dir="both";
    delete($G, N2);
  }
}

以及一个(Linux)命令行:

gvpr -c -f single2double.gvpr myfile.gv | dot -Tpng >o.png

生成的结果如下:

我可以使Graphviz根据颜色集中边缘吗?

英文:

There may be an easier way, but this gvpr (http://www.graphviz.org/pdf/gvpr.1.pdf) program does what you want.
For each edge, it checks if a "reverse" edge exists, and if so checks if both have the same color.
If so, set dir=both & delete the "reverse" edge.

E{
  edge_t N2;
  N2=isEdge($.head, $.tail,"");
  if (N2!=NULL && N2.color==$.color){
    print("// bingo : ", $.name, " <--> ", N2.name);
    $.dir="both";
    delete($G, N2);
  }
}

And a (Linux) command line of
gvpr -c -f single2double.gvpr myfile.gv | dot -Tpng >o.png
Giving:
我可以使Graphviz根据颜色集中边缘吗?

huangapple
  • 本文由 发表于 2023年2月16日 08:57:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466836.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定