基本介绍

graphviz是一个图形可视化软件,这是贝尔实验室开发的一个开源的工具包,软件地址在这里,它使用dot作为脚本语言,然后使用布局引擎来解析此脚本,并完成自动布局。graphviz提供丰富的导出格式,如常用的图片格式,SVG,PDF格式等。

graphviz的布局器包含:

- dot布局,主要用于有向图,是默认的布局。
- 基于spring-model(又称force-based)算法的neato。
- 径向布局twopi。
- 圆环布局circo。
- 无向图fdp。

graphviz包含3个元素,图(graphs),节点(nodes)和边(edges)。每个元素都可以具有各自的属性,用来定义字体,样式,颜色,形状等。graphviz画图步骤如下:

1. 定义图的名称。
2. 添加节点和边,并指定样式。
3. 使用布局器进行绘制。

基本图形绘制

graphviz使用graph定义无向图,使用digraph定义有向图,使用subgraph定义子图。

dot命令使用

1
$ dot -Tpng graph1.dot -o graph1.png

简单无向图

1
2
3
4
5
6
7
8
graph graphDemo {
//无向图使用 --
a -- b;
b -- c;
b -- d;
b -- e;
d -- a;
}

img

简单有向图

1
2
3
4
5
6
7
8
9
10
11
digraph smallGraph {
// 有向图使用 ->
main -> parse -> execute;
main -> init;
main -> cleanup;
execute -> make_string;
execute -> printf
init -> make_string;
main -> printf;
execute -> compare;
}

img

复杂的有向图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
digraph fancyGraph{
size ="4,4";
main [shape=box]; /* this is a comment */
main -> parse [weight=8];
parse -> execute;
main -> init [style=dotted];
main -> cleanup;
execute -> { make_string; printf}
init -> make_string;
edge [color=red]; // so is this
main -> printf [style=bold,label="100 times"];
make_string [label="make a\nstring"];
node [shape=box,style=filled,color=".7 .3 1.0"];
execute -> compare;
}

img