<一> graphviz dot
使用步驟
- 安裝graphviz
brew install graphviz
(mac os x系統(tǒng)) - 創(chuàng)建文本文件并命名為*.dot, 編寫dot腳本
- 在第二部創(chuàng)建的文件中編寫腳本
- 編譯腳本, 輸出圖片
- 編譯命令:
dot -Tpng *.dot -o *.png
- 記得把
*
換成具體的文件名, 這樣你就成功的用腳本渲染出你要繪制的圖片啦
- 編譯命令:
<二> graphviz dot
語言相關知識
- 注釋
- 使用雙斜杠注釋
- 有向圖
- 使用
digraph
定義有向圖 - 使用
->
表述節(jié)點之間的關系 - 如:
```dot
digraph g {
a->b;
b->c;
c->a;
}
```
效果如下:
a.png
- 無向圖
- 使用
graph
定義無向圖 - 使用
--
表述節(jié)點之間的關系 - 節(jié)點之間的關系
- 表述節(jié)點直接的關系如下:
- 有向圖:
a -> b
, a節(jié)點指向b節(jié)點 - 無像圖:
a -- b
, a節(jié)點與b節(jié)點連通
- 有向圖:
- 定義節(jié)點屬性
- 定義屬性, 格式為:
node[attribute1=value1, attribute2=value2]
- 如:
```
//定義a節(jié)點為長方形, 節(jié)點顯示的文本為"Hello world"樣式為填充, 填充顏色為#ABACBA
a[shape=box,label="Hello world",style=filled,fillcolor="#ABACBA"];
```
- 各種形狀請參考下面第7條
- 定義關系屬性(即連接兩個節(jié)點之間的線的樣式)
- 定義節(jié)點的形狀
- 栗子:
```dot
//定義節(jié)點屬性
digraph g {
//==========定義節(jié)點關系============
a->b;
b->c;
c->a;
c->d->e->f;
d->g;
e->h;
//==========定義節(jié)點屬性============
//定義a節(jié)點為長方形, 樣式為填充, 填充顏色為#ABACBA
a[shape=box,label="Server1\nWebServer",fillcolor="#ABACBA",style=filled];
//定義b為5邊形, 標簽為"bb", 樣式為填充, 填充色為red
b[shape=polygon,sides=5,label="bb",style=filled,fillcolor=red];
//c, 默認為橢圓
d[shape=circle]; //園
e[shape=triangle]; //三角形
f[shape=polygon, sides=4, skew=0.5]; //平行四邊形
g[shape=polygon, distortion=0.5]; //梯形, 上邊長
h[shape=polygon, distortion=-.5]; //梯形, 下邊長
}
```
效果如下:
a.png
- 哈希表(hash table)
```
digraph g {
nodesep = .5;
rankdir = LR; //指定繪圖的方向 (LR從左到右繪制)
//定義豎直節(jié)點
node[shape=record, width=.1, height=.1];
node0[label="<f0> |<f1> |<f2> |<f3> |<f4> |<f5> |<f6> ", height=2.5]; //我是一個屬性, 我有7個屬性
//定義橫向節(jié)點
node[width=1.5];
node1[label="{<n> a13 | 111 | <p>}"]; //我也是一個節(jié)點, 定義了3個屬性
node2[label="{<n> hello | 2387 | <p>}"];
node3[label="{<n> g23 | 344 | <p>}"];
node4[label="{<n> k535 | 246 | <p>}"];
node5[label="{<n> h25 | 13 | <p>}"];
node6[label="{<n> dj | 04 | <p>}"];
node7[label="{<n> sbd | 0x543 | <p>}"];
//建立節(jié)點之間的聯(lián)系
node0:f0 -> node1:n;
node0:f1 -> node2:n;
node0:f2 -> node3:n;
node0:f5 -> node4:n;
node0:f6 -> node5:n;
node2:p -> node6:n;
node4:p -> node7:n;
}
```
效果如下:
![a.png](http://upload-images.jianshu.io/upload_images/1642441-4a6b6edecad8f422.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
- 更多形狀
* 請參考[官方文檔](http://www.graphviz.org/Documentation.php)
* 或者下面
271314316446130.jpg
- 定義結(jié)構(gòu)
- 一個簡單的數(shù)據(jù)結(jié)構(gòu)示例
digraph g { node[shape=record,height=.1]; //定義了下面的node樣式 node0[label="<f0> |<f1> A|<f2> "]; //我是一個node, 我有三個屬性, 第二個屬性名字為A, 其他兩個為空 node1[label="<f0> |<f1> B|<f2> "]; node2[label="<f0> |<f1> C|<f2> "]; node3[label="<f0> |<f1> D|<f2> "]; node4[label="<f0> |<f1> E|<f2> "]; node5[label="<f0> |<f1> F|<f2> "]; node6[label="<f0> |<f1> H|<f2> "]; node7[label="<f0> |<f1> I|<f2> "]; node8[label="<f0> |<f1> J|<f2> "]; node9[label="<f0> |<f1> K|<f2> "]; "node0":f2 -> "node1": f1; //node0的第三個屬性連到node1的第二個屬性 "node1":f0 -> "node2": f1; "node1":f1 -> "node3": f2; "node3":f0 -> "node4": f0; "node3":f1 -> "node5": f1; "node3":f2 -> "node6": f2; "node6":f1 -> "node7": f1; "node7":f1 -> "node8": f0; "node2":f2 -> "node9": f1; }
效果如下:
a.png使用node定義結(jié)構(gòu), node[shape=record]
-
使用subgraph定義子圖
digraph g { //定義一個子圖, subgraph定義子圖 subgraph cluster0 { node[style=filled, color=white]; //定義子圖中的節(jié)點的樣式 style=filled; //定義子圖的樣式 color=red; //定義子圖的填充色 a0->a1->a2->a3; //定義節(jié)點, 及節(jié)點之間的關系 label="process #1"; //定義子圖的標簽 } //又定義一個子圖 subgraph cluster1 { node[style=filled, color=white]; style=filled; color=blue; //定義子圖的填充色 b0->b1->b2->b3; //定義節(jié)點及其關系 label="process #2"; labelColor=white; } //定義子圖之間的關系 start->a0; start->b0; a1->b3; b2->a3; a3->end; b3->end; }
效果如下:
demo6.png 更多結(jié)構(gòu)請參考官方文檔
-
常用顏色如下:
271313527842101.jpg
- 定義關系的樣式(節(jié)點之間的連線的樣式)
-
有向圖
digraph g { //edge[style=dashed]; //定義邊的樣式, 虛線 node[peripheries=2, style=filled, color="#eecc80"]; a->b [color=red, style=dashed]; //定義邊的顏色, 紅色 (b和方括號之間必須有空格) b->c; //箭頭, 三角形; 箭尾, 菱形 b->d [arrowhead=box]; //箭頭, 長方形 b->e [dir=none]; //沒有箭頭 d->f [dir=both]; //雙向箭頭 f->h [label=go]; //定義edge的標簽 f->k [arrowhead=diamond]; //更改箭頭形狀 (更多箭頭形狀請參考官方文檔: http://www.graphviz.org/content/arrow-shapes) k->y [headlabel="哈哈", taillabel="洗洗"]; }
效果如下:
a.png -
無向圖
graph g { edge[style=dashed]; //定義邊的樣式, 虛線 a -- b [color=red]; //定義邊的顏色, 紅色 (b和方括號之間必須有空格) }
效果如下:
demo8.png
-
references:
graphviz官網(wǎng)
graphviz文檔地址
dot官方文檔
使用graphviz繪制流程圖