graphviz dot java,graphviz dot语言学习笔记

graphviz dot 使用步骤

安装graphvizbrew install graphviz (mac os x系统)

创建文本文件并命名为*.dot, 编写dot脚本

在第二部创建的文件中编写脚本

编译脚本, 输出图片

编译命令: dot -Tpng *.dot -o *.png

记得把 * 换成具体的文件名, 这样你就成功的用脚本渲染出你要绘制的图片啦

graphviz dot语言相关知识

注释

使用双斜杠注释

有向图

使用digraph定义有向图

使用->表述节点之间的关系

如:

```dot

digraph g {

a->b;

b->c;

c->a;

}

```

效果如下:

e44885a777f0

a.png

无向图

使用graph定义无向图

使用 -- 表述节点之间的关系

节点之间的关系

表述节点直接的关系如下:

有向图: a -> b, a节点指向b节点

无像图: a -- b, a节点与b节点连通

定义节点属性

定义属性, 格式为: node[attribute1=value1, attribute2=value2]

如:

```

//定义a节点为长方形, 节点显示的文本为"Hello world"样式为填充, 填充颜色为#ABACBA

a[shape=box,label="Hello world",style=filled,fillcolor="#ABACBA"];

```

各种形状请参考下面第7条

定义关系属性(即连接两个节点之间的线的样式)

定义节点的形状

栗子:

```dot

//定义节点属性

digraph g {

//==========定义节点关系============

a->b;

b->c;

c->a;

c->d->e->f;

d->g;

e->h;

//==========定义节点属性============

//定义a节点为长方形, 样式为填充, 填充颜色为#ABACBA

a[shape=box,label="Server1\nWebServer",fillcolor="#ABACBA",style=filled];

//定义b为5边形, 标签为"bb", 样式为填充, 填充色为red

b[shape=polygon,sides=5,label="bb",style=filled,fillcolor=red];

//c, 默认为椭圆

d[shape=circle]; //园

e[shape=triangle]; //三角形

f[shape=polygon, sides=4, skew=0.5]; //平行四边形

g[shape=polygon, distortion=0.5]; //梯形, 上边长

h[shape=polygon, distortion=-.5]; //梯形, 下边长

}

```

效果如下:

e44885a777f0

a.png

哈希表(hash table)

```

digraph g {

nodesep = .5;

rankdir = LR; //指定绘图的方向 (LR从左到右绘制)

//定义竖直节点

node[shape=record, width=.1, height=.1];

node0[label=" | | | | | | ", height=2.5]; //我是一个属性, 我有7个属性

//定义横向节点

node[width=1.5];

node1[label="{ a13 | 111 |

}"]; //我也是一个节点, 定义了3个属性

node2[label="{ hello | 2387 |

}"];

node3[label="{ g23 | 344 |

}"];

node4[label="{ k535 | 246 |

}"];

node5[label="{ h25 | 13 |

}"];

node6[label="{ dj | 04 |

}"];

node7[label="{ sbd | 0x543 |

}"];

//建立节点之间的联系

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;

}

```

效果如下:

![a.png](http://upload-images.jianshu.io/upload_images/1642441-4a6b6edecad8f422.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

更多形状

* 请参考[官方文档](http://www.graphviz.org/Documentation.php)

* 或者下面

e44885a777f0

271314316446130.jpg

定义结构

一个简单的数据结构示例

digraph g {

node[shape=record,height=.1]; //定义了下面的node样式

node0[label=" | A| "]; //我是一个node, 我有三个属性, 第二个属性名字为A, 其他两个为空

node1[label=" | B| "];

node2[label=" | C| "];

node3[label=" | D| "];

node4[label=" | E| "];

node5[label=" | F| "];

node6[label=" | H| "];

node7[label=" | I| "];

node8[label=" | J| "];

node9[label=" | K| "];

"node0":f2 -> "node1": f1; //node0的第三个属性连到node1的第二个属性

"node1":f0 -> "node2": f1;

"node1":f1 -> "node3": f2;

"node3":f0 -> "node4": f0;

"node3":f1 -> "node5": f1;

"node3":f2 -> "node6": f2;

"node6":f1 -> "node7": f1;

"node7":f1 -> "node8": f0;

"node2":f2 -> "node9": f1;

}

效果如下:

e44885a777f0

a.png

使用node定义结构, node[shape=record]

使用subgraph定义子图

digraph g {

//定义一个子图, subgraph定义子图

subgraph cluster0 {

node[style=filled, color=white]; //定义子图中的节点的样式

style=filled; //定义子图的样式

color=red; //定义子图的填充色

a0->a1->a2->a3; //定义节点, 及节点之间的关系

label="process #1"; //定义子图的标签

}

//又定义一个子图

subgraph cluster1 {

node[style=filled, color=white];

style=filled;

color=blue; //定义子图的填充色

b0->b1->b2->b3; //定义节点及其关系

label="process #2";

labelColor=white;

}

//定义子图之间的关系

start->a0;

start->b0;

a1->b3;

b2->a3;

a3->end;

b3->end;

}

效果如下:

e44885a777f0

demo6.png

更多结构请参考官方文档

常用颜色如下:

e44885a777f0

271313527842101.jpg

定义关系的样式(节点之间的连线的样式)

有向图

digraph g {

//edge[style=dashed]; //定义边的样式, 虚线

node[peripheries=2, style=filled, color="#eecc80"];

a->b [color=red, style=dashed]; //定义边的颜色, 红色 (b和方括号之间必须有空格)

b->c; //箭头, 三角形; 箭尾, 菱形

b->d [arrowhead=box]; //箭头, 长方形

b->e [dir=none]; //没有箭头

d->f [dir=both]; //双向箭头

f->h [label=go]; //定义edge的标签

f->k [arrowhead=diamond]; //更改箭头形状 (更多箭头形状请参考官方文档: http://www.graphviz.org/content/arrow-shapes)

k->y [headlabel="哈哈", taillabel="洗洗"];

}

效果如下:

e44885a777f0

a.png

无向图

graph g {

edge[style=dashed]; //定义边的样式, 虚线

a -- b [color=red]; //定义边的颜色, 红色 (b和方括号之间必须有空格)

}

效果如下:

e44885a777f0

demo8.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值