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

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

可以嵌入在markdown代码中的mermaid

mermaid的好处是可以在写markdown文档的同时,就直接可以画图了。
下面是我用Haroopad工具直接在markdown中画流图的情况:
工具下载地址:http://pad.haroopress.com/

独立使用mermaid

可以通过npm安装mermad:

npm install -g mermaid
npm install -g phantomjs

mermaid依赖于phantomjs

下面是mermaid的帮助信息:

Usage: mermaid [options] <file>...

file    The mermaid description file to be rendered

Options:
  -s --svg             Output SVG instead of PNG (experimental)
  -p --png             If SVG was selected, and you also want PNG, set this flag

  -o --outputDir       Directory to save files, will be created automatically, d
efaults to `cwd`
  -e --phantomPath     Specify the path to the phantomjs executable
  -t --css             Specify the path to a CSS file to be included when proces
sing output
  -c --sequenceConfig  Specify the path to the file with the configuration to be
 applied in the sequence diagram
  -g --ganttConfig     Specify the path to the file with the configuration to be
 applied in the gantt diagram
  -h --help            Show this message
  -v --verbose         Show logging
  -w --width           width of the generated png (number)
  --version            Print version and quit

mermaid数据流图速成

决定图的方向

与graphviz不同,graph语句首先需要设置图的方向

  • TD: 默认方向,从上到下
  • TB: 从上到下
  • BT: 从下到上
  • LR: 从左到右
  • RL: 从右到左

例:

graph TB;
    Context_sendbroadcast --> ContextWrapper_sendBroadcast;
    ContextWrapper_sendBroadcast --> ContextImpl_sendBroadcast;
    ContextImpl_sendBroadcast --> ActivityManagerService_broadcastIntent;

与graphviz的不同

graphviz的dot图是用“节点1 -> 节点2”的格式,而mermaid是用“节点1 --> 节点2”的方式,中间多了一个"-".

属性定义方面,mermaid可以用[]和()几种方式来决定图形的方式。
比如:

graph TD;
    Context_sendbroadcast(Context.sendBroadcast Intent);
    ContextWrapper_sendBroadcast[ContextWrapper.sendBroadcast Intent];
    Context_sendbroadcast --> ContextWrapper_sendBroadcast;

Context_sendbroadcast就是圆脚的矩形,而ContextWrapper_sendBroadcast就是方的。

需要注意的一点,在标签属性定义的时候,不能出现()[]之类引起误会的符号。官方文档说可以用""来引起来,不过在我的版本上有点问题。

下面还是看跟上面一篇中所讲的graphviz完全一样的图的例子:

graph TD
    Context_sendbroadcast[Context.sendBroadcast Intent];
    Context_sendbroadcast_asUser[Context.sendBroadcastAsUser Intent,UserHandle];
    Context_sendOrderedBroadcast[Context.sendOrderedBroadcast Intent,String,BroadcastReceiver,Handler,int,String,Bundle];
    Context_sendOrderedBroadcast2[Context.sendOrderedBroadcast Intent,String];
    Context_sendStickyBroadcast[Context.sendStickyBroadcast Intent];
    Context_sendStickyBroadcast2[Context.sendStickyOrderedBroadcast Intent,BroadcastReceiver,Handler,int,String,Bundle];

    ContextWrapper_sendBroadcast[ContextWrapper.sendBroadcast Intent];

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

    ActivityManagerService_broadcastIntent(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;

可以嵌入在markdown代码中的mermaid

mermaid的好处是可以在写markdown文档的同时,就直接可以画图了。
下面是我用Haroopad工具直接在markdown中画流图的情况:

mermaid_flow
mermaid_flow

工具下载地址:http://pad.haroopress.com/

独立使用mermaid

可以通过npm安装mermad:

npm install -g mermaid
npm install -g phantomjs

mermaid依赖于phantomjs

下面是mermaid的帮助信息:

Usage: mermaid [options] <file>...

file    The mermaid description file to be rendered

Options:
  -s --svg             Output SVG instead of PNG (experimental)
  -p --png             If SVG was selected, and you also want PNG, set this flag

  -o --outputDir       Directory to save files, will be created automatically, d
efaults to `cwd`
  -e --phantomPath     Specify the path to the phantomjs executable
  -t --css             Specify the path to a CSS file to be included when proces
sing output
  -c --sequenceConfig  Specify the path to the file with the configuration to be
 applied in the sequence diagram
  -g --ganttConfig     Specify the path to the file with the configuration to be
 applied in the gantt diagram
  -h --help            Show this message
  -v --verbose         Show logging
  -w --width           width of the generated png (number)
  --version            Print version and quit

mermaid数据流图速成

决定图的方向

与graphviz不同,graph语句首先需要设置图的方向

  • TD: 默认方向,从上到下
  • TB: 从上到下
  • BT: 从下到上
  • LR: 从左到右
  • RL: 从右到左

例:

graph TB;
    Context_sendbroadcast --> ContextWrapper_sendBroadcast;
    ContextWrapper_sendBroadcast --> ContextImpl_sendBroadcast;
    ContextImpl_sendBroadcast --> ActivityManagerService_broadcastIntent;

与graphviz的不同

graphviz的dot图是用“节点1 -> 节点2”的格式,而mermaid是用“节点1 --> 节点2”的方式,中间多了一个"-".

属性定义方面,mermaid可以用[]和()几种方式来决定图形的方式。
比如:

graph TD;
    Context_sendbroadcast(Context.sendBroadcast Intent);
    ContextWrapper_sendBroadcast[ContextWrapper.sendBroadcast Intent];
    Context_sendbroadcast --> ContextWrapper_sendBroadcast;

Context_sendbroadcast就是圆脚的矩形,而ContextWrapper_sendBroadcast就是方的。

需要注意的一点,在标签属性定义的时候,不能出现()[]之类引起误会的符号。官方文档说可以用""来引起来,不过在我的版本上有点问题。

下面还是看跟上面一篇中所讲的graphviz完全一样的图的例子:

graph TD
    Context_sendbroadcast[Context.sendBroadcast Intent];
    Context_sendbroadcast_asUser[Context.sendBroadcastAsUser Intent,UserHandle];
    Context_sendOrderedBroadcast[Context.sendOrderedBroadcast Intent,String,BroadcastReceiver,Handler,int,String,Bundle];
    Context_sendOrderedBroadcast2[Context.sendOrderedBroadcast Intent,String];
    Context_sendStickyBroadcast[Context.sendStickyBroadcast Intent];
    Context_sendStickyBroadcast2[Context.sendStickyOrderedBroadcast Intent,BroadcastReceiver,Handler,int,String,Bundle];

    ContextWrapper_sendBroadcast[ContextWrapper.sendBroadcast Intent];

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

    ActivityManagerService_broadcastIntent(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;

下面是生成的图片:


test1_md
test1_md

PlantUML画时序图

用描述性的语言来写UML,尤其是Sequence Diagram,PlantUML应该是很多同学的首选了吧。

网址:
http://plantuml.com/

我们可以使用eclipse的plugin来写PlantUML,如下:


plantuml_eclipse

速成教程

@startuml和@enduml

这个没啥说的,开始的时候用@startuml,结束的时候要加个@enduml。

标题

格式: title 标题名

participant

格式:participant 类名 #颜色

例:

@startuml
title Android Broadcast procedure

participant Activity #Lime
participant ContextWrapper #Cyan
@enduml

消息传递

格式:类1 -> 类2 : 消息

例:

@startuml
title Android Broadcast procedure

participant Activity #Lime
participant ContextWrapper #Cyan

Activity -> ContextWrapper : registerReceiver()
@enduml

自动编号

加一句话:autonumber

例子

好了,快餐教程讲多了大家就烦了,所以基本够用的讲完了我们就看个例子吧:

@startuml
title Android Broadcast procedure
'author Ali.Xulun
'version 1.0.0
participant Activity #Lime
participant ContextWrapper #Cyan
participant ContextImpl #Cyan
participant ActivityManagerService #Cyan
participant ActivityStackSupervisor #Cyan
participant ActivityStack #Cyan
participant ApplicationThreadProxy #Silver
participant InnerReceiver #Magenta
participant ReceiverDispatcher #Magenta
participant BroadcastReceiver #Magenta

autonumber
Activity -> ContextWrapper : registerReceiver()
ContextWrapper -> ContextImpl : registerReceiver()
ContextImpl -> LoadedApk : getReceiverDispatcher()
LoadedApk -> ActivityManagerProxy : registerReceiver()
ActivityManagerProxy -> ActivityManagerService : registerReceiver()

Activity -> ContextWrapper : sendBroadcast()
ContextWrapper -> ContextImpl : sendBroadcast()
ContextImpl -> ActivityManagerService: broadcastIntent()
ActivityManagerService -> ActivityManagerService : broadcastIntentLocked()
ActivityManagerService -> ActivityManagerService : collectReceiverComponents()
ActivityManagerService -> ActivityManagerService : scheduleBroadcastsLocked()
ActivityManagerService -> ActivityManagerService : processNextBroadcast()
ActivityManagerService -> ActivityManagerService : deliverToRegisteredReceiverLocked()
ActivityManagerService -> ActivityManagerService : performReceiveLocked()
ActivityManagerService -> ApplicationThreadProxy : scheduleRegisteredReceiver()
ApplicationThreadProxy -> InnerReceiver : performReceive()
InnerReceiver -> ReceiverDispatcher : performReceive()
ReceiverDispatcher -> BroadcastReceiver : onReceive()

Activity -> ContextWrapper : sendOrderedBroadcast()
ContextWrapper -> ContextImpl : sendOrderedBroadcast()
ContextImpl -> ActivityManagerService: broadcastIntent()
@enduml

显示的结果如下:


android_broadcast
android_broadcast

mermaid时序图

时序图声明

mermaid支持多种图,所以要先通过sequenceDiagram

sequenceDiagram

participant

mermaid的participant不支持直接声明颜色,只能声明个名字了

sequenceDiagram
participant Activity
participant ContextWrapper

消息要用双箭头

按照PlantUML的习惯写“->”是不行的,这样会没有箭头。如果要往箭头,还需要再加一个">",变成"->>".

sequenceDiagram
    Activity ->> ContextWrapper : registerReceiver()
    ContextWrapper ->> ContextImpl : registerReceiver()

完整例子

sequenceDiagram
    Activity ->> ContextWrapper : registerReceiver()
    ContextWrapper ->> ContextImpl : registerReceiver()
    ContextImpl ->> LoadedApk : getReceiverDispatcher()
    LoadedApk ->> ActivityManagerProxy : registerReceiver()
    ActivityManagerProxy ->> ActivityManagerService : registerReceiver()

    Activity ->> ContextWrapper : sendBroadcast()
    ContextWrapper ->> ContextImpl : sendBroadcast()
    ContextImpl ->> ActivityManagerService: broadcastIntent()
    ActivityManagerService ->> ActivityManagerService : broadcastIntentLocked()
    ActivityManagerService ->> ActivityManagerService : collectReceiverComponents()
    ActivityManagerService ->> ActivityManagerService : scheduleBroadcastsLocked()
    ActivityManagerService ->> ActivityManagerService : processNextBroadcast()
    ActivityManagerService ->> ActivityManagerService : deliverToRegisteredReceiverLocked()
    ActivityManagerService ->> ActivityManagerService : performReceiveLocked()
    ActivityManagerService ->> ApplicationThreadProxy : scheduleRegisteredReceiver()
    ApplicationThreadProxy ->> InnerReceiver : performReceive()
    InnerReceiver ->> ReceiverDispatcher : performReceive()
    ReceiverDispatcher ->> BroadcastReceiver : onReceive()

    Activity ->> ContextWrapper : sendOrderedBroadcast()
    ContextWrapper ->> ContextImpl : sendOrderedBroadcast()
    ContextImpl ->> ActivityManagerService: broadcastIntent()

生成的图形如下:


test2_mermaid
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,569评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,499评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,271评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,087评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,474评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,670评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,911评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,636评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,397评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,607评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,093评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,418评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,074评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,092评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,865评论 0 196
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,726评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,627评论 2 270

推荐阅读更多精彩内容