有時候我們在linux下也會有繪制流程圖的需求,你是不是也愁找不到一款合適的繪圖工具呢,當然也有好多圖形化的也是非常好用的(默默的說一聲,我也是一個資深的菜鳥,當然沒有用過了,也是工作沒有用到)
好了,進入主題吧,這裡有兩位前輩的帖子,感覺不錯所以就借鑒過來了。
http://codingstandards.iteye.com/blog/840055
http://blog.chinaunix.net/uid-26009923-id-3151636.html
首先創建一個dot文件,fllow.dot
vi fllow.dot
digraph G {
hello [shape=box];
world [style=filled, color="1,1,1"];
hello -> world [label="Yes"];
}
然後編譯,導出一個png文件
sudo apt-get install graphviz
dot -Tpng flow.dot -o flow.png
最後就可以預覽了
sudo apt-get install eog
eog ./flow.png
用途說明
Graphviz (Graph Visualization Software的縮寫)是一個由AT&T實驗室啟動的開源工具包,用於繪制DOT語言腳本描述的圖形。它也提供了供其它軟件使用的庫。Graphviz是一個自由軟件,其授權為Common Public License。其Mac版本曾經獲得2004年的蘋果設計獎。Graphviz包括很多命令行工具,dot命令是一個用來將生成的圖形轉換成多種輸出格式的命令行工具,其輸出格式包括PostScript,PDF,SVG,PNG,含注解的文本等等。neato命令用於spring
model的生成(在Mac OS版本中稱為energy minimized)。twopi命令用於放射狀圖形的生成。circo命令用於圓形圖形的生成。fdp命令另一個用於生成無向圖的工具。dotty命令一個用於可視化與修改圖形的圖形用戶界面程序。lefty命令是一個可編程的(使用一種被EZ影響的語言[4])控件,它可以顯示DOT圖形,並允許用戶用鼠標在圖上執行操作。Lefty可以作為MVC模型的使用圖形的GUI程序中的視圖部分。 DOT語言是一種文本圖形描述語言。它提供了一種簡單的描述圖形的方法,並且可以為人類和計算機程序所理解。DOT語言文件通常是具有.gv或是.dot的文件擴展名。本文將主要介紹從源代碼安裝Graphviz工具以及dot命令的使用方式。
常用參數
格式:dot -T<type> -o<outfile> <infile.dot>
輸入文件是<infile.dot>,生成的格式由<type>指定,生成的文件是<outfile>。
其中-T<type>包括:
-Tps (PostScript),
-Tsvg -Tsvgz (Structured Vector Graphics),
-Tfig (XFIG graphics),
-Tmif (FrameMaker graphics),
-Thpgl (HP pen plotters),
-Tpcl (Laserjet printers),
-Tpng -Tgif (bitmap graphics),
-Tdia (GTK+ based diagrams),
-Timap (imagemap files for httpd servers for each node or edge that has a non-null "href" attribute.),
-Tcmapx (client-side imagemap for use in html and xhtml).
下面簡單介紹下DOT語言的語法:
無向圖:在最簡單的應用中,DOT語言可以用來描述一張無向圖。無向圖顯示了對象間最簡單的關系,例如人之間的友誼。使用關鍵字graph開始一張無向圖的定義,並用大括號包含要描述的節點,雙連字號(--)被用來描述節點間的關系。另外,一行的末尾需要加上分號(;)。
graph graphname {
a -- b -- c;
b -- d;
}
有向圖:類似於無向圖,DOT語言也可以用來描述一張有向圖,類似於流程圖和樹狀圖。其語法與無向圖相似,但要在圖的最開始使用關鍵字'digraph',並用箭頭(->)表示節點直接的關系。
digraph graphname {
a -> b -> c;
b -> d;
}
屬性:DOT語言中,可以對節點和邊添加不同的屬性。這些屬性可以控制節點和邊的顯示樣式,例如顏色,形狀和線形。可以在語句和句尾的分號間放置一對方括號,並在其中中放置一個或多個屬性-值對。多個屬性可以被逗號和空格(, )分開。節點的屬性被放置在只包含節點名稱的表達式後。
graph graphname {
// label屬性可以改變節點的顯示名稱
a [label="Foo"];
// 節點形狀被改變了
b [shape=box];
// a-b邊和b-c邊有相同的屬性
a -- b -- c [color=blue];
b -- d [style=dotted];
}
注釋:DOT語言支持C語言與C++風格的單行與多行注釋。另外,也支持Shell腳本風格的以#開頭的注釋。
// 單行注釋
/* 多行
注
釋 */
# 如此的行也會被忽略。
使用示例
示例一 從源代碼安裝Graphviz
[root@jfht setup]# wget "http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.26.3.tar.gz"
--2010-12-12 09:10:26-- http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.26.3.tar.gz
正在解析主機 www.graphviz.org... 192.20.225.20
Connecting to www.graphviz.org|192.20.225.20|:80... 已連接。
已發出 HTTP 請求,正在等待回應... 200 OK
長度:17092429 (16M) [application/x-gzip]
Saving to: `graphviz-2.26.3.tar.gz'
100%[==========================================================================================>] 17,092,429 174K/s in 1m 44s
2010-12-12 09:12:10 (161 KB/s) - `graphviz-2.26.3.tar.gz' saved [17092429/17092429]
[root@jfht setup]# tar zxf graphviz-2.26.3.tar.gz
[root@jfht setup]# cd graphviz-2.26.3
[root@jfht graphviz-2.26.3]# ls
aclocal.m4 builddate.h config.h.in configure.old dot.demo graphviz.sln INSTALL makearch plugin
ast_common.h ChangeLog config.h.old contrib Doxyfile graphviz.spec INSTALL.old Makeargs README
ast_common.h.in cmd config.iffe COPYING Doxyfile.in graphviz.spec.in lib Makefile.am rtest
AUTHORS compat_getopt.h Config.mk.old cpl1.0.txt features graphviz.vcproj libltdl Makefile.in share
autogen.sh compat.h configure debian graphs graphviz_version.h m4 Makefile.old tclpkg
awk config configure.ac doc graphviz.7 iffe macosx NEWS windows
[root@jfht graphviz-2.26.3]# ./configure --prefix=/usr
checking build system type... i686-pc-linux-gnu
checking host system type... i686-pc-linux-gnu
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for style of include used by make... GNU
checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
省略輸出
----------------------------------------------------------------
graphviz-2.26.3 will be compiled with the following:
options:
cgraph: No (disabled by default - experimental)
digcola: Yes
expat: No (missing library)
fontconfig: No (missing fontconfig-config)
freetype: No (missing freetype-config)
glut: No (missing GL/glut.h)
gts: No (gts library not available)
ipsepcola: No (disabled by default - C++ portability issues)
ltdl: Yes
ortho: No (disabled by default - experimental)
png: No (missing png.h)
jpeg: No (missing header)
sfdp: Yes
shared: Yes
static: No (disabled by default)
commands:
dot: Yes (always enabled)
neato: Yes (always enabled)
fdp: Yes (always enabled)
circo: Yes (always enabled)
twopi: Yes (always enabled)
gvpr: Yes (always enabled)
lefty: No (missing Xaw headers)
smyrna: No (disabled by default - experimental)
plugin libraries:
dot_layout: Yes (always enabled)
neato_layout: Yes (always enabled)
core: Yes (always enabled)
devil: No (missing library)
gd: Yes (internal)
gdiplus: No (disabled by default - Windows only)
gdk_pixbuf: No (gdk_pixbuf library not available)
ghostscript: No (missing headers)
glitz: No (disabled by default - incomplete)
gtk: No (gtk library not available)
lasi: No (lasi library not available)
ming: No (disabled by default - incomplete)
pangocairo: No (pangocairo library not available)
quartz: No (disabled by default - Mac only)
rsvg: No (rsvg library not available)
visio: No (disabled by default - experimental)
xlib: No (disabled or unavailable)
language extensions:
gv_sharp: No (swig not available)
gv_guile: No (swig not available)
gv_io: No (disabled by default - no swig support yet)
gv_java: No (swig not available)
gv_lua: No (swig not available)
gv_ocaml: No (swig not available)
gv_perl: No (swig not available)
gv_php: No (swig not available)
gv_python: No (swig not available)
gv_python23: No (disabled by default - for multiversion installs)
gv_python24: No (disabled by default - for multiversion installs)
gv_python25: No (disabled by default - for multiversion installs)
gv_python26: No (disabled by default - for multiversion installs)
gv_R: No (swig not available)
gv_ruby: No (swig not available)
gv_tcl: No (tcl not available)
tcldot: No (tcl not available)
tclpathplan: No (tcl not available)
gdtclft: No (tcl not available)
tkspline: No (tk not available)
[root@jfht graphviz-2.26.3]#
[root@jfht graphviz-2.26.3]# make && make install
test -z "/usr/share/graphviz/doc/html" || /bin/mkdir -p "/usr/share/graphviz/doc/html"
test -z "/usr/share/man/man7" || /bin/mkdir -p "/usr/share/man/man7"
/usr/bin/install -c -m 644 'graphviz.7' '/usr/share/man/man7/graphviz.7'
test -z "/usr/include/graphviz" || /bin/mkdir -p "/usr/include/graphviz"
/usr/bin/install -c -m 644 'graphviz_version.h' '/usr/include/graphviz/graphviz_version.h'
test -z "/usr/share/graphviz/doc" || /bin/mkdir -p "/usr/share/graphviz/doc"
/usr/bin/install -c -m 644 'AUTHORS' '/usr/share/graphviz/doc/AUTHORS'
/usr/bin/install -c -m 644 'COPYING' '/usr/share/graphviz/doc/COPYING'
/usr/bin/install -c -m 644 'ChangeLog' '/usr/share/graphviz/doc/ChangeLog'
/usr/bin/install -c -m 644 'NEWS' '/usr/share/graphviz/doc/NEWS'
/usr/bin/install -c -m 644 'cpl1.0.txt' '/usr/share/graphviz/doc/cpl1.0.txt'
make[2]: Leaving directory `/root/setup/graphviz-2.26.3'
make[1]: Leaving directory `/root/setup/graphviz-2.26.3'
You have new mail in /var/spool/mail/root
[root@jfht graphviz-2.26.3]#
示例二 操作系統的進程狀態轉換圖
下面的例子來自http://www.graphviz.org/Gallery/undirected/process.html。
[root@jfht ~]# cat process.gv
graph G {
run -- intr;
intr -- runbl;
runbl -- run;
run -- kernel;
kernel -- zombie;
kernel -- sleep;
kernel -- runmem;
sleep -- swap;
swap -- runswap;
runswap -- new;
runswap -- runmem;
new -- runmem;
sleep -- runmem;
}
[root@jfht ~]# dot -Tpng -oprocess.png process.gv
Format: "png" not recognized. Use one of: canon cmap cmapx cmapx_np dot eps fig gd gd2 gif gv imap imap_np ismap plain plain-ext ps ps2 svg svgz tk vml vmlz wbmp xdot
[root@jfht ~]# dot -Tgif -oprocess.gif process.gv
[root@jfht ~]# dot -Tsvg -oprocess.svg process.gv
[root@jfht ~]#
示例三 中文支持
要想在dot文件中使用中文,必須要安裝有fontconfig-devel和freetype-devel。如示例一所示的安裝中就沒有這兩項,
fontconfig: No (missing fontconfig-config)
freetype: No (missing freetype-config)
先得安裝上,先把Linux安裝盤掛載上。安裝步驟如下
[root@jfht software]# find . -name "*fontconfig*"
./Server/fontconfig-devel-2.4.1-7.el5.i386.rpm
./Server/fontconfig-2.4.1-7.el5.i386.rpm
[root@jfht software]#
[root@jfht software]#
[root@jfht software]# rpm -qa | grep fontconfig
fontconfig-2.4.1-7.el5
[root@jfht software]# rpm -ivh ./Server/fontconfig-devel-2.4.1-7.el5.i386.rpm
warning: ./Server/fontconfig-devel-2.4.1-7.el5.i386.rpm: Header V3 DSA signature: NOKEY, key ID 37017186
error: Failed dependencies:
freetype-devel >= 2.1.4 is needed by fontconfig-devel-2.4.1-7.el5.i386
[root@jfht software]# find . -name "freetype-devel*rpm"
./Server/freetype-devel-2.2.1-21.el5_3.i386.rpm
[root@jfht software]# rpm -ivh ./Server/freetype-devel-2.2.1-21.el5_3.i386.rpm ./Server/fontconfig-devel-2.4.1-7.el5.i386.rpm
warning: ./Server/freetype-devel-2.2.1-21.el5_3.i386.rpm: Header V3 DSA signature: NOKEY, key ID 37017186
Preparing... ########################################### [100%]
1:freetype-devel ########################################### [ 50%]
2:fontconfig-devel ########################################### [100%]
[root@jfht software]#
安裝好這兩個rpm包之後,再到graphviz源代碼目錄,執行
./configure --prefix=/usr
make && make install
這樣dot就可以支持中文了。首先我們編寫一個簡單的dot文件。
[root@jfht ~]# cat cn.gv
graph G {
中文 -- 英文
}
[root@jfht ~]# dot -Tgif -ocn.gif cn.gv
Error: Invalid 2-byte UTF8 found in input. Perhaps "-Gcharset=latin1" is needed?
要注意的是:dot不支持gbk編碼的中文,但可以支持UTF8的中文,所以先把它轉換成UTF8編碼的。
[root@jfht ~]# iconv -f GBK -t UTF8 cn.gv >cn8.gv
[root@jfht ~]# dot -Tgif -ocn8.gif cn8.gv
[root@jfht ~]#
示例四 改變中文字體
默認的中文字體是宋體的,如果想改成仿宋、黑體、幼圓或別的字體,在Linux下可能沒有,那麼可以在Windows下找到相應的字體文件,上傳的LInux上使用。步驟如下:
到C:\Windows\Fonts下找到simyou.ttf,用sftp上傳到Linux下的/usr/share/fonts/chinese/TrueType目錄。
[root@jfht ~]# cd /usr/share/fonts/chinese/TrueType
[root@jfht TrueType]#
mkfontscale [root@jfht TrueType]#
mkfontdir[root@jfht TrueType]#
fc-cache[root@jfht TrueType]# cat fonts.dir
注意下面內容中粗體部分。
62
fangsong.ttf -misc-
fangsong_gb2312-medium-r-normal--0-0-0-0-p-0-gb2312.1980-0
fangsong.ttf -misc-fangsong_gb2312-medium-r-normal--0-0-0-0-p-0-iso10646-1
simhei.ttf -misc-
simhei-medium-r-normal--0-0-0-0-p-0-cns11643-1
simhei.ttf -misc-simhei-medium-r-normal--0-0-0-0-p-0-cns11643-2
simhei.ttf -misc-simhei-medium-r-normal--0-0-0-0-p-0-cns11643-3
simhei.ttf -misc-simhei-medium-r-normal--0-0-0-0-p-0-gb18030.2000-0
simhei.ttf -misc-simhei-medium-r-normal--0-0-0-0-p-0-gb2312.1980-0
simhei.ttf -misc-simhei-medium-r-normal--0-0-0-0-p-0-iso10646-1
simhei.ttf -misc-simhei-medium-r-normal--0-0-0-0-p-0-jisx0208.1983-0
simhei.ttf -misc-simhei-medium-r-normal--0-0-0-0-p-0-jisx0208.1990-0
simsun.ttc -misc-
simsun-medium-r-normal--0-0-0-0-p-0-ascii-0
simsun.ttc -misc-simsun-medium-r-normal--0-0-0-0-p-0-cns11643-1
simsun.ttc -misc-simsun-medium-r-normal--0-0-0-0-p-0-cns11643-2
simsun.ttc -misc-simsun-medium-r-normal--0-0-0-0-p-0-cns11643-3
simsun.ttc -misc-simsun-medium-r-normal--0-0-0-0-p-0-gb18030.2000-0
simsun.ttc -misc-simsun-medium-r-normal--0-0-0-0-p-0-gb2312.1980-0
simsun.ttc -misc-simsun-medium-r-normal--0-0-0-0-p-0-iso10646-1
simsun.ttc -misc-simsun-medium-r-normal--0-0-0-0-p-0-iso8859-1
simsun.ttc -misc-simsun-medium-r-normal--0-0-0-0-p-0-jisx0208.1983-0
simsun.ttc -misc-simsun-medium-r-normal--0-0-0-0-p-0-jisx0208.1990-0
simsun.ttc -misc-simsun-medium-r-normal--0-0-0-0-p-0-koi8-r
simyou.ttf -misc-
youyuan-medium-r-normal--0-0-0-0-m-0-cns11643-2
simyou.ttf -misc-youyuan-medium-r-normal--0-0-0-0-m-0-gb2312.1980-0
simyou.ttf -misc-youyuan-medium-r-normal--0-0-0-0-m-0-iso10646-1
stcaiyun.ttf -misc-
stcaiyun-medium-r-normal--0-0-0-0-p-0-ascii-0
stcaiyun.ttf -misc-stcaiyun-medium-r-normal--0-0-0-0-p-0-gb2312.1980-0
stcaiyun.ttf -misc-stcaiyun-medium-r-normal--0-0-0-0-p-0-iso10646-1
stcaiyun.ttf -misc-stcaiyun-medium-r-normal--0-0-0-0-p-0-iso8859-1
ukai.ttf -misc-ar pl zenkai uni-medium-r-normal--0-0-0-0-p-0-ascii-0
ukai.ttf -misc-ar pl zenkai uni-medium-r-normal--0-0-0-0-p-0-big5-0
ukai.ttf -misc-ar pl zenkai uni-medium-r-normal--0-0-0-0-p-0-big5.eten-0
ukai.ttf -misc-ar pl zenkai uni-medium-r-normal--0-0-0-0-p-0-cns11643-1
ukai.ttf -misc-ar pl zenkai uni-medium-r-normal--0-0-0-0-p-0-cns11643-2
ukai.ttf -misc-ar pl zenkai uni-medium-r-normal--0-0-0-0-p-0-gb2312.1980-0
ukai.ttf -misc-ar pl zenkai uni-medium-r-normal--0-0-0-0-p-0-iso10646-1
ukai.ttf -misc-ar pl zenkai uni-medium-r-normal--0-0-0-0-p-0-iso8859-10
ukai.ttf -misc-ar pl zenkai uni-medium-r-normal--0-0-0-0-p-0-iso8859-13
ukai.ttf -misc-ar pl zenkai uni-medium-r-normal--0-0-0-0-p-0-iso8859-15
ukai.ttf -misc-ar pl zenkai uni-medium-r-normal--0-0-0-0-p-0-iso8859-16
ukai.ttf -misc-ar pl zenkai uni-medium-r-normal--0-0-0-0-p-0-iso8859-1
ukai.ttf -misc-ar pl zenkai uni-medium-r-normal--0-0-0-0-p-0-iso8859-2
ukai.ttf -misc-ar pl zenkai uni-medium-r-normal--0-0-0-0-p-0-iso8859-3
ukai.ttf -misc-ar pl zenkai uni-medium-r-normal--0-0-0-0-p-0-iso8859-4
ukai.ttf -misc-ar pl zenkai uni-medium-r-normal--0-0-0-0-p-0-iso8859-9
ukai.ttf -misc-ar pl zenkai uni-medium-r-normal--0-0-0-0-p-0-suneu-greek
uming.ttf -misc-ar pl shanheisun uni-medium-r-normal--0-0-0-0-p-0-ascii-0
uming.ttf -misc-ar pl shanheisun uni-medium-r-normal--0-0-0-0-p-0-big5-0
uming.ttf -misc-ar pl shanheisun uni-medium-r-normal--0-0-0-0-p-0-big5.eten-0
uming.ttf -misc-ar pl shanheisun uni-medium-r-normal--0-0-0-0-p-0-cns11643-1
uming.ttf -misc-ar pl shanheisun uni-medium-r-normal--0-0-0-0-p-0-cns11643-2
uming.ttf -misc-ar pl shanheisun uni-medium-r-normal--0-0-0-0-p-0-gb2312.1980-0
uming.ttf -misc-ar pl shanheisun uni-medium-r-normal--0-0-0-0-p-0-iso10646-1
uming.ttf -misc-ar pl shanheisun uni-medium-r-normal--0-0-0-0-p-0-iso8859-10
uming.ttf -misc-ar pl shanheisun uni-medium-r-normal--0-0-0-0-p-0-iso8859-13
uming.ttf -misc-ar pl shanheisun uni-medium-r-normal--0-0-0-0-p-0-iso8859-15
uming.ttf -misc-ar pl shanheisun uni-medium-r-normal--0-0-0-0-p-0-iso8859-16
uming.ttf -misc-ar pl shanheisun uni-medium-r-normal--0-0-0-0-p-0-iso8859-1
uming.ttf -misc-ar pl shanheisun uni-medium-r-normal--0-0-0-0-p-0-iso8859-2
uming.ttf -misc-ar pl shanheisun uni-medium-r-normal--0-0-0-0-p-0-iso8859-3
uming.ttf -misc-ar pl shanheisun uni-medium-r-normal--0-0-0-0-p-0-iso8859-4
uming.ttf -misc-ar pl shanheisun uni-medium-r-normal--0-0-0-0-p-0-iso8859-9
uming.ttf -misc-ar pl shanheisun uni-medium-r-normal--0-0-0-0-p-0-suneu-greek
[root@jfht TrueType]#
上面粗體標出的部分就是中文字體的名稱,比如
fangsong_gb2312、simhei、simsun、youyuan、stcaiyun。[root@jfht ~]# dot -Nfontname=fangsong_gb2312 -Tgif -ocn8_fangsong_gb2312.gif cn8.gv
[root@jfht ~]# dot -Nfontname=simhei -Tgif -ocn8_simhei.gif cn8.gv
[root@jfht ~]# dot -Nfontname=simsun -Tgif -ocn8_simsun.gif cn8.gv
[root@jfht ~]# dot -Nfontname=youyuan -Tgif -ocn8_youyuan.gif cn8.gv
[root@jfht ~]# dot -Nfontname=stcaiyun -Tgif -ocn8_stcaiyun.gif cn8.gv
[root@jfht ~]# ls -l cn8*
-rw-r--r-- 1 root root 922 12-12 15:58 cn8_fangsong_gb2312.gif
-rw-r--r-- 1 root root 902 12-12 15:38 cn8.gif
-rw-r--r-- 1 root root 35 12-12 15:35 cn8.gv
-rw-r--r-- 1 root root 926 12-12 16:00 cn8_simhei.gif
-rw-r--r-- 1 root root 866 12-12 16:01 cn8_simsun.gif
-rw-r--r-- 1 root root 974 12-12 16:03 cn8_stcaiyun.gif
-rw-r--r-- 1 root root 876 12-12 16:02 cn8_youyuan.gif
[root@jfht ~]#
問題思考
相關資料
【1】維基百科
Graphviz
【2】維基百科
DOT語言
【3】啄木鳥社區
Graphviz
【4】age的個人空間
用Dot繪圖
【5】海贼’blog
linux dot 繪圖
【6】daxuxu
graphviz dot簡介
【7】openstudy
英文版linux安裝中文字體