Graphviz由一种被称为DOT语言的图形描述语言与一组可以生成和/或处理DOT文件的工具组成。通过DOT语言来编写绘图脚本。

一、看个简单例子:


graph G{
a--b;
a--d;
}
digraph G{
a->b;
a->d;
}

105628791.png105629946.png

digraph是有向图,graph是无向图,要注意,->用在有向图中,--用在无向图中表示一条边,不能混用。除去有向与无向的区别外,剩下都一样了。

二、简单属性设置:

可以给node设置属性,也可以给边设置属性。先来讲讲怎么设置边的属性,在每条边后面的双括号里设置边的属性。也可以在用edge设置边的默认值。

而给node设置属性就必须给每个node单独的设置一个属性,node表示点的默认值。

node的默认参数是shape=ellipse, width=.75, height=.5 并且节点标识的名字就是节点本身。

通过shape可以设置选择不同的图形。


digraph G{
    size = "4, 4";//图片大小
    main[shape=box];/*形状*/
    main->parse;
    parse->execute;
    main->init[style = dotted];//虚线
    main->cleanup;
    execute->{make_string; printf}//连接两个
    init->make_string;
    edge[color = red]; // 连接线的颜色
    main->printf[style=bold, label="100 times"];//线的 label
    make_string[label = "make a\nstring"]// \n, 这个node的label,注意和上一行的区别
    node[shape = box, style = filled, color = ".7.3 1.0"];//一个node的属性
    execute->compare;
}

105023951.png


三、复杂属性设置

点的shape 除了record 和Mrecord 这两种之外,其他的形状都是多边形,而我们可以对多边形进行一下属性上的设置,shape = polygon。Sides 用于设置它的边数,peripheries 用于设置多边形的外框的层数,regular = true 可以让你的多边形是一个规则的多边形,orientation =*,可以让你的多边形旋转一个角度,如orientation = 15 就是转了15 度。Skew 后面跟一个(-1.0~1.0)的小数,能让你的图形斜切一个角度,distortion 是让你的图形产生透视效果。


digraph G{
    size = "4, 4"
    a->b->c;
    b->d;
                                                                                                                                                      
    a[shape = polygon, sides = 5, peripheries=3, color = lightblue, style = filled];
    //我的形状是多边形,有五条边,3条边框, 颜色的淡蓝色, 样式为填充
    c[shape = polygon, sides = 4, skew= 0.4, lable = "hello world"];
    //我的形状是4变形, 角的弯曲度0.4, 里面的内容为"hello world"
    d[shape = invtriange];
    //我是三角形
    e[shape = polygon, side = 4, distortion = .7];
    //我是梯形啊
}

110526456.png


官方文档:http://www.graphviz.org/pdf/dotguide.pdf