graphviz dot语言学习笔记

转载自:

https://www.jianshu.com/p/e44885a777f0

 

<一> graphviz dot 使用步骤

  1. 安装graphvizbrew install graphviz (mac os x系统)
  2. 创建文本文件并命名为*.dot, 编写dot脚本
  3. 在第二部创建的文件中编写脚本
  4. 编译脚本, 输出图片
    • 编译命令: dot -Tpng *.dot -o *.png
    • 记得把 * 换成具体的文件名, 这样你就成功的用脚本渲染出你要绘制的图片啦

<二> graphviz dot语言相关知识

  1. 注释
  • 使用双斜杠注释
  1. 有向图
  2. 使用digraph定义有向图
  3. 使用->表述节点之间的关系
  4. 如:
```dot
digraph g {
    a->b;
    b->c;
    c->a;
}
```
效果如下: 

a.png

  1. 无向图
  2. 使用graph定义无向图
  3. 使用 -- 表述节点之间的关系
  4. 节点之间的关系
  • 表述节点直接的关系如下:
    1. 有向图: a -> b, a节点指向b节点
    2. 无像图: a -- b, a节点与b节点连通
  1. 定义节点属性
  2. 定义属性, 格式为: node[attribute1=value1, attribute2=value2]
  3. 如:
```
//定义a节点为长方形, 节点显示的文本为"Hello world"样式为填充, 填充颜色为#ABACBA
a[shape=box,label="Hello world",style=filled,fillcolor="#ABACBA"];
```
  1. 各种形状请参考下面第7条
  2. 定义关系属性(即连接两个节点之间的线的样式)
  3. 定义节点的形状
  4. 栗子:
```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]; //梯形, 下边长
  }
```
 效果如下: 

a.png

  1. 哈希表(hash table)
```
digraph g {
    nodesep = .5;
    rankdir = LR;    //指定绘图的方向 (LR从左到右绘制)

    //定义竖直节点
    node[shape=record, width=.1, height=.1];
    node0[label="<f0> |<f1> |<f2> |<f3> |<f4> |<f5> |<f6> ", height=2.5]; //我是一个属性, 我有7个属性

    //定义横向节点
    node[width=1.5];
    node1[label="{<n> a13 | 111 | <p>}"]; //我也是一个节点, 定义了3个属性
    node2[label="{<n> hello | 2387 | <p>}"];
    node3[label="{<n> g23 | 344 | <p>}"];
    node4[label="{<n> k535 | 246 | <p>}"];
    node5[label="{<n> h25 | 13 | <p>}"];
    node6[label="{<n> dj | 04 | <p>}"];
    node7[label="{<n> sbd | 0x543 | <p>}"];

    //建立节点之间的联系
    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)
  1. 更多形状
* 请参考[官方文档](http://www.graphviz.org/Documentation.php)  
* 或者下面  

271314316446130.jpg

  1. 定义结构
    1. 一个简单的数据结构示例
    digraph g {
      node[shape=record,height=.1]; //定义了下面的node样式
    
      node0[label="<f0> |<f1> A|<f2> "]; //我是一个node, 我有三个属性, 第二个属性名字为A, 其他两个为空
      node1[label="<f0> |<f1> B|<f2> "];
      node2[label="<f0> |<f1> C|<f2> "];
      node3[label="<f0> |<f1> D|<f2> "];
      node4[label="<f0> |<f1> E|<f2> "];
      node5[label="<f0> |<f1> F|<f2> "];
      node6[label="<f0> |<f1> H|<f2> "];
      node7[label="<f0> |<f1> I|<f2> "];
      node8[label="<f0> |<f1> J|<f2> "];
      node9[label="<f0> |<f1> K|<f2> "];
    
      "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;
    }
    

    效果如下:

     

    a.png

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

    2. 使用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;
      }
      

      效果如下:

      demo6.png

    3. 更多结构请参考官方文档

    4. 常用颜色如下:

       

      271313527842101.jpg

  2. 定义关系的样式(节点之间的连线的样式)
    1. 有向图

      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="洗洗"];
      }
      

      效果如下:

      a.png

       

    2. 无向图

       graph g {
        edge[style=dashed]; //定义边的样式, 虚线
        a -- b [color=red]; //定义边的颜色, 红色 (b和方括号之间必须有空格)
      }
      

      效果如下:

      demo8.png

references:
graphviz官网
graphviz文档地址
dot官方文档
使用graphviz绘制流程图

小礼物走一走,来简书关注我

赞赏支持



作者:元亨利贞o
链接:https://www.jianshu.com/p/e44885a777f0
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值