前言
在使用GraphViz之前,一直在使用Xmind,还有OmniGraffle,但是拖来拖去,完全不符合我们程序员的style啊,所以我也不喜欢用Xcode的SB,xib。
GraphViz,简单,而且方便,容易学习。
- GraphViz是什么?
GraphViz是一个开源的图像可视化的软件,是贝尔实验室开发的一个开源的工具包,它使用一个特定的DSL(领域特定语言): dot作为脚本语言,然后使用布局引擎来解析此脚本,并完成自动布局。graphviz提供丰富的导出格式,如常用的图片格式,SVG,PDF格式等。 - 怎么使用GraphViz?
GraphViz使用DOT语言,而DOT是纯文本图像描述语言,简单易学,还容易阅读。
1、安装
我是使用Sublime Text3配套使用的,只要在Sublime Text中安装plugin即可。首先使用shift+command+p
,输入搜索选中Package Control: Install Package
,然后输入GraphViz
,然后安装GraphVizPreview
即可,安装完成后,只要全选中代码,然后按shift+command+g
就可以预览了。
其实也可以直接用brew
安装GraphViz,但是每次要敲命令行,太麻烦了。
2、使用
GraphViz中包含多种布局:
- dot 默认布局,用于有向图
- neato 基于spring-model算法(force-based)
- twopo 径向布局
- circo 圆形布局
- fdp 用于无向图
2.1 简单使用
graph {
a -- b;
b -- c;
a -- c;
d -- c;
e -- c;
e -- a;
}
效果图:
是不是很简单呢,还有有向图如下:
digraph {
a -> b;
b -> c;
}
效果图:
2.2 简单的语法介绍
在这里就不啰嗦了,链接在此:语法介绍
2.3 稍微复杂点的例子
2.3.1 带标签
digraph {
player[label = "player"];
game[label = "game"];
player -> game[label = "play"]
}
2.3.2 不同颜色
digraph {
player[label = "player", color = Blue, fontcolor = Red, fontsize = 24, shape = box];
game[label = "game", color = Red, fontcolor = Blue, fontsize = 24, shape = ellipse];
player -> game[label = "play"]
}
更多shape,看这里
2.4 一些技巧
2.4.1 插入图片
digraph {
c[shape = none, image = "./pic.png"]
a -> b -> c;
c -> d;
}
注:需要用命令行
dot test.dot -T png -o test.png
生成,前提是用brew安装了GraphViz
2.4.2 统一节点和连线
digraph {
node[color = Red, fontsize = 24, shape = box]
edge[color = Blue, style = "dashed"]
c[shape = none, image = "./pic.png"]
a -> b -> c;
c -> d;
}
2.4.3 子视图
digraph {
label = visitNet
rankdir = LR
node[color = Red, fontsize = 24, shape = box]
edge[color = Blue, style = "dashed"]
user[style = "filled", color = "yellow", fillcolor = "chartreuse"]
subgraph cluster_cd{
label = "server and browser"
bgcolor = green;
browser -> server
}
user -> computer;
computer -> browser;
}
2.4.4 结构视图
digraph {
node[shape = record];
struct1[label = "<f0> left|<f1> mid\ dle|<f2> right"];
struct2[label = "<f0> one|<f1> two"];
struct3[label = "hello\nworld | {b|{c|<here> d|e}|f}|g|h"];
struct1:f1 -> struct2:f0;
struct1:f2 -> struct3:here;
}
这里引用了官方的一个例子,更多。
2.5.5 树形结构
digraph tree {
fontname = "PingFang-SC-Light"
fontsize = 24
node[shape = "plaintext"]
1 -> 2;
1 -> 3;
2 -> 4;
2 -> 5;
3 -> 6;
3 -> 7;
4 -> 8;
4 -> 9;
5 -> 10;
5 -> 11;
6 -> 12;
6 -> 13;
7 -> 14;
7 -> 15;
}
2.4.6 继承
digraph UML {
node[fontname = "Courier New", fontsize = 10, shape = record];
edge[fontname = "Courier New", fontsize = 10, arrowhead = "empty"];
Car[label = "{Car | v : float\nt : float | run() : float}"]
subgraph clusterSome{
bgcolor = "yellow";
Bus[label = "{Bus | | carryPeople() : void}"];
Bike[label = "{bike | | ride() : void}"];
}
Bus -> Car
Bike -> Car
}
2.4.7 时序图
digraph time {
rankdir = "LR";
node[shape = "point", width = 0, height = 0];
edge[arrowhead = "none", style = "dashed"];
{
rank = "same"
edge[style = "solided"];
APP[shape = "plaintext"];
APP -> step00 -> step01 -> step02 -> step03 -> step04 -> step05;
}
{
rank="same";
edge[style="solided"];
SDK[shape="plaintext"];
SDK -> step10 -> step11 -> step12 -> step13 -> step14 -> step15;
}
{
rank="same";
edge[style="solided"];
AliPay[shape="plaintext"];
AliPay -> step20 -> step21 -> step22 -> step23 -> step24 -> step25;
}
{
rank="same";
edge[style="solided"];
Server[shape="plaintext"];
Server -> step30 -> step31 -> step32 -> step33 -> step34 -> step35;
}
step00 -> step10 [label="sends order info", arrowhead="normal"];
step11 -> step21 [label="open AliPay", arrowhead="normal"];
step22 -> step12 [label="pay success", arrowhead="normal"];
step13 -> step03 [label="pay success", arrowhead="normal"];
step24 -> step34 [label="pay success", arrowhead="normal"];
}