本文链接:http://codingstandards.iteye.com/blog/840055 (转载请注明出处)
用途说明
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
输入文件是
其中-T
-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 (p_w_picpathmap files for httpd servers for each node or edge that has a non-null "href" attribute.),
-Tcmapx (client-side p_w_picpathmap 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安装中文字体