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

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

版本历史:

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

graphviz的dot图

工具下载网址:http://www.graphviz.org/。
可以使用sublime text来编辑。

Windows下的GVEdit

在Windows下,Graphviz提供一个GVedit工具来编辑。

命令行方式

在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;
}

我们看看最终生成的图的样子:

中文的支持

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

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;
}

颜色很重要

边和节点都可以设颜色,颜色的值可以在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="支持"]
}

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

形状

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
}

生成的图是这样的:

时间: 2024-12-31 09:01:04

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

用代码画流程图和时序图快餐教程(2) - mermaid数据流图速成

可以嵌入在markdown代码中的mermaid mermaid的好处是可以在写markdown文档的同时,就直接可以画图了. 下面是我用Haroopad工具直接在markdown中画流图的情况: 工具下载地址:http://pad.haroopress.com/ 独立使用mermaid 可以通过npm安装mermad: npm install -g mermaid npm install -g phantomjs mermaid依赖于phantomjs 下面是mermaid的帮助信息: Usa

用代码画流程图和时序图快餐教程(3) - PlantUML画时序图

PlantUML画时序图 用描述性的语言来写UML,尤其是Sequence Diagram,PlantUML应该是很多同学的首选了吧. 网址:http://plantuml.com/ 我们可以使用eclipse的plugin来写PlantUML,如下: 速成教程 @startuml和@enduml 这个没啥说的,开始的时候用@startuml,结束的时候要加个@enduml. 标题 格式: title 标题名 participant 格式:participant 类名 #颜色 例: @start

用代码画流程图和时序图快餐教程(4) - mermaid时序图

mermaid时序图 时序图声明 mermaid支持多种图,所以要先通过sequenceDiagram sequenceDiagram participant mermaid的participant不支持直接声明颜色,只能声明个名字了 sequenceDiagram participant Activity participant ContextWrapper 消息要用双箭头 按照PlantUML的习惯写"->"是不行的,这样会没有箭头.如果要往箭头,还需要再加一个"&

用代码画UML类图快餐教程

之前我们讨论了graphviz的dot图,mermaid画流程图和时序图,plantuml画时序图. plantuml除了可以画时序图之外,还可以画类图.就跟写代码一样,直接上例子: @startuml interface Tree{ + Tree getParent() + Object getPayload() + Tree getChild(int i) + int getChildCount() + String toStringTree() } interface SyntaxTree

用代码画折线图/柱形图/条形图/饼图

饼图|折线图|柱形图 本文将介绍在.Net中如何使用代码画图表,就像用MS Excel生成的图表一样.也可以画像DataGrid一样的表格. 在.Net中,微软给我们提供了画图类(System.Drawing.Imaging),在该类中画图的基本功能都有.比如:直线.折线.矩形.多边形.椭圆形.扇形.曲线等等,因此一般的图形都可以直接通过代码画出来.接下来介绍一些画图函数:Bitmap bMap=new Bitmap(500,500) //定义图像大小:bMap.Save(Stream,Imag

用代码画折线图

问题描述 在c#.NET中WEB页面用代码画折线图并在图中加条直线通过移动直线可以在TEXBOX或别的控件中显示值有比较全的代码或例子吗谢谢本人新手最好代码就剩这几分了 解决方案 解决方案二:........解决方案三:该回复于2008-11-05 18:57:10被版主删除解决方案四:vml

怎样在word中画流程图

word画流程图方法一: 步骤一:找到word的"插入" 步骤二:点击之后,点击"形状",如图所示 步骤三:点击"形状",出现下图这样 步骤四:选择相应的形状,构建流程图

Wps文字表格也能画流程图

流程图可以在页面直接画,总之通过绘图工具画好,大量图组合,难度有点大,弄不好,辛苦劳动有时白流.用表格画流程图,不用组合,图可以随着表格移动,可以调整任何位置. 1.简单的图形 2.复杂的图形 三联推荐:点击免费下载最新版WPS办公软件    >>查看更多电脑教程 以上只是以简单的举例说明,绘图时用表格也能绘出复杂图形,满足自己工作的需求.

yEd 3.7.0.1发布 画流程图的工具

yed 是一个画流程图的工具,不是开源软件,但是可以让你摆脱 http://www.aliyun.com/zixun/aggregation/11208.html">Microsoft Office Visio  ,也有linux下的版本,整个程序是java开发的,确实比较强大. 是一个功能强大的图表编辑器,可用于快速手动创建图表.您可以使用YED快速创建流程图,BPMN和UML图,组织图,心智图,实体关系图和图表,图形和网络等等.直观的用户界面和视觉吸引力使创建图表更有乐趣.一旦创建了一