使用graphviz如何绘制带有内部节点的圆形图?

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

How can I draw a circular graph with a node inside, using graphviz?

问题

我想在圆形图中生成一个圆心节点(可能还有其他几个...)。

例如,考虑这个简单的7个节点无向图:

graph g1 {

0--1;
0--2;
0--3;
0--4;
0--5;
0--6;


1--2--3--4--5--6--1;
}

我希望得到类似这样的效果(在csacademy.com/app/graph_editor/上在线制作的):
使用graphviz如何绘制带有内部节点的圆形图?

Graphviz circo引擎在将所有节点放在圆形布局中方面做得很好,但我想要节点0位于中间。

我在其页面上阅读到,root属性可以用于指定中间的节点:

>布局的中心将是生成的生成树的根节点。
> 作为图属性,这给出了节点的名称。
> 作为节点属性,它指定该节点应该用作中心节点。

据我理解,这个属性可以用作全局图属性,也可以用作“节点”属性。因此,我尝试添加以下内容:

0[root=true];

在开始时告诉它节点0是根节点。但没有变化。

我还尝试了graph [root=0];,没有成功(参见此处的演示)。我该如何实现这一点?

其他答案建议添加一个“隐藏”的节点,但我无法这样做,因为dot文件既用于生成渲染,又用于其他用途的数据输入。

第二个问题:是否可能在圆形图中有一个包含多个节点的“集群”,使用这种技术?或者“root”属性需要分配给单个节点?

编辑 例如,类似于以下示例,其中圆形布局中有3个节点:
使用graphviz如何绘制带有内部节点的圆形图?

英文:

I want to produce a circular graph with one (and possibly several others...) in the middle of the circle.
As an example, consider this simple 7-nodes undirected graph:

graph g1 {

0--1;
0--2;
0--3;
0--4;
0--5;
0--6;


1--2--3--4--5--6--1;
}

I would like something like this (did online with csacademy.com/app/graph_editor/:
使用graphviz如何绘制带有内部节点的圆形图?

The Graphviz circo engine does a good job putting all the nodes in a circular layout, but I want the node 0 to be in the middle.

I read on its page that the attribute root can be used to specify which one is in the middle:

>The center of the layout will be the root of the generated spanning tree.
> As a graph attribute, this gives the name of the node.
> As a node attribute, it specifies that the node should be used as a central node.

As I understand it, this attribute can be used either as a global graph attribute, either as a "node" attribute. So I tried adding:

0[root=true];

at the beginning, to tell him that the node 0 is the root one. No change.

I also tried graph [root=0];, no success (see live demo here). How can I achieve this?

Other answers suggest adding a "hidden" node, but I can't do that because the dot file is used both to produce a rendering and as data input for something else.

Second question: would it be possible to have a "cluster" of several nodes in the middle of the circle, using that technique? Or does the "root" attribute need to be assigned to a single node?

Edit For example, something like this, with 3 nodes inside the circular layout.
使用graphviz如何绘制带有内部节点的圆形图?

答案1

得分: 2

使用 twopi(https://graphviz.org/docs/layouts/twopi/)而不是 circo。请注意,节点的放置(起始点、方向和顺序)似乎不受我们控制(尽管在我看来还好)。

请更详细地说明或展示 问题#2 的示例(草图也可以):

graph g1 {
root="0"

0--1;
0--2;
0--3;
0--4;
0--5;
0--6;
1--2--3--4--5--6--1;
}

结果:
使用graphviz如何绘制带有内部节点的圆形图?

英文:

Use twopi (https://graphviz.org/docs/layouts/twopi/) instead of circo.
Note that the node placement (starting point, direction, and sequence) seems to be out of our control (though OK to my eye).

Please say more or show an example of Question #2 (sketch would be fine)

graph g1 {
root="0"

0--1;
0--2;
0--3;
0--4;
0--5;
0--6;
1--2--3--4--5--6--1;
}

Gives:
使用graphviz如何绘制带有内部节点的圆形图?

答案2

得分: 1

"twopi"布局基本上是"采纳或放弃"。以下内容非常接近,但开始使用与其他引擎不同的"weight"属性而变得复杂。如果你想要更多控制,这里有一个链接到用于Graphviz的径向布局预处理器:https://gist.github.com/steveroush/60d9a850a545ec4e02b0482c7bac3ad5

graph g1 {
  root="X"  // 添加 - 隐藏根节点
  X [shape=point style=invis]
  ranksep=".5:2"  // 英寸
  { edge [style=invis]
    X -- {0 6 7}
  }
  0 -- {6 7}
  6 -- 7

  0 -- {2 3}
  6 -- {4 5}
  7 -- {1 }
  7 -- {2 } [style=invis]

  edge[weight=0]  // 类似于dot的constraint=false
  0 -- 4
  6 -- 1

  edge[weight=1]  
  1 -- 2 -- 3 -- 4 -- 5 -- 1
}

生成:
使用graphviz如何绘制带有内部节点的圆形图?

英文:

twopi layout is pretty much "take it or leave it".
The following gets pretty close, but starts getting fiddley with weight attributes (different than weight for the other engines).
If you want more control, here is a link to a radial layout preprocessor for Graphviz https://gist.github.com/steveroush/60d9a850a545ec4e02b0482c7bac3ad5

graph g1 {
  root="X"  // added - invisible root
  X [shape=point style=invis]
  ranksep=".5:2"  // inches
  { edge [style=invis]
    X -- {0 6 7}
  }
  0 -- {6 7}
  6 -- 7

  0 -- {2 3}
  6 -- {4 5}
  7 -- {1 }
  7 -- {2 } [style=invis]

  edge[weight=0]  // similar to constraint=false for dot
  0 -- 4
  6 -- 1

  edge[weight=1]  
  1 -- 2 -- 3 -- 4 -- 5 -- 1
}

Giving:
使用graphviz如何绘制带有内部节点的圆形图?

huangapple
  • 本文由 发表于 2023年7月17日 22:03:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705238.html
匿名

发表评论

匿名网友

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

确定