用代码画流程图和时序图快餐教程(1) - graphviz的dot图

用代码画流程图和时序图快餐教程(1)

版本历史:

2016.06.02,v1:初稿
2016.07.31,v2:更新中文字体的设置。更新颜色的作用。
2017.08.02,v3:更新命令行方式

graphviz的dot图

工具下载网址:http://www.graphviz.org/
可以使用sublime text来编辑。
用代码画流程图和时序图快餐教程(1) - graphviz的dot图

Windows下的GVEdit

在Windows下,Graphviz提供一个GVedit工具来编辑。
用代码画流程图和时序图快餐教程(1) - graphviz的dot图

命令行方式

在mac和Linux下,我们可以采用命令行的方式来实现。
对于层次图,我们主要使用dot工具,常用格式如下例:

dot -Tpng -O model2.gv

-T是输出的格式类型,如png,jpg,pdf等。

图的结构

首先写个有向图的结构:

digraph G{
}

节点和边

然后就用“节点1 -> 节点2”的方式画图就好了。

例:

Context_sendbroadcast -> ContextWrapper_sendBroadcast;
ContextWrapper_sendBroadcast -> ContextImpl_sendBroadcast;
ContextImpl_sendBroadcast -> ActivityManagerService_broadcastIntent;

节点的文字

如果节点的文字太长,可以通过label属性来保存完整的文本。
例:

Context_sendbroadcast[label="Context.sendBroadcast(Intent)"];
Context_sendbroadcast_asUser[label="Context.sendBroadcastAsUser(Intent,UserHandle)"];
Context_sendOrderedBroadcast[label="Context.sendOrderedBroadcast(Intent,String,BroadcastReceiver,Handler,int,String,Bundle)"];
Context_sendOrderedBroadcast2[label="Context.sendOrderedBroadcast(Intent,String"];
Context_sendStickyBroadcast[label="Context.sendStickyBroadcast(Intent)"];
Context_sendStickyBroadcast2[label="Context.sendStickyOrderedBroadcast(Intent,BroadcastReceiver,Handler,int,String,Bundle)"];

边和节点的属性

可以针对每个节点和边设置属性,也可以为所有的节点和边统一设。
例:针对所有的节点和边都设属性

node [shape = "Mrecord", color="skyblue", style="filled"];
edge [color="darkgreen"];

注释

dot语言支持C++格式的注释,也就是说/**/和//的注释都是支持的。

完整例子

digraph G{
node [shape = "Mrecord", color="skyblue", style="filled"];
edge [color="darkgreen"];

Context_sendbroadcast[label="Context.sendBroadcast(Intent)"];
Context_sendbroadcast_asUser[label="Context.sendBroadcastAsUser(Intent,UserHandle)"];
Context_sendOrderedBroadcast[label="Context.sendOrderedBroadcast(Intent,String,BroadcastReceiver,Handler,int,String,Bundle)"];
Context_sendOrderedBroadcast2[label="Context.sendOrderedBroadcast(Intent,String"];
Context_sendStickyBroadcast[label="Context.sendStickyBroadcast(Intent)"];
Context_sendStickyBroadcast2[label="Context.sendStickyOrderedBroadcast(Intent,BroadcastReceiver,Handler,int,String,Bundle)"];

ContextWrapper_sendBroadcast[label="ContextWrapper.sendBroadcast(Intent)"];
ContextWrapper_sendOrderedBroadcast2[label="ContextWrapper.sendOrderedBroadcast(Intent,String)"];
ContextWrapper_sendOrderedBroadcast[label="ContextWrapper.sendOrderedBroadcast(Intent,String,BroadcastReceiver,Handler,int,String,Bundle)"];
ContextWrapper_sendStickyBroadcast[label="ContextWrapper.sendStickyBroadcast(Intent)"];
ContextWrapper_sendStickyBroadcast2[label="ContextWrapper.sendStickyOrderedBroadcast(Intent,BroadcastReceiver,Handler,int,String,Bundle)"];
ContextWrapper_sendbroadcast_asUser[label="ContextWrapper.sendBroadcastAsUser(Intent,UserHandle)"];
ContextImpl_sendBroadcast[label="ContextImpl.sendBroadcast(Intent)"];
ContextImpl_sendOrderedBroadcast2[label="ContextImpl.sendOrderedBroadcast(Intent,String)"];
ContextImpl_sendOrderedBroadcast[label="ContextImpl.sendOrderedBroadcast(Intent,String,BroadcastReceiver,Handler,int,String,Bundle)"];
ContextImpl_sendStickyBroadcast[label="ContextImpl.sendStickyBroadcast(Intent)"];
ContextImpl_sendStickyBroadcast2[label="ContextImpl.sendStickyOrderedBroadcast(Intent,BroadcastReceiver,Handler,int,String,Bundle)"];
ContextImpl_sendbroadcast_asUser[label="ContextImpl.sendBroadcastAsUser(Intent,UserHandle)"];

ContextImpl_sendOrderedBroadcast_all[label="ContextImpl.sendOrderedBroadcast(Intent,String,int,BroadcastReceiver,Handler,int,String,Bundle,Bundle)"];

ActivityManagerService_broadcastIntent[label="ActivityManagerService.broadcastIntent(IApplicationThread,Intent,String,IIntentReceiver,int,String,Bundle,String[],int,Bundle,boolean,boolean,int)"];

Context_sendbroadcast -> ContextWrapper_sendBroadcast;
ContextWrapper_sendBroadcast -> ContextImpl_sendBroadcast;
ContextImpl_sendBroadcast -> ActivityManagerService_broadcastIntent;

Context_sendbroadcast_asUser -> ContextWrapper_sendbroadcast_asUser;
ContextWrapper_sendbroadcast_asUser -> ContextImpl_sendbroadcast_asUser;
ContextImpl_sendbroadcast_asUser -> ActivityManagerService_broadcastIntent;

Context_sendOrderedBroadcast2 -> ContextWrapper_sendOrderedBroadcast2;
ContextWrapper_sendOrderedBroadcast2 -> ContextImpl_sendOrderedBroadcast2;
ContextImpl_sendOrderedBroadcast2 -> ActivityManagerService_broadcastIntent;

Context_sendOrderedBroadcast -> ContextWrapper_sendOrderedBroadcast;
ContextWrapper_sendOrderedBroadcast -> ContextImpl_sendOrderedBroadcast;
ContextImpl_sendOrderedBroadcast -> ContextImpl_sendOrderedBroadcast_all;
ContextImpl_sendOrderedBroadcast_all -> ActivityManagerService_broadcastIntent;

Context_sendStickyBroadcast -> ContextWrapper_sendStickyBroadcast;
ContextWrapper_sendStickyBroadcast -> ContextImpl_sendStickyBroadcast;
ContextImpl_sendStickyBroadcast -> ActivityManagerService_broadcastIntent;

Context_sendStickyBroadcast2 -> ContextWrapper_sendStickyBroadcast2;
ContextWrapper_sendStickyBroadcast2 -> ContextImpl_sendStickyBroadcast2;
ContextImpl_sendStickyBroadcast2 -> ActivityManagerService_broadcastIntent;
}

我们看看最终生成的图的样子:
用代码画流程图和时序图快餐教程(1) - graphviz的dot图

中文的支持

按照默认的配置,中文显示会成为乱码的。我们需要设置中文字体.
比如下面我们设成仿宋字体:

node [shape = "Mrecord", color="skyblue", style="filled",fontname="SimSun"];

这样,存为UTF-8格式,我们就可以放心地使用中文了。
中文例:

digraph G{
node [shape = "Mrecord", color="skyblue", style="filled",fontname="SimSun"];
edge [color="darkgreen"];

Golang[label="Go语言"]

basic_structure[label="基本结构"]
basic_type[label="基本组件"]
basic_flow_control[label="流程控制"]

variable[label="变量与常量"]
package_manager[label="包管理"]
functions[label="函数"]
types[label="自定义类型"]
reference[label="引用类型"]
goroutines[label="并发"]
control_flow[label="单任务流程控制"]
defers[label="延时型流程控制"]

Golang -> basic_structure;
Golang -> basic_type;
Golang -> basic_flow_control;

basic_structure -> variable;
basic_structure -> package_manager;
basic_type -> functions;
basic_type -> types;
basic_type -> reference;
basic_flow_control -> control_flow;
basic_flow_control -> goroutines;
basic_flow_control -> defers;
}

用代码画流程图和时序图快餐教程(1) - graphviz的dot图

颜色很重要

边和节点都可以设颜色,颜色的值可以在http://www.graphviz.org/content/color-names中查到。

下面我们举个例子,看看颜色的作用:

digraph G{
node [shape = "Mrecord", color="skyblue", style="filled",fontname="SimSun"];
edge [color="darkgreen", fontname="SimHei"];

armv4[label="ARM v4", color="antiquewhite4"]
armv4T[label="ARM-V4T", color="bisque3"]
ARMv5TE[label="ARMv5TE",color="burlywood"]
ARMv6[label="ARM v6",color="cyan3"]
ARMv7A[label="ARM v7-A",color="gold1"]
ARMv8A[label="ARMv8-A",color="chartreuse1"]

arm32[label="ARM 32位指令集",color="darkolivegreen1"]
thumb16[label="Thumb指令集",color="darkseagreen3"]
thumb32[label="Thumb2指令集",color="chocolate3"]
arm64 [label="ARM 64位指令集",color="deepskyblue"]

armv4 -> armv4T [label="发展",color="deeppink"]
armv4T -> ARMv5TE [label="发展",color="deeppink"]
ARMv5TE -> ARMv6 [label="发展",color="deeppink"]
ARMv6 -> ARMv7A [label="发展",color="deeppink"]
ARMv7A -> ARMv8A [label="发展",color="deeppink"]

armv4 -> arm32 [label="支持"]
armv4T -> arm32 [label="支持"]
ARMv5TE -> arm32 [label="支持"]
ARMv6 -> arm32 [label="支持"]
ARMv7A -> arm32 [label="支持"]
ARMv8A -> arm32 [label="支持"]

armv4T -> thumb16 [label="支持"]
ARMv5TE -> thumb16 [label="支持"]
ARMv6 -> thumb16 [label="支持"]

ARMv6 -> thumb32 [label="可选"];
ARMv7A -> thumb32 [label="支持"]
ARMv8A -> thumb32 [label="支持"]

ARMv8A -> arm64 [label="支持"]
}

最终生成的图是这样子的:

用代码画流程图和时序图快餐教程(1) - graphviz的dot图

形状

graphviz的节点支持各种形状,比如椭圆,平行四边形,梯形,鸡蛋形,七边形,八边形,文件夹等。
下面我们看一个各处奇形怪状凑在一起的例子:

digraph G{
node [shape = "Mrecord", color="skyblue", style="filled",fontname="SimSun"];
edge [color="darkgreen", fontname="SimHei"];

java1[label="Java 1.0", color="antiquewhite4",shape="box3d"]

java5[label="Java 5.0", color="bisque3",shape="ellipse"]

java7[label="Java 7.0",color="burlywood",shape="oval"]

java8[label="Java 8.0",color="gold1",shape="septagon"]

Synchronized[label="基于线程和锁的并发",color="deepskyblue",shape="egg"]

Future[label="Future模式",color="darkolivegreen1",shape="octagon"]

ForkJoin[label="Fork-Join框架",color="darkseagreen3",shape="folder"]

Lambda[label="基于Lambda表达式的并发",color="chocolate3",shape="component"]

java1 -> java5 [label="发展",color="deeppink"]
java5 -> java7 [label="发展",color="deeppink"]
java7 -> java8 [label="发展",color="deeppink"]

java1 -> Synchronized;

java5 -> Future

java7 -> ForkJoin

java8 -> Lambda
}

生成的图是这样的:
用代码画流程图和时序图快餐教程(1) - graphviz的dot图

上一篇:Exchange 2010与Exchange Online混合部署PART 1:添加域名


下一篇:如何清除某条SQL在库缓存中的执行计划