1. 程式人生 > >用程式碼畫流程圖和時序圖快餐教程(2)

用程式碼畫流程圖和時序圖快餐教程(2)

可以嵌入在markdown程式碼中的mermaid

mermaid的好處是可以在寫markdown文件的同時,就直接可以畫圖了。
下面是我用Haroopad工具直接在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;

下面是生成的圖片: