利用Graphviz 畫結構圖

1. Graphviz介紹

    Graphviz是大名鼎鼎的貝爾實驗室的幾位牛人開發的一個畫圖工具。

它的理念和一般的“所見即所得”的畫圖工具不一樣,是“所想即所得”。

Graphviz提供了dot語言來編寫繪圖腳本。什麼?!畫個圖也需要一個語言!!

不要急,dot語言是非常簡單地,只要看了下面幾個列子,就能使用了。

 

2. Graphviz的幾個例子

    下面的幾個例子都來自於官方文檔。詳情請見:Graphviz官網.

2.1 Fancy graph

digraph G{

	size = "4, 4";//圖片大小
	main[shape=box];/*形狀*/

	main->parse;
	parse->execute;

	main->init[style = dotted];//虛線

	main->cleanup;

	execute->{make_string; printf}//連接兩個

	init->make_string;

	edge[color = red]; // 連接線的顏色

	main->printf[style=bold, label="100 times"];//線的 label

	make_string[label = "make a/nstring"]// /n, 這個node的label,注意和上一行的區別

	node[shape = box, style = filled, color = ".7.3 1.0"];//一個node的屬性

	execute->compare;
}

從上面的代碼可以看出,dot語言非常簡單,就是一個純描述性的語言而已。

大家可以把上面的代碼和下圖中的連接對應起來看。

                                       1

                                              <圖1. Fancy graph>

 

2.2 Polygon graph

digraph G{
	size = "4, 4"
	a->b->c;
	b->d;
	
	a[shape = polygon, sides = 5, peripheries=3, color = lightblue, style = filled];
	//我的形狀是多邊形,有五條邊,3條邊框, 顏色的淡藍色, 樣式爲填充
	c[shape = polygon, sides = 4, skew= 0.4, lable = "hello world"];
	//我的形狀是4變形, 角的彎曲度0.4, 裏面的內容爲"hello world"
	d[shape = invtriange];
	//我是三角形
	e[shape = polygon, side = 4, distortion = .7];
	//我是梯形啊
}

 

 

下面是對應的圖片:

                                          polygon

                                       <圖2. Polygon graph>

2.3 連接點的方向

     我們可以用“n”,”ne”,”e”,””se”, “sw”,”w”,”nw”,

分別表示衝哪一個方向連接這個節點(圖形)-“north, northeast……”

如:

digraph G{
	//b->c[tailport = se];
	b->c:se;
}

 

 

                                         se

                                           <圖3. Se graph>

 

2.4 數據結構圖

    數據結構圖是我們很容易用到的一類圖形,一個簡單地數據結構圖代碼如下:

digraph g{
	node [shape = record,height=.1//我定義了我下面的樣式;  
 	node0[label = "<f0> |<f1> G|<f2> "];  
	//我是一個node,我有三個屬性,第二個的名字爲G,其他兩個爲空
        node1[label = "<f0> |<f1> E|<f2> "];  
       node2[label = "<f0> |<f1> B|<f2> "];  
       node3[label = "<f0> |<f1> F|<f2> "];  
       node4[label = "<f0> |<f1> R|<f2> "];  
       node5[label = "<f0> |<f1> H|<f2> "];  
       node6[label = "<f0> |<f1> Y|<f2> "];  
       node7[label = "<f0> |<f1> A|<f2> "];  
       node8[label = "<f0> |<f1> C|<f2> "];  
	
	"node0": f2->"node4":f1;
	//我的第三個屬性連到node4的第二個屬性
	"node0": f0->"node1":f1;
	"node1": f0->"node2":f1;
	"node1": f2->"node3":f1;
	"node2": f2->"node8":f1;
	"node2": f0->"node7":f1;
	"node4": f2->"node6":f1;
	"node4": f0->"node5":f1;
}

 

 

                                         data

                                               <圖4. Data graph>

2.5 Hash table graph

digraph g {
	nodesep = .05;
	rankdir = LR;
	
	node[shape = record, width = .1, height = .1];
	
	node0[label = "<f0> |<f1> |<f2> |<f3> |<f4> |<f5> |<f6> |", height = 2.5];
	//我是一個節點,我有7個屬性
	node [width = 1.5];
	node1[label = "{<n> n14 | 719 |<p>}"];
	//我還是一個節點, 也定義了三個屬性
	node2[label = "{<n> a1 | 719 |<p>}"];
	node3[label = "{<n> i9 | 512 |<p>}"];
	node4[label = "{<n> e5 | 632 |<p>}"];
	node5[label = "{<n> t20 | 959 |<p>}"];
	node6[label = "{<n> o15 | 794 |<p>}"];
	node7[label = "{<n> s19 | 659 |<p>}"];

	//好了,我開始連接了
	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;
}

 

這是一個簡單地哈希表,如下圖所示

                                             Hash

                                                          <圖5. Hash table graph>

 

 

2.6 Process grahp

下面畫一個輕量級的流程圖。

digraph g {
	subgraph cluster0 {
		//我是一個子圖,subgraph定義了我,
		node[style = filled, color = white];
		//我之內的節點都是這種樣式
		style = filled;
		//我的樣式是填充
		color = lightgrey;
		//我的顏色
		a0->a1->a2->a3;
		label = "prcess #1"
		//我的標題
	}

	subgraph cluster1 {
		//我也是一個子圖
		node[style = filled];
		b0->b1->b2->b3;
		label = "process #2";
		color = blue;
	}

	//定義完畢之後,下面還是連接了
	start->a0;
	start->b0;
	a1->b3;
	b2->a3;
	a3->end;
	b3->end;
	
	start[shape=Mdiamond];
	end[shape=Msquare];
}

 

 

 

結果輸出圖形如下:

                                             Process

                                                <圖6. Hash table graph>

 

3. 小結

    相信這幾個列子下來,各位看官對graphviz也有了瞭解了吧,我個人用了一遍下來發現太爽了。

而對於dot語言,作爲一個描述性的語言就非常簡單了, 只要有編程基礎的人,模仿幾個列子下來

應該就能應用了。

    各位看官,有沒有心動啊。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章