Graphviz

DOT Language 主要有三种对象: 图 graph, 边 edge, 节点 node
其中graph 又包括有向图(directed graph) digraph 和 无向图graph
在图内,{} 大括号中定义的内容(节点和边)叫做 subgraph

DotLanguage.jpg
digraph dotLanguage{ 
  "DOT Language" -> {"graph" , "edge", "node"};
  "graph" -> {"digraph", "graph "};
} 
  • 当ID(Name/名称)含有关键字或空白字符时要使用双引号括起来
  • 结尾的分号 ; 可要可不要

Graphviz

软件下载

  • 将包含 Graphviz 可执行文件的目录 D:\Graphviz2.38\bin 添加到环境变量 Path

  • 选择 Layout 预览结果

    layout.PNG

  • 在命令行窗口执行命令生成 jpg 图片: dot -Tjpg DotLanguage.gv -o DotLanguage.jpg


小示例

smallGraph.jpg
digraph G {
  main -> init -> make_string;
  main -> {parse, cleanup};
  main -> printf;
  parse -> execute -> {make_string, compare, printf};
}
  • 节点名 创建节点
  • -> 创建有向边,-- 创建无向边

属性

attr.jpg
digraph G {
  /* 一个示例的图 */
  /* 节点和边的属性定义在方括号中 */

  size = "4, 4";  # 设置图的大小(4, 4 inches) 太大的尺寸会自动调整成合适的大小
  main -> parse [weight=8]; # 设置边的权重(默认为1),增加权重使边变值并且位于正中间
  main [shapde=box];
  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;
}
  • 属性直接写出来 --> 为图设置属性
  • 节点和边的属性定义在方括号中
  • 同时指向多个节点可以用 {} 大括号表示,用 分号 分隔
  • 属性用 逗号 分隔,
  • 关键字 node 为后面创建的节点设置默认属性,edge 为后面的边设置默认属性

常用属性

dot_01.jpg
dot_02.jpg
dot_03.jpg

Node 属性

  • shape = box | circle | record | plaintext | point

node_shapes.jpg
digraph NodeShape {
  node_shape [label="Node Shape"]
  polygon_base [label="polygon-base"]
  record_base [label="record-base"]

  node_shape -> {polygon_base; record_base}
  polygon_base -> "all others node shapes"
  record_base -> {"record"; "Mrecord"}
} 
  • Node Shapes 分为两大类:polygon-base 和 record-base;
  • 除了 record 和 Mrecord 外,其他都是 polygon ,可以设置 polygon 属性,比如 sides (circle, ellipse 除外)
  • regular=true 强制设置形状为矩形
  • 节点的大小默认是大于里面文字所需的空间,fixedsize=true 强制 widthheight 属性

polygon-base 多边形

peripheries & orientation
peripheries_orientation.jpg
digraph G {
  a[shape=polygon, sides=5, peripheries=3,color=lightblue, style=filled, label="peripheries=3", width=1, height=2]
  b[shape=polygon, orientation=45, label="orientation=45°", width=1, height=2]
}

distortion & skew
digraph G {
  origin [shape=polygon]
  a[shape=polygon, distortion=.1, label="polygon\ndistortion: .1"]
  b[shape=polygon, distortion=.9, label="polygon\ndistortion: .9"]
  c[shape=polygon, distortion=-.9, label="polygon\ndistortion: -.9"]
  s1[shape=polygon, skew=.1, label="polygon\nskew=.1"]
  s2[shape=polygon, skew=.9, label="polygon\nskew=.9"]
  s3[shape=polygon, skew=-.9, label="polygon\nskew=-.9"]
  
  a -> b -> c
  s1 -> s2 -> s3
}
polygon.jpg
  • distortion : 正数从上向下缩小(梯形)
  • skew: 倾斜程度,正数顶部向右移动(平行四边形)

record-base

record 和 Mrecord 的区别是 Mrecord 时圆角的;

record_example.jpg
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"];
}
  • record 里面的结构由属性 label 定义
  • record 用于表示一系列可嵌套的项(field)
  • 项由 <name>"field string label" 组成
  • 项之间用 | 分隔
  • 嵌套项用 {} 表示,嵌套表示为平行/垂直相减的项

在节点中,label 默认时节点的名称

record.jpg

field.jpg
digraph Field {
    main [shape=record, label="{field | {\<name\>\ optional | field\ string\ label}}"]
}
  • 项由 <name>"field string label" 组成

由于项定在 label 属性中,所以在创建项时使用 " 双引号时会和 label ,因此不能直接使用双引号,而是 转义序列 空格,<>{}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • <一> graphviz dot 使用步骤 安装graphvizbrew install graphviz (ma...
    元亨利贞o阅读 25,308评论 9 24
  • graphviz 画流程图 brew install graphvizvi lz.dot dot -Tjpg lz...
    神刀阅读 8,998评论 0 0
  • brew install graphvizvi lz.dot dot -Tjpg lz3.dot -o lz3.j...
    神刀阅读 7,501评论 0 0
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,771评论 19 139
  • 技能的意义是建立在这样的关系上的:相对于增加个人利益,要更重视向客户交付价值。罗胖曰:我们来翻译一下作者讲的道理—...
    过河卒阅读 1,846评论 0 2