复杂的graphviz树结构 [英] complicated graphviz tree structure
问题描述
我正在尝试使用graphviz创建树结构.我愿意手工编写graphviz代码或使用ruby-graphviz gem作为ruby.鉴于下面的图片,任何人都可以对必要的代码提供任何见解吗?忽略直线不是直线...应该在graphviz构建图形时出现.当线条相交时,我也很愿意拥有点/点.
I am trying to create a tree structure with graphviz. I am open to either writing the graphviz code by hand or using the ruby-graphviz gem for ruby. Given the below picture can anyone provide any insight on the necessary code? Ignore that the lines are not straight...they should be when graphviz builds the graph. I am open to having dots/points when lines intersect as well.
我玩过ruby-graphviz和家族树类...这使我成为其中的一部分,但我确实需要所有线条都笔直并以直角相交并且开箱即用代码似乎没有做到这一点.
I have played with ruby-graphviz and the family tree class...this is getting me part of the way there but I really need all lines to be straight and intersect at right angles and the out-of-the-box code doesn't seem to do that.
代码应该足够通用,以允许"C"框也有子级,并且在"A"下也可以有更多子级.
The code should be generic enough to allow for the "C" box to have children as well and for there also to be more children under "A".
颜色无关紧要...示例可以排除任何颜色.
The colors are irrelevant...the examples can exclude any coloring.
推荐答案
据我所知,这需要一些解决方法;我只会用Graphviz DOT语言来做.我先给您解决方案,然后提供一些扩展方法的说明.
As far as I know this requires a little work-around; I will only do it in Graphviz DOT language. I give you the solution first, then provide some explanations of how you can extend it.
这是结果图:
这是产生图形的Graphviz代码:
This is the Graphviz code producing the figure:
graph atree {
Item1 [shape=none,label="Item 1",pos="2.2,1.1!"];
Item2 [shape=none,label="Item 2",pos="2.2,0.1!"];
Item3 [shape=none,label="Item 3",pos="2.9,-0.3!"];
A [shape=box,color=lightblue,style=filled,pos="2,3!"];
B [shape=box,color=lightblue,style=filled,pos="1,2.1!"];
C [shape=box,color=lightblue,style=filled,pos="3,2.1!"];
D [shape=box,color=lightblue,style=filled,pos="1.5,1.5!"];
E [shape=box,color=lightblue,style=filled,pos="1.5,0.5!"];
D0 [style=invisible,fixedsize=true,width=0,height=0,pos="2,2.5!",label=""];
D1 [style=invisible,fixedsize=true,width=0,height=0,pos="1,2.5!",label=""];
D2 [style=invisible,fixedsize=true,width=0,height=0,pos="3,2.5!",label=""];
D3 [style=invisible,fixedsize=true,width=0,height=0,pos="1,1.5!",label=""];
D4 [style=invisible,fixedsize=true,width=0,height=0,pos="1,0.5!",label=""];
D5 [style=invisible,fixedsize=true,width=0,height=0,pos="1.5,1.1!",label=""];
D6 [style=invisible,fixedsize=true,width=0,height=0,pos="1.5,0.1!",label=""];
D7 [style=invisible,fixedsize=true,width=0,height=0,pos="2.2,-0.3!",label=""];
A -- D0 -- D1 -- B -- D3 -- D4 -- E [color=blue];
E -- D6 -- Item2 -- D7 -- Item3 [color=blue];
D0 -- D2 -- C [color=blue];
D3 -- D -- D5 -- Item1 [color=blue];
}
如果将其放置在名为inputfile.dot
的文件中,则可以使用命令neato -Tpng inputfile.dot > outfile.png
获取生成的图像文件.
If you put it in a file named inputfile.dot
you can get the resulting image file by using the command neato -Tpng inputfile.dot > outfile.png
.
现在对其工作方式进行一些评论:使用A, B, C, D, E, Item1, Item2, Item3
构建树的代码非常简单(属性仅设置颜色和框样式).使直线直线和正交的技巧包括1)在图形上添加零尺寸的不可见节点,以及2)在画布上以绝对坐标放置所有对象.步骤1)需要辅助节点D1, D2, D3, D4, D5, D6, D7
,步骤2)需要pos="x,y!"
选项.请注意,在pos
命令的末尾需要!
符号,因为否则这些位置将不会被视为最终位置,并且布局仍会更改.
Now a couple of comments on how it works: The code building the tree with A, B, C, D, E, Item1, Item2, Item3
is straightforward (the attributes merely set the colors and box styles). The trick to get the lines straight and orthogonal consists of 1) adding invisible nodes with zero size to the graph, and 2) positioning all objects in absolute coordinates on the canvas. The auxiliary nodes D1, D2, D3, D4, D5, D6, D7
are needed for step 1) and the pos="x,y!"
options are needed for step 2). Note, that you need the !
sign at the end of the pos
command since otherwise the positions would not be considered final and the layout would still get changed.
您可以通过以下方式添加其他节点:首先放置一个新节点(通过使用节点A ... Item3
的代码作为模板),添加一个不可见的辅助节点(使用pos
,以便与该节点之间的所有连接都是正交),然后通过<StartingNode> -- <AuxiliaryNode> -- <NewNode>
将连接添加到图形.
You can add additional nodes by first positioning a new node (by using the code for the nodes A ... Item3
as a template), adding an invisible, auxiliary node (with pos
such that all connections to and from it are orthogonal) and then adding the connection to the graph via <StartingNode> -- <AuxiliaryNode> -- <NewNode>
.
这篇关于复杂的graphviz树结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!