批量多边
一对多:
节点文字内容->{节点文字内容 ;节点文字内容 ;节点文字内容 }
如:a->{b;c;d}
多对一:
{节点文字内容 ; 节点文字内容 ; 节点文字内容}->节点文字内容
如:{b;c;d}->a
同样的也有多对多。
颜色名称列表

形状名称列表

设置两节点间边的方向
节点文字内容>节点文字内容[dir = 方向名称]
如:a->b[dir=both]、a–b[dir=back]。
4个系统方向名称:
- both(a<->b)->
- none(a–b)
- back(a<-b)
- forward(a->b)
有向图中默认为forward,无向图中默认为none。
复杂结构

上图的结构十分复杂, Graphviz 依然可以画出,源码如下:
1 2 3 4 5 6 7 8
| digraph structs { node [shape=record]; struct1 [shape=record,label="<f0> left|<f1> mid\ dle|<f2> right"]; struct2 [shape=record,label="<f0> one|<f1> two"]; struct3 [shape=record,label="hello\nworld |{ b |{c|<here> d|e}| f}| g | h"]; struct1 -> struct2; struct1 -> struct3; }
|

Graphviz 中的 label 标签还支持 Html 格式,上图可以用下面混入 html 的代码画出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| digraph html { abc [shape=none, margin=0, label=< <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4"> <TR><TD ROWSPAN="3"><FONT COLOR="red">hello</FONT><BR/>world</TD> <TD COLSPAN="3">b</TD> <TD ROWSPAN="3" BGCOLOR="lightgrey">g</TD> <TD ROWSPAN="3">h</TD> </TR> <TR><TD>c</TD> <TD PORT="here">d</TD> <TD>e</TD> </TR> <TR><TD COLSPAN="3">f</TD></TR> </TABLE>>]; }
|
子图
在dot中以cluster开头的子图会被当做是一个新的布局来处理,而不是在原图的基础上继续操作。

上图是个流程图,值得注意的是子图一定用 subgraph 声明,且图名称前面一定要带有 cluster 这个关键词。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| digraph G { subgraph cluster0 { node [style=filled,color=white]; style=filled; color=lightgrey; a0 -> a1 -> a2 -> a3; label = "process #1"; } subgraph cluster1 { node [style=filled]; b0 -> b1 -> b2 -> b3; label = "process #2"; color=blue } start -> a0; start -> b0; a1 -> b3; b2 -> a3; a3 -> a0; a3 -> end; b3 -> end; start [shape=Mdiamond]; end [shape=Msquare]; }
|
如果我们想直接指向子图怎么办,我们要设置 compound 为true,并配合 lhead 或 ltail 来实现。

1 2 3 4 5 6 7 8 9 10
| digraph G { compound=true; subgraph cluster0 { a; } subgraph cluster1 { b; } a -> b [lhead=cluster1]; }
|
以图片为节点
节点还可以使用图片,通过在节点中使用 image="image_path" 来设置图片。不过需要注意的是,在使用图片作为节点的时候,需要将本来的形状设置为 none,并且将 label 置为空字符串,避免出现文字对图片的干扰。
python 调用
除了可以使用 dot 文件编写图形外,也可以使用python编写相关的代码,生成图形文件。
安装 python 对应的 graphviz 相应的模块:
一个实例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import pygraphviz as pgv
G = pgv.AGraph()
G.add_node('a') G.add_edge('b','c')
G.graph_attr['label'] = 'test graphf' G.node_attr['shape'] = 'circle' G.edge_attr['color'] = 'red'
G.layout() G.layout(prog='dot')
G.draw('file.png') G.draw('file.ps',prog='circo')
|
引用
- https://blog.csdn.net/sd10086/article/details/52979462/
- https://www.cnblogs.com/taceywong/p/5439574.html
- https://www.jianshu.com/p/5b02445eca1d
- https://www.cnblogs.com/liang1101/p/7641984.html