dot language 学习笔记

 

dot language 学习笔记

 

dot language 学习笔记

1 dot 语言简介

DOT语言是一种文本图形描述语言。文件扩展名通常为.gv或.dot 。它可以导出的格式有:GIF、PNG、SVG、PDF及PostScript。

2 基本语法

 

2.1 常用图形

  • box、polygon、ellipse、circle、point、triangle、invtriangle、plaintext、diamond
digraph shapes {
  "Box"[shape=box]
  "Polygon"[shape=polygon,sides=7] /* 定义了一个7边行,sides属性定义多边形边的数量 */
  "Ellipse"[shape=ellipse]
  "Circle"[shape=circle]
  "Point"[shape=point]
  "triangle"[shape=triangle]
  "invtriangle"[shape=invtriangle]
  "plaintext"[shape=plaintext]
  "diamond"[shape=diamond]
}

./resources/svg/shapes.svg

想了解更多内容,那就点击我

2.2 常用线条

  • 无向线条、又向线条、双向线条
digraph lines {
  "Undirected1"->"Undirected2"[arrowhead=none]
  "Undirected1"->"Directed1"
  "Directed1"->"Directed2"[dir=both]
}

./resources/svg/lines.svg

  • 实线、虚线、点线、加粗、透明
digraph lines2 {
  rankdir=LR
  "solid"->"dashed"[style=solid]
  "dashed"->"dotted"[style=dashed]
  "dotted"->"bold"[style=dotted]
  "bold"->"invis"[style=bold]
  "invis"->"solid"[style=invis]
}

./resources/svg/lines2.svg

2.3 常用箭头

  • box、crow、curve、icurve、diamond、dot、inv、none、normal、tee、vee
digraph arrow {
  rankdir=LR

  "box"->"crow"[arrowhead=box]
  "crow"->"curve"[arrowhead=crow]
  "curve"->"diamond"[arrowhead=curve]
  "diamond"->"dot"[arrowhead=diamond]
  "dot"->"inv"[arrowhead=dot]
  "inv"->"none"[arrowhead=inv]
  "none"->"normal"[arrowhead=none]
  "normal"->"tee"[arrowhead=normal]
  "tee"->"vee"[arrowhead=tee]
  "vee"->"box"[arrowhead=vee]

  #来个高级的用法
  a->b[arrowhead=lcrowortee]
}

./resources/svg/arrow.svg

想了解更多内容,那就点击我

2.4 常用属性

 
2.4.1 Node常用属性
属性名默认值说明
colorblacknode图形颜色,see color values
fontcolorblack字体颜色
fontnameTimes-Roman字体
fontsize14字体大小
image node背景图片地址
labelnode namenode 显示内容
shapeellipse形状
style 图形样式,eg. bold、dotted、filled
height.5最低高度尺寸
width.75最低宽度尺寸
URL 点击跳转地址
target 点击跳转打开方式
digraph demo_node {
   rankdir=LR
   "default_node"
   "node"[shape=box,color=antiquewhite,style=filled,label="node attrs",fontcolor=brown,fontsize=10,URL="http://www.baidu.com",target="_brank"]
   "default_node"->"node"
}

./resources/svg/node.svg

2.4.2 Edge常用属性
属性名默认值说明
arrowheadnormal箭头样式
arrowsize1.0箭头尺寸
colorblack颜色
dirforward箭头方向
edgeURL 点击跳转地址
fontcolorblack字体颜色
fontnameTimes-Roman字体
fontsize14字体大小
headport 线条头部连接处,eg.n、ne、e、se、s、sw、w、nw
label 描述内容
labelfontcolorblacklabel字体颜色
labelfontnameTimes-Romanlabel字体
labelfontsize14label字体大小
style 图形样式,eg.bold、dotted、filled
tailport 线条尾部连接处,eg.n、ne、e、se、s、sw、w、nw
weight1integer cost of stretching an edge
digraph demo_edge {
  rankdir=LR
  a->b[label="Yes",labelfontcolor=blue]
  edge[arrowhead=box,arrowsize=1.5,color=red,dir=both,style=dotted,weight=10]
  b->c
}

./resources/svg/edge.svg

2.4.3 Graph常用属性
属性名默认值说明
bgcolor 背景颜色
colorblackfor clusters,outline color,and fill color
dpi96图像输出像素
fillcolorblackcluster fill color
fontcolorblack字体颜色
fontnameTimes-Roman字体
fontsize14字体大小
label 描述内容
landscape true:orientation=landscape
rank 子图等级限制, same,min,max,source,sink
rankdirTB图片排列方式,LR(left to right) or TB(top to bottom)
size 最大图片尺寸
style 图片样式
stylesheet pathname or URL to XML style sheet for SVG
URL 点击跳转地址
target 点击跳转打开方式
digraph demo_graph {
  bgcolor=beige
  fontcolor=gray10
  fontsize=12
  label="this is demo"
  rankdir=TB

  {rank=same; "level1" "a-level1" "b-level1" "c-level1"}
  {rank=same; "level2" "d-level2" "e-level2"}
  {rank=same; "level3" "f-level3" "g-level3" "h-level3" "i-level3" "j-level3"}
  {rank=same; "level4" "k-level4"}
  "level1"->"level2"->"level3"->"level4"
  "a-level1"->"d-level2"
  "e-level2"->{"h-level3", "k-level4"}
  "b-level1"->{"e-level2", "f-level3", "i-level3", "k-level4"}
  "c-level1"->{"d-level2", "g-level3", "h-level3"}
  "j-level3"->{"f-level3", "k-level4"}
}

./resources/svg/graph.svg

2.5 子图

dot支持将一组元素组合成一个子图,且一个图形可以包含一个及多个子图。
使用子图需要注意以下两点:

  • 子图node无法在主图rank中使用。
  • 子图命名必须以:"cluster"作为前缀。
digraph demo_subgraph {
  bgcolor=beige
  fontcolor=gray10
  fontsize=12
  label="this is demo"
  rankdir=TB

  /* 特别注意: 子图命名必须以"cluster"为前缀 */
  subgraph "cluster_g1" {
    label="g1"
    bgcolor=cadetblue
    "level1"->"level2"->"level3"->"level4"
  }

  subgraph "cluster_g2" {
    label="g2"
    bgcolor=cornsilk
    "level2"
    "d-level2"
    "e-level2"
  }

  subgraph "cluster_g3" {
    label="g3"
    bgcolor=cornsilk
    "level3"
    "f-level3"
    "g-level3"
    "h-level3"
    "i-level3"
    "j-level3"
  }

  "a-level1"->"d-level2"
  "e-level2"->{"h-level3", "k-level4"}
  "b-level1"->{"e-level2", "f-level3", "i-level3", "k-level4"}
  "c-level1"->{"d-level2", "g-level3", "h-level3"}
  "j-level3"->{"f-level3", "k-level4"}
}

./resources/svg/subgraph.svg

2.6 其他用法

 
2.6.1 label使用html标签
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>>];
 }

./resources/svg/label_html.svg

2.6.2 使用label定义子节点
digraph structs {
  node [shape=record];
  struct1 [shape=record,label="<f0> left|<f1> middle|<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:f1 -> struct2:f0;
  struct1:f2 -> struct3:here;
}

./resources/svg/label_subnode.svg

3 总结

通过这几天对dot语言的学习,发现它还是非常强大和灵活的。只要能够发挥你的组合及想象能力,肯定是能够满足一些基本文档图表的绘制需求。而且还摆脱了普通制图功能繁琐的排版工作,还是非常适合有一定编程基础的用户使用的。
个人非常厌恶繁琐的排版工作,因此emacs + org-mode + Graphviz会是以后书写文档的必备工具。
之后有时间在将ditaa及plantuml集成进来,毕竟用他们绘制一些专业图表还是更加快捷方便一些。


参考文献:
http://billnote.github.io/chenfanyu/archive/2013/01/27/2878845.html
http://coldnew.github.io/blog/2013/07/13_07e15.html
http://www.graphviz.org

Date: 2014-10-22T14:02+0800

Author: Billyhuang@sina.cn

Org version 7.9.3f with Emacs version 24

Validate XHTML 1.0

 

转载于:https://www.cnblogs.com/billnote/p/4043267.html

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值