GraphViz dot

工具

在线编译工具:http://viz-js.com
VSCode安装插件:Graphviz (dot) language support for Visual Studio Code
命令行:dot,比如,dot -Tpng -ofile.png file.dot可以导出图片。在Ubuntu系统中,通过sudo apt install graphviz安装即可使用。

添加系统环境变量:

GRAPHVIZ_DOT = D:\Program Files\Graphviz\bin\dot.exe

学习资料

Node Shapes
Graphviz (dot) examples
Node, Edge and Graph Attributes

常用属性介绍

节点(node)属性

节点的形状主要有三种:Polygon-based Nodes, Record-based Nodes, User-defined Nodes。点击链接可看到详细介绍。

属性名称 介绍
shape 节点形状,box长方形、circle圆形等,参考Node Shapes
color 边框颜色
style 节点样式,filled, invisible, diagonals等,参考Styles for Nodes
fillcolor 填充颜色, 只有在style=filled时才生效
fontcolor 文字颜色

边(edge)属性

边也可以认为就是节点之间的连接线

属性名称 介绍
label 文字描述
headlabel 连接线头部文字描述,比如A -> B, A端是尾部,B端是头部
taillabel 连接线尾部文字描述
headport 连接线头部指向节点的位置,n(north), s(south), w(west), e(east)
tailport 连接线尾部指向节点的位置
color 连接线颜色
labelfontcolor 文字颜色,还有fontname和
arrowhead 箭头样式
splines 连接线样式,直线、曲线、折线等,splines=false设置为直线。
dir 连接线方向
headclip 连接线头部指向节点边缘或节点中心,默认是边缘
tailclip 连接线尾部指向节点边缘或节点中心,默认是边缘
weight 连接线布局系数,值越大则线越短、越直、越垂直
constraint 为false时,该连接线不用于节点布局的计算
decorate 为true时,连接线标签通过下划线附着到连接线上

图(graph)属性

属性名称 介绍
rankdir 图形布局方向,默认TB,表示top -> bottom。值为LR表示left -> right。
ranksep 最小间隔
group 对节点有效,同组节点的连接线将保持在一个直线方向。示例
compound 如果为ture,则允许clusters集群之间留出边。默认不允许。

设置node和edge的默认属性

node和edge是两个特殊关键字,用于设置默认属性

1
2
3
4
5
digraph {
node [attr=value, ...]
edge [attr=value, ...]
......
}

多点连线

1
2
3
4
5
6
digraph {
node [shape=box]
A -> {B, C, D} -> E
node [shape=""]
{P, Q} -> {M, N}
}
%0 A A B B A->B C C A->C D D A->D E E B->E C->E D->E P P M M P->M N N P->N Q Q Q->M Q->N

垂直方向布局

g B B C C D D A A A->B A->C A->D
1
2
3
4
5
6
7
digraph g{
{node[group=a]; B;C;D;}
A -> B;
A -> C;
A -> D;
B ->C ->D [style=invis];
}
g l1->l2 B2 B2 l1->B2 l2->l3 C2 C2 l2->C2 D2 D2 l3->D2 B1 B1 B1->l1 C1 C1 C1->l2 D1 D1 D1->l3 A A A->l1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
digraph g{
{node[style=invis, shape=point, width=0, height=0]; l1; l2; l3}
{rank=same; l1; B1; B2}
{rank=same; l2; C1; C2}
{rank=same; l3; D1; D2}

A -> l1 -> l2 -> l3 [dir=none]
B1 -> l1 [dir=back]
l1 -> B2
C1 -> l2 [dir=back]
l2 -> C2
D1 -> l3 [dir=back]
l3 -> D2
}

参考:

横向布局示例

G clusterCG C 1 1 2 2 1->2 3 3 2->3 4 4 3->4 A A A1 A1 A->A1 A2 A2 A1->A2 A3 A3 A2->A3 B B C C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 digraph G {
newrank=true
rankdir = LR;
node [shape = none]

1->2->3->4[arrowhead=none]

node [shape = ellipse]

A->A1->A2->A3;

subgraph clusterCG{
shape = rect;
rank=same;

A2;
B;
C;
color=blue;
label="C";
}

{ rank=same; 1; A;}
{ rank=same; 2; A1}
{ rank=same; 3; A2}
{ rank=same; 4; A3;}
}

参考:How to change Graphviz subgraph rank?