前几天去杨瑞那儿玩,他向我介绍了一个叫graphviz的工具,回来之后试用了一下,觉得真的是很好很强大。下面简单的介绍一下这个工具。
graphviz是贝尔实验室设计的一个开源的画图工具,它的强大主要体现在“所思即所得"(WYTIWYG,what you think is what you get),这是和office的“所见即所得“(WYSIWYG,what you see is what you get)完全不同的一种方式。它的输入是一个用dot语言 编写的绘图脚本,通过对输入脚本的解析,分析出其中的点,边以及子图,然后根据属性进行绘制。用graphviz来绘图的时候,你的主要工作就是编写dot脚本,你只要关注图中各个点之间的关系就好了,你不需要考虑如何安排各个节点的位置,怎样布局能够使你所绘制的图看起来更美观一些。呵呵,这也许就是”所思即所得“的魅力吧。
下面可以看看来自官方文档的一些例子。
$ cat 1.dot
digraph G {
main -> parse -> execute;
main -> init;
main -> cleanup;
execute -> make_string;
execute -> printf
init -> make_string;
main -> printf;
execute -> compare;
}
$ dot -Tgif 1.dot -o 1.gif
效果如下图:
$ cat 2.dot
digraph structs {
node [shape=record];
struct1 [shape=record,label="<f0> left|<f1> mid/ dle|<f2> right"];
struct2 [shape=record,label="<f0> one|<f1> two"];
struct3 [shape=record,label="hello/nworld |{ b |{c|<here> d|e}| f}| g | h"];
struct1 -> struct2;
struct1 -> struct3;
}
$ dot -Tgif 2.dot -o 2.gif
效果如下图:
$ cat 3.dot
digraph g {
node [shape = record,height=.1];
node0[label = "<f0> |<f1> G|<f2> "];
node1[label = "<f0> |<f1> E|<f2> "];
node2[label = "<f0> |<f1> B|<f2> "];
node3[label = "<f0> |<f1> F|<f2> "];
node4[label = "<f0> |<f1> R|<f2> "];
node5[label = "<f0> |<f1> H|<f2> "];
node6[label = "<f0> |<f1> Y|<f2> "];
node7[label = "<f0> |<f1> A|<f2> "];
node8[label = "<f0> |<f1> C|<f2> "];
"node0":f2 -> "node4":f1;
"node0":f0 -> "node1":f1;
"node1":f0 -> "node2":f1;
"node1":f2 -> "node3":f1;
"node2":f2 -> "node8":f1;
"node2":f0 -> "node7":f1;
"node4":f2 -> "node6":f1;
"node4":f0 -> "node5":f1;
}
$ dot -Tgif 3.dot -o 3.gif
效果如下图:
再看一个稍微丰富一点吧
$ cat 4.dot
digraph G {
node [peripheries=2 style=filled color="#eecc80"]
edge [color="sienna" fontcolor="green"]
main -> parse -> execute;
main -> init [arrowhead = box];
main -> cleanupi -> main;
make_string[label = once shape=parallelogram style=filled ]
execute -> make_string[label=go style=dashed arrowtail=diamond];
execute -> printf [shape=box];
init -> make_string;
main -> printf[dir=none];
execute -> compare[dir=both];
}
$ dot -Tgif 4.dot -o 4.gif
效果如下图:
呵呵,效果还不错吧。改天抽个时间再写个简单的教程。