graphviz程序⽣成多种类型图表详解
简介
  ⼀幅图抵得上千⾔万语,这在描述复杂的计算机系统时尤为正确。当系统环境变得更加复杂时,⽤图将它们表⽰出来并记⼊⽂档就显得更加重要。例如,虚拟化技术有很多优点,但它们通常会让环境变得更加复杂和更难理解。Graphviz 是⼀个可以创建图表的灵活应⽤程序,可以轻松实现脚本化。本⽂将介绍 Graphviz 的安装、使⽤,以及其中使⽤ DOT 语⾔的基础知识,并提供了⼀些⽰例脚本。本⽂有助于您了解 Graphviz 应⽤程序的基础知识,以及如何编写⾃动创建图表的脚本。
  还有另外⼀个我觉得更加⽜逼的作⽤,在使⽤ markdown 时⽆法直接将图⽚直接粘贴上去,还得想办法将图⽚传到某⼀个中间服务再引⼊,有么有?⽽ graphviz 可以完美的解决这个问题,在 markdown 中也可以直接编写相应的代码⽣成相应的图表,使⽤起来相当 easy,越⽤越 happy。
  针对 graphviz 想要了解更加详细内容,请查看其对对应的
安装 graphviz
  Graphviz 是⼀个开源⼯具,可以运⾏在类似于 UNIX® 的⼤多数平台和 Microsoft® Windows® 之上。graphviz ⽀持 Windows、
Mac OS X、FreeBSD、Solaris、Linux 等多种系统
Ubuntu 或 Debian 下安装: 
sudo apt-get install graphviz
sudo yum install graphviz
Mac 下安装使⽤ brew 命令:
  我本⼈ mac 上安装的时候并没有装 ruby,故需要先安装 ruby 再进⾏安装 graphviz
ruby -e "$(curl -fsSL raw.githubusercontent/Homebrew/install/master/install)" < /dev/null 2> /dev/null ; brew install caskroom/cask/brew-cask 2> /dev/null brew install graphviz
Windows 下安装:
  由于本⼈⼀直使⽤的是 mac 和 linux,故此次试验并没有在 windows 上做验证,请查看进⾏安装。
DOT 语⾔基础
  安装 Graphviz 之后,就可以开始使⽤⼯具创建图表,第⼀步是创建⼀个 DOT ⽂件。该 DOT ⽂件是⼀个⽂本⽂件,描述了图表的组成元素以及它们之间的关系,以便该⼯具可以⽣成这些组成元素和它们之间的关系的图形化表⽰。
  1. 编写以dot为后缀的源代码⽂件hello.dot,内容如下:
  digraph {
  hello -> world;
  }
  2. 使⽤ dot 命令编译
  dot hello.dot -T png -o hello.png
  完整的命令格式为:
  <cmd><inputfile> -T <format> -o <outputfile>
  其中graphviz 的<cmd>有好⼏种,每种使⽤⽅法都完全相同,差别只在于渲染出来的图⽚效果不⼀
样。man中的简介是这样的 
<cmd>介绍
dot渲染的图具有明确⽅向性。
neato渲染的图缺乏⽅向性。
twopi渲染的图采⽤放射性布局。
circo渲染的图采⽤环型布局。
fdp渲染的图缺乏⽅向性。
sfdp渲染⼤型的图,图⽚缺乏⽅向性。
  可以透过man <cmd>取得进⼀步说明。但还是亲⾃⽤⽤⽐较容易理解。在本⽂中,凡没有说明的图,预设都是以dot渲染出来的。
  3. 查看效果
  运⾏ 2 中的命令后会⽣成对应的 png ⽂件,双击打开查看效果如下所⽰:
基础应⽤
1. 编写⼀个案例 dot ⽂件为:example1.dot
graph example1 {
Server1 -- Server2
Server2 -- Server3
Server3 -- Server1
}
  上述内容中共有三个节点(Server1、Server2 和 Server3)。--定义了节点之间的联系。这个使⽤ DO
T 语⾔的例⼦说明,Server2、Server2 连接到 Server3,⽽ Server3 连接到 Server1。
  创建 DOT ⽂件后,可以运⾏dot命令来⽣成图表,如 2 中所⽰。–T png将输出格式指定为 PNG,⽽–o example1.png指定必须将输出保存到⼀个名叫 example1.png 的⽂件中。
2. 从 DOT ⽂件⽣成图像
$ dot example1.dot –T png –o example1.png
⽣成的图表如下:
  第⼀个⽰例 (example1.dot) 描述了⼀个⽆向图,即⽤不带箭头的直线表⽰节点之间的联系的图。
  有向图不仅能表⽰节点之间的联系,⽽且能⽤箭头表⽰节点之间流动的⽅向。要创建有向图,可以在 DOT ⽂件的第⼀⾏上指
定digraph⽽⾮graph,并在节点之间使⽤->,⽽不是--。
3. ⽣成有向图,创建 dot ⽂件:example2.dot
digraph example2 {
Server1 -> Server2
Server2 -> Server3
Server3 -> Server1
}
使⽤ dot 命令⽣成对应的图表如下:
  你也可以轻松控制图中每个节点的形状、颜⾊和标签。具体⽅法是列出每个节点的名称,然后将选项放在名称后⾯的括号中。例如,代码⾏
  Server1[shape=box, label="Server1\nWebServer", fillcolor="#ABACBA", style=filled]
  定义了 Server1 节点应该是⼀个长⽅形,有⼀个 Server1\nWeb Server 标签(\n 表⽰⼀个新⾏),颜⾊设为⼗六进制 (hex) 颜⾊ #ABACBA,⽽且该颜⾊应该填充了节点。
4. ⽣成有颜⾊和形状的图表
 创建 dot ⽂件:example3.dot
digraph example3 {
Server1 -> Server2
Server2 -> Server3
Server3 -> Server1
Server1 [shape=box, label="Server1\nWeb Server", fillcolor="#ABACBA", style=filled]
Server2 [shape=triangle, label="Server2\nApp Server", fillcolor="#DDBCBC", style=filled]
Server3 [shape=circle, label="Server3\nDatabase Server", fillcolor="#FFAA22", style=filled]
}
对应的⽣成相应图表如下:
编写脚本创建 Graphviz 图表
  在了解了 Graphviz DOT 语⾔的基础知识之后,您可以开始创建脚本,从⽽动态创建⼀个 DOT ⽂件。这允许您动态创建始终准确且保持最新的图表。
  以下⽰例是⼀个 bash shell 脚本(hmc_to_dot.sh),它连接到 Hardware Management Console (HMC),收集托管服务器和逻辑分区(LPAR) 的相关信息,然后使⽤这些信息来创建 DOT 输出。
#!/bin/bash
HMC="$1"
serverlist=`ssh -q -o "BatchMode yes" $HMC lssyscfg -r sys -F "name" | sort`
echo "graph hmc_graph{"
for server in $serverlist; do
echo " \"$HMC\" -- \"$server\" "
lparlist=`ssh -q -o "BatchMode yes" $HMC lssyscfg -m $server -r lpar -F "name" | sort`
for lpar in $lparlist; do
echo "    \"$server\" -- \"$lpar\" "
done
done
echo "}"
  通过提供⼀个 HMC 服务器名称作为参数传递给脚本,便可运⾏此脚本。该脚本将传递的第⼀个参数设置为HMC 变量。设置serverlist 变量的⽅法是连接到 HMC 并获得该 HMC 控制的所有托管服务器的清单。在这些托管服务器上进⾏循环,⽽脚本将为每台托管服务器打印⼀⾏ "HMC" -- "server" ,这表明 Graphviz 在每台 HMC 与其托管服务器之间绘制了⼀条直线。此外针对每台托管服务器,脚本再次连接到HMC 并获得该托管系统上的 LPAR 清单,然后通过它们循环打印⼀⾏ "server" -- "LPAR"。这表明 Graphviz 在每台托管服务器与其 LPAR 之间都绘制了⼀条直线。(此脚本要求您在运⾏脚本的服务器与 HMC 之间设置 Secure Shell (SSH) 密钥⾝份验证)。
命令执⾏如下:
  ./hmc_to_dot.sh hmc_name
以上执⾏对应输出的内容如下所⽰:
graph hmc_graph{
"hmc01" -- "test520"
"test520" -- "lpar2"
"test520" -- "lpar3"
"hmc01" -- "test570"
"test570" -- "aixtest01"
"test570" -- "aixtest02"
"test570" -- "aixtest03"
"hmc01" -- "test510"
"test510" -- "lpar1"
}
你可以轻松从脚本⽣成图,具体⽅法是运⾏以下命令:
./hmc_to_dot.sh hmc_server_name | dot -T png -o hmc_graph.png
运⾏脚本,该脚本会动态创建 DOT 语⾔,然后将这些输出传递给 dot 命令,以便让它创建⼀个⽂件名为 hmc_graph.png 的图表。下图显⽰了创建的图表。
基础语法总结(供有识之⼠参考)
具体的图说明内容包含属性(attr)、节点(node)、边(edge)和⼦图(subgraph)说明。
节点属性如下:
Name Default Values
color black node shape color
comment any string (format-dependent)
distortion0.0node distortion for shape=polygon
fillcolor lightgrey/black node fill color
fixedsize false label text has no affect on node size
fontcolor black type face color
fontname Times-Roman font family
fontsize14point size of label
group name of node’s group
height.5height in inches
label node name any string
layer overlay range all, id or id:id
orientation0.0node rotation angle
peripheries shape-dependent number of node boundaries
regular false force polygon to be regular
shape ellipse node shape; see Section 2.1 and Appendix E
shapefile external EPSF or SVG custom shape file
sides4number of sides for shape=polygon
skew0.0skewing of node for shape=polygon
style graphics options, e.g. bold, dotted, filled; cf. Section 2.3
URL URL associated with node (format-dependent)
width.75width in inches
z0.0z coordinate for VRML output
边框属性:
Name Default Values
arrowhead normal style of arrowhead at head end
arrowsize  1.0scaling factor for arrowheads
arrowtail normal style of arrowhead at tail end
color black edge stroke color
comment any string (format-dependent)
constraint true use edge to affect node ranking
decorate if set, draws a line connecting labels with their edges
dir forward forward, back, both, or none
fontcolor black type face color
fontname Times-Roman font family
fontsize14point size of label
headlabel label placed near head of edge
headport n,ne,e,se,s,sw,w,nw
headURL URL attached to head label if output format is ismap
label edge label
labelangle-25.0angle in degrees which head or tail label is rotated off edge labeldistance  1.0scaling factor for distance of head or tail label from node labelfloat false lessen constraints on edge label placement labelfontcolor black type face color for head and tail labels
labelfontname Times-Roman font family for head and tail labels
labelfontsize14point size for head and tail labels
layer overlay range all, id or id:id
lhead name of cluster to use as head of edge
ltail name of cluster to use as tail of edge
minlen1minimum rank distance between head and tail samehead tag for head node; edge heads with the same tag are sametail merged onto the same port
style tag for tail node; edge tails with the same tag are merged onto the same port
taillabel graphics options, e.g. bold, dotted, filled; cf. Section 2.3
tailport label placed near tail of edge n,ne,e,se,s,sw,w,nw
tailURL URL attached to tail label if output format is ismapsvg的类型有几种
weight1integer cost of stretching an edge
图属性如下:
Name Default Values
bgcolor background color for drawing, plus initial fill color
center false center drawing on page
clusterrank local may be global or none
color black for clusters, outline color, and fill color if fillcolor not defined comment any string (format-dependent)
compound false allow edges between clusters
concentrate false enables edge concentrators
fillcolor black cluster fill color
fontcolor black type face color
fontname Times-Roman font family
fontpath list of directories to search for fonts
fontsize14point size of label
label any string
labeljust centered”l” and ”r” for left- and right-justified cluster labels, respectively labelloc top”t” and ”b” for top- and bottom-justified cluster labels, respectively layers id:id:id…
margin.5margin included in page, inches
mclimit  1.0scale factor for mincross iterations
nodesep.25separation between nodes, in inches.