graphviz笔记

来源:互联网 发布:linux端口占用查看命令 编辑:程序博客网 时间:2024/06/05 18:02

graphviz笔记

1.简介
一个高效而简洁的绘图工具graphviz。graphviz是贝尔实验室开发的一个开源的工具包,它使用一个特定的DSL(领域特定语言): dot作为脚本语言,然后使用布局引擎来解析此脚本,并完成自动布局。graphviz提供丰富的导出格式,如常用的图片格式,SVG,PDF格式等。

graphviz中包含了众多的布局器:

dot 默认布局方式,主要用于有向图neato 基于spring-model(又称force-based)算法twopi 径向布局circo 圆环布局fdp 用于无向图

2.三要素
图,顶点和边

每个元素都可以具有各自的属性,用来定义字体,样式,颜色,形状等。

3.绘制流程

定义一个图,并向图中添加需要的顶点和边为顶点和边添加样式使用布局引擎进行绘制

4.简单实例
使用dot布局,默认的顶点中的文字为定义顶点变量的名称,形状为椭圆。边的默认样式为黑色实线箭头

digraph abc{    a;    b;    c;    a -> b;    b -> c;    c -> a;}

这里写图片描述

定义顶点和边的样式

node [shape="record"]; /* 矩形框 */edge [style="dashed"]; /* 虚线 */digraph abc{    node [shape="record"];    edge [style="dashed"];    a;    b;    c;    a -> b;    b -> c;    c -> a;}

这里写图片描述

修改顶点和边样式

digraph abc{    node [shape="record"];    edge [style="dashed"];    a [style="filled", color="red", fillcolor="blue"];    b;    c;    a -> b;    b -> c;    c -> a [color="green"];}

这里写图片描述

增加子图

digraph abc{  node [shape="record"];  edge [style="dashed"];  a [style="filled", color="red", fillcolor="blue"];  b;    subgraph cluster_cd{      label="c and d";      bgcolor="mintcream";      c;      d;    }  a -> b;  b -> d;  c -> d [color="green"];}

这里写图片描述

顶点形状:

digraph G {      a -> b -> c;      b -> d;      a [shape=polygon,sides=5,peripheries=3,color=lightblue,style=filled];      c [shape=polygon,sides=4,skew=.4,label="hello world"]      d [shape=invtriangle];      e [shape=polygon,sides=4,distortion=.7];}

这里写图片描述

例2:

digraph G {      main -> parse -> execute;      main -> init;      main -> cleanup;      execute -> make_string;      execute -> printf      init -> make_string;      main -> printf;      execute -> compare;}

这里写图片描述

digraph G {    size ="4,4";    main [shape=box];     main -> parse [weight=8];    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"];    make_string [label="make a\nstring"];    node [shape=box,style=filled,color=".7 .3 1.0"];    execute -> compare;}

这里写图片描述

Lables 标签

绘制多表格

digraph html {        abc [shape=none, margin=0, label=<        <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">        <TR><TD ROWSPAN="3"><FONT COLOR="red">hello</FONT><BR/>world</TD>        <TD COLSPAN="3">b</TD>        <TD ROWSPAN="3" BGCOLOR="lightgrey">g</TD>        <TD ROWSPAN="3">h</TD>        </TR>        <TR><TD>c</TD>        <TD PORT="here">d</TD>        <TD>e</TD>        </TR>        <TR><TD COLSPAN="3">f</TD>        </TR>        </TABLE>>];}

这里写图片描述