您的位置:首页 > 其它

Graphviz 入门

2016-07-13 15:37 330 查看
Graphviz - Graph Visualization Software

Graphviz 是一个绘图软件,类似微软的visio,但是他和visio也有很大的不同,他是用代码绘图的,使用一种名为dot的语言绘图,对于绘制复杂的流程图,类图等非常好用。 Graphviz 的自动布局功能,无需人为干预就可以做到“最小化连线交叉”。

DOT是纯文本图像描述语言,对于计算机和人来说,都很容易阅读。

我们的主要工作就是编写dot脚本,你只要关注图中各个点之间的关系就好了,你不需要考虑如何安排各个节点的位置,怎样布局能够使你所绘制的图看起来更美观一些。

官网下载:http://www.graphviz.org

图分为无向图和有向图两种。windows下安装后打开GVEdit.exe,编写代码,F5运行即可。

1. 无向图

graph graphname {
a -- b ;
a--c;
c -- d;
}


添加图的属性,设置背景色等。

graph graphname {
graph[bgcolor="cadetblue" label="图的标题" fontsize=24 fontcolor="green",fontname="FangSong"];//字体为仿宋,其他有kaiti

a -- b ;
a--c;
c -- d;
}


有向图

digraph graphname {
a -> b ;
a->c;
c -> d;
}


—-属性[编辑]

一张有属性的图

DOT语言中,可以对节点和边添加不同的属性。这些属性可以控制节点和边的显示样式,例如颜色,形状和线形。可以在语句和句尾的分号间放置一对方括号,并在其中中放置一个或多个属性-值对。多个属性可以被逗号和空格(, )分开。节点的属性被放置在只包含节点名称的表达式后。

graph graphname {
// label属性可以改变节点的显示名称
a [label="Foo"];
// 节点形状被改变了
b [shape=box];
// a-b边和b-c边有相同的属性
a -- b -- c [color=blue];
b -- d [style=dotted];
}


默认的顶点中的文字为定义顶点变量的名称,形状为椭圆。边的默认样式为黑色实线箭头。

支持的输出格式 非常多,

图片格式【至少】支持:png、jpg、gif、bmp、tiff、ico、svg

文档格式【至少】支持:pdf、ps/eps。具体见官网介绍。

DOT 语言并不复杂。从原则上讲,它只描述三种东西,分别是:节点(node)、线(edge)、图形(graph)。你可以通过 DOT 语言定义这三种东西的属性(比如:颜色、形状)。

注释 和C/C++/java注释一样,支持 // 和 /* */ ; 也支持 # .

常用的【属性名】包括如下:

color——颜色

style——样式

shape——形状

label——标题

bgcolor——颜色

fontname——字体名称(【不】影响节点和连线)

fontsize——字体大小(【不】影响节点和连线)

fontcolor——字体颜色(【不】影响节点和连线)

center——是否居中绘制

其他还有fontcolor,fontsize

color有两种形式: color = red ;color = “#0xff00ff”

label 标题支持 \n换行。label = “一切有为法,\n皆梦幻泡影,\n如露亦如电,\n应作如是观。”

shape 也有很多种,常见的是:

box, polygon, ellipse, oval, circle, point, egg, triangle, plaintext, diamond, trapezium,parallelogram, house, pentagon, hexagon, septagon, octagon, doublecircle, doubleoctagon, tripleoctagon…

graph graphname {//属性 这些属性可以控制节点和边的显示样式,例如颜色,形状和线形。
// label属性可以改变节点的显示名称
a [label="Foo",color = red];
// 节点形状被改变了
b [shape=box,color = blue];
// a-b边和b-c边有相同的属性
a -- b -- c [color=blue];
b -- d [style=dotted];
digraph g {
node [shape=plaintext]
A1 -> B1
A2 -> B2
A3 -> B3

A1 -> A2 [label=f]
A2 -> A3 [label=g]
B2 -> B3 [label="g'"]
B1 -> B3 [label="(g o f)'" tailport=s headport=s]

{ rank=same; A1 A2 A3 }
{ rank=same; B1 B2 B3 }
}

graph graphname {
rankdir=LR;  //Rank Direction Left to Right

a -- b -- c;
b -- d;
}

digraph G {
a -> c;
a -> b;
b -> c[constraint=false];
}
digraph test{
graph[bgcolor="cadetblue" label="图的标题" fontsize=24 fontcolor="green",fontname="FangSong"];//kaiti,

start[color = white,fontcolor=red,fontsize=24,style=filled, fillcolor="#1245f0",shape=rounded];
ini[fontname="FangSong",label="test1\ntest2\n水电费",color = white,fontcolor=red,fontsize=24,style=filled, fillcolor=green,shape=box];
api[color = red,fontcolor=black,fontsize=24,style=filled, fillcolor=yellow,shape=oval];//oval  ellipse
over;a[shape=box];b[shape=box];
start->ini[color = green];
ini->api;
api->a[label="yes"];
api->b[label="no"];
a->over;
b->over;
A -> {B C};
rank = same; A; B; C;
a1 -> b1 [dir=both color="red:blue"]
c -> d [dir=none color="green:red;0.25:blue"]
//dir=forward;back
}

digraph hierarchy {
nodesep=1.0 // increases the separation between nodes

node [color=Red,fontname=Courier,shape=box] //All nodes will this shape and colour
edge [color=Blue, style=dashed] //All the lines look like this

Headteacher->{Deputy1 Deputy2 BusinessManager}
Deputy1->{Teacher1 Teacher2}
BusinessManager->ITManager
//  {rank=same;ITManager Teacher1 Teacher2}  // Put them on the same level
}
digraph summary{
start [label="Start with a Node"]
next [label="Choose your shape", shape=box]
warning [label="Don't go overboard", color=Blue, fontcolor=Red,fontsize=24,style=filled, fillcolor=green,shape=octagon]
end [label="Draw your graph!", shape=box, style=filled, fillcolor=yellow]

start->next
start->warning
next->end [label="Getting Better...", fontcolor=darkblue]
}

digraph node_attr
{
shape1 [shape=box, label="编程随想注:\n矩形节点"];
shape2 [shape=circle, label="编程随想注:\n圆形节点"];
shape3 [shape=ellipse, label="编程随想注:\n椭圆形节点"];
shape4 [shape=polygon, sides=4, skew=0.4, label="编程随想注:\n平行四边形节点"];
shape5 [shape=none, label="编程随想注:\n无边框节点"];
shape1 -> shape2 -> shape3 -> shape4 -> shape5

color1 [color="blue", label="编程随想注:\n蓝色边框"]
color2 [color="green", style=filled, label="编程随想注:\n绿色填充"]
color3 [color="#ff0000", style=filled, fillcolor="yellow", label="编程随想注:\n红色边框+黄色填充"]
color4 [color="#0000FF" style=filled, fillcolor="green:red", label="编程随想注:\n蓝色边框+从绿色到红色渐变填充"]
/* 上面两个节点采用 HTML 的颜色语法,dot 支持 这种语法 */
color1 -> color2 -> color3 -> color4

text1 [shape=box, fontsize=12, label="编程随想注:\n小字体"]
text2 [shape=box, fontsize=24, label="编程随想注:\n大字体"]
text3 [shape=box, fontcolor="blue", label="编程随想注:\n蓝色文字"]
text4 [shape=box, label=<编程随想注:<br/><b>粗体</b> <i>斜体</i> <u>下划线</u>>]
// 注意:text4 使用 HTML 风格的 label,无需引号,但必须用尖括号
text1 -> text2 -> text3 -> text4
}

digraph edge_attr
{
style0[label="编程随想注:\n以下是样式的示例"];
style1[label=""] style2[label=""] style3[label=""] style4[label=""];

style0 -> style1 [style=solid, label="实线"];
style1 -> style2 [style=bold, label="粗线"];
style2 -> style3 [style=dashed, label="短划线"];
style3 -> style4 [style=dotted, label="虚线"];

arrow0[label="编程随想注:\n以下是箭头的示例"];
arrow1[label=""] arrow2[label=""] arrow3[label=""] arrow4[label=""] arrow5[label=""] arrow6[label=""];
arrow0 -> arrow1 [dir=both, label="双向箭头"];
arrow1 -> arrow2 [arrowsize=2.0, label="大箭头"];
arrow2 -> arrow3 [arrowhead="open", label="带倒钩的箭头"];
arrow3 -> arrow4 [arrowhead="halfopen", label="单边倒钩"];
arrow4 -> arrow5 [arrowhead="ediamond", label="菱形箭头"];
arrow5 -> arrow6 [arrowhead="dot", label="圆形箭头"];

color0[label="编程随想注:\n以下是颜色的示例"];
color1[label=""] color2[label=""] color3[label=""];
color0 -> color1 [color="blue", label="蓝色"];
color1 -> color2 [color="red:blue", label="双色"];
color2 -> color3 [color="green:red;0.4:blue", label="颜色分段"];
}
digraph G{//{}中的注意空格
{ a b c} -> { d e f }
}


中文乱码问题需要把文件保存为utf-8格式,然后为中文设置字体,fontname = “fangsong”….

rank:

rank在此是等级,层的意思。

每个线段的头端,都会比尾端多出一个等级。

比如 a->b ; b就比a多一个等级。

digraph demo{

a -> b -> c -> d [label = “rank增加”];

b -> { e f } [label = “rank增加”];

f -> a [label = “不影响rank”];

}

rank 的指定是「先说先赢」的。也可指定rank属性:

{rank=same;a b c} 。

Graphviz 博大精深,这里只是非常简单的介绍下。

参考:

http://blog.csdn.net/xiajian2010/article/details/23748557
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: