绘图利器 — Graphviz 由AT&T实验室启动的开源工具包 — DOT 图形描述语言

来源:互联网 发布:mysql dba 前景 编辑:程序博客网 时间:2024/05/20 21:18

http://blog.csdn.net/zhangskd/article/details/8250470

概述

 

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

Graphviz (Graph Visualization Software) 是一个由AT&T实验室启动的开源工具包。DOT是一种图形描述语言,非常简单的,

Graphviz就是用来处理这种语言的工具。只需要简单了解一下DOT语言,就可以用Graphviz绘图了,它对程序员特别有用。

So in short, if you are a programmer, it is born for you。

 

无向图

[java] view plain copy
  1. graph graphname {  
  2.     a -- b -- c;  
  3.     b -- d;  
  4. }  

 

有向图

[java] view plain copy
  1. digraph graphname {  
  2.     a -> b -> c;  
  3.     b -> d;  
  4. }  

 

属性

[java] view plain copy
  1. //DOT语言中,可以对节点和边添加不同的属性。  
  2.   
  3. digraph graphname {  
  4.     //节点的属性,节点的名称  
  5.     a [lable = "Foo"];  
  6.   
  7.     //节点的属性,节点的形状  
  8.     b [shape = box];  
  9.   
  10.     //边的属性,边的颜色  
  11.     a -> b -> c [color = blue];  
  12.   
  13.     //边的属性,边的线状  
  14.     b -> d [style = dotted];  
  15. }  

 

基本图形

[java] view plain copy
  1. digraph G {  
  2. //把图片的尺寸设为4inch * 4inch  
  3. size = "4,4";  
  4. main [shape = box];  
  5.   
  6. //边的重要程度,默认是1  
  7. main->parse [weight = 8];  
  8. parse->execute;  
  9.   
  10. //点状线  
  11. main->init[style = dotted];  
  12. main->cleanup;  
  13.   
  14. //连接了两条线  
  15. execute->{make_string;printf}  
  16. init->make_string;  
  17.   
  18. //把边的默认颜色设为red  
  19. edge [color = red];  
  20. main->printf [sytle=bold, label = "100times"];  
  21.   
  22. //节点的名称  
  23. make_string [label = "make a\nstring"];  
  24.   
  25. //设置节点的默认属性  
  26. node [shape=box,style =filled,color=lightgrey];  
  27. execute->compare;  
  28. }  

 

 

多边形

[java] view plain copy
  1. digraph G{  
  2. a -> b -> c;  
  3. b -> d;  
  4.   
  5. /* 形状为多边形,边数为5,外框为3条,颜色为淡蓝,样式为填充 */  
  6. a [shape = polygon, sides = 5, peripheries = 3, color = lightblue, style = filled];  
  7.   
  8. /* 形状为多边形,边数为4,角的倾斜度为0.4,内容为hellow world*/  
  9. c [shape = polygon, sides = 4, skew = 0.4, label = "hello world"];  
  10.   
  11. /* 形状为倒三角,整体旋转30度 */  
  12. d [shape = invtriangle,orientation = 30];  
  13.   
  14. /* 形状为多边形,边数为4,扭曲度为0.7 */  
  15. e [shape = polygon, sides = 4, distortion = 0.7];  
  16. }  

 

 

数据结构

 

(1)复杂的标签

[java] view plain copy
  1. digraph structs {  
  2. /* 把节点的默认形状设为矩形record,默认的是圆角矩形Mrecord */  
  3. node [shape = record];  
  4.   
  5. struct1 [label = "left|middle|right"];  
  6. struct2 [label = "one|two"];  
  7. struct3 [label = "hello\nworld|{b|{c|d|e}|f}|g|h"];  
  8.   
  9. struct1 -> struct2;  
  10. struct1 -> struct3;  
  11. }  


 

[java] view plain copy
  1. graph picture {  
  2. //这幅图的名字  
  3. label = "I love you";  
  4.   
  5. //图名字的位置在bottom,也可以是t  
  6. labelloc = b;  
  7.   
  8. //图名字的位置在left,也可以是r  
  9. labeljust = l;  
  10.   
  11. edge[decorate = true];  
  12.   
  13. C -- D [label = "s1"];  
  14. C -- E [label = "s2"];  
  15. C -- F [label = "s3"];  
  16. D -- E [label = "s4"];  
  17. D -- F [label = "s5"];  
  18.   
  19. edge[decorate = false, labelfontcolor = blue, fontcolor = red];  
  20. C1 -- D1 [headlabel = "c1",taillabel = "d1",label = "c1 - d1"];  
  21. }  


 

 

(2)行列对齐

[java] view plain copy
  1. digraph html {  
  2. rankdir = LR;  
  3. {  
  4. node[shape = plaintext];  
  5. 1995 -> 1996 -> 1997 -> 1998 -> 1999 -> 2000 -> 2001;  
  6. }  
  7. {  
  8. node[shape = box, style = filled];  
  9. WAR3 -> Xhero -> Footman -> DOTA:  
  10. WAR3 -> Battleship;  
  11. }  
  12. {rank = same; 1996; WAR3;}  
  13. {rank = same; 1998; Xhero; Battleship;}  
  14. {rank = same; 1999; Footman;}  
  15. {rank = same; 2001; DOTA;}  
  16. }  

 

 

(3)二叉树

[java] view plain copy
  1. digraph G {  
  2. label = "Binary search tree";  
  3. node [shape = record];  
  4.   
  5. A [label = "<f0>|<f1>A|<f2>"];  
  6. B [label = "<f0>|<f1>B|<f2>"];  
  7. C [label = "<f0>|<f1>C|<f2>"];  
  8. D [label = "<f0>|<f1>D|<f2>"];  
  9. E [label = "<f0>|<f1>E|<f2>"];  
  10. F [label = "<f0>|<f1>F|<f2>"];  
  11. G [label = "<f0>|<f1>G|<f2>"];  
  12.   
  13. A:f0 -> B:f1;  
  14. A:f2 -> C:f1;  
  15. B:f0 -> D:f1;  
  16. B:f2 -> E:f1;  
  17. C:f0 -> F:f1;  
  18. C:f2 -> G:f1;  
  19. }  

 

 

(4)哈希表

[java] view plain copy
  1. digraph G{  
  2. nodesep = .05;  
  3. rankdir = LR;  
  4.   
  5. node [shape = record,width = .1,height = .1];  
  6. node0 [label = "<f0>|<f1>|<f2>|<f3>|<f4>|<f5>|<f6>|",height = 2.5];  
  7.   
  8. node [width = 1.5];  
  9. node1 [label = "{<n>n14|719|<p>}"];  
  10. node2 [label = "{<n>a1|805|<p>}"];  
  11. node3 [label = "{<n>i9|718|<p>}"];  
  12. node4 [label = "{<n>e5|989|<p>}"];  
  13. node5 [label = "{<n>t20|959|<p>}"];  
  14. node6 [label = "{<n>o15|794|<p>}"];  
  15. node7 [label = "{<n>s19|659|<p>}"];  
  16.   
  17. node0:f0 -> node1:n;  
  18. node0:f1 -> node2:n;  
  19. node0:f2 -> node3:n;  
  20. node0:f5 -> node4:n;  
  21. node0:f6 -> node5:n;  
  22. node2:p -> node6:n;  
  23. node4:p -> node7:n;  
  24. }  


 

 

流程图

[java] view plain copy
  1. digraph G{  
  2. subgraph cluster0 {  
  3. node [style = filled,color = white];  
  4. style = filled;  
  5. color = lightgrey;  
  6. a0 -> a1 -> a2 -> a3;  
  7. label = "process #1";  
  8. }  
  9.   
  10. subgraph cluster1 {  
  11. node [style = filled];  
  12. b0 -> b1 -> b2 -> b3;  
  13. label = "process #2";  
  14. color = blue;  
  15. }  
  16.   
  17. start -> a0;  
  18. start -> b0;  
  19. a1 -> b3;  
  20. b2 -> a3;  
  21. a3 -> a0;  
  22. a3 -> end;  
  23. b3 -> end;  
  24. start [shape = Mdiamond];  
  25. end [shape = Msquare];  
  26. }  


 

Reference

[1] http://zh.wikipedia.org/zh-cn/DOT语言,DOT语言简明介绍。

[2] http://zh.wikipedia.org/zh/Graphviz,简单背景知识。

[3] Graphviz中文指南。

 


阅读全文
0 0
原创粉丝点击