DOT中使用图(
digraph
/graph
/subgraph
)、节点(node
)和边(edge
)来描述关系图/流程图,在配合一些属性的设置完成绘图。node
digraph/graph/subgraph graphName{
label="示例";
bgcolor="beige"
......
}
复制代码
其中,digraph
/graph
/subgraph
分别表明无向图、有向图和子图,graphName
则是图片的名称。bash
digraph
用来描述有向图,关系使用 ->
来描述graph
用来描述无向图,关系使用 --
来描述label
和bgcolor
为定义的图片属性graphName
定义图片的名称//
和 #
注释单行, /* */
多行注释。属性名 | 默认值 | 说明 |
---|---|---|
label | 图片标签,如上面示例 |
|
bgcolor | 背景颜色,颜色文档点此 | |
fontcolor | black | 字体颜色,定义上面示例 的颜色 |
fontname | Times-Roman | 字体 |
fontsize | 14 | 字体大小 |
rank | 子图等级限制, same,min,max,source,sink | |
rankdir | TB | 排序方向,LR(left to right) or TB(top to bottom) |
compound | false | If true, allow edges between clusters. 配合 lhead 和 ltail 使用 |
node
)的用法digraph demo {
label="示例"
bgcolor="beige"
//定义节点默认属性
node[color="grey"]
//定义节点
root[label="根节点", shape="box"]
left[label="左子节点", shape="box"]
node[color="#FF6347"]
right[label="右子节点", shape="ellipse"]
//定义关系(边)
root -> {left,right}[label="父子"]
left -> subnode[label="父子"]
{rank=same; left, right}
}
复制代码
root
、left
、right
、subnode
为节点。节点能够提早定义如root[label="根节点", shape="box"]
,也能够在定义边的关系时直接使用如left -> subnode[label="父子"]
。[]
内属性,属性能够针对图、节点、边来设置。rank
定义设置节点处在同一行,辅助渲染出来的图的效果。属性名 | 默认值 | 说明 |
---|---|---|
label | node name | 节点显示内容 |
color | black | node边框颜色 |
fontcolor | black | 字体颜色 |
fillcolor | 背景色 | |
fontname | Times-Roman | 字体 |
fontsize | 14 | 字体大小 |
shape | ellipse | 形状,box、ellipse、circle、diamond、plaintext、point、triangle、invtriangle |
style | 图形样式,eg. bold、dashed、dotted、filled | |
image | 背景图片地址 |
digraph demo {
bgcolor="floralwhite"
"box"[shape=box]
"polygon"[shape=polygon,sides=7]
"ellipse"[shape=ellipse]
"circle"[shape=circle]
"point"[shape=point]
"triangle"[shape=triangle]
"invtriangle"[shape=invtriangle]
"plaintext"[shape=plaintext]
"diamond"[shape=diamond]
}
复制代码
digraph demo {
label="示例"
bgcolor="beige"
//定义节点默认属性
node[color="grey"]
//定义节点
root[label="根节点", shape="box"]
left[label="左子节点", shape="box"]
node[color="#FF6347"]
right[label="右子节点", shape="ellipse"]
//定义关系(边)
root -> {left,right}[label="父子"]
//定义边的默认属性
edge[color="#FF6347"]
left -> subnode[label="父子"]
{rank=same; left, right}
}
复制代码
edge用来定义边的默认属性。用法与node类似,做用域从本次定义到下一次定义截住。特定边设置的属性会覆盖默认值。markdown
本质上来讲,node和edge也是属于节点,是预留用于描述节点属性和边属性的特殊节点ide
无向图关系使用--
描述,有向图关系使用->
描述oop
属性名 | 默认值 | 说明 |
---|---|---|
label | 描述关系 | |
color | black | 箭头颜色 |
fontcolor | black | 关系文字颜色 |
dir | forward | 设置方向:forward,back,both,none |
arrowhead | normal | 箭头头部形状。box、crow、diamond、dot、none、normal、vee。箭头文档点此 |
arrowtail | 箭头尾部形状 | |
arrowsize | 1.0 | 箭头大小 |
style | 图形样式,eg. bold、dashed、dotted、filled | |
lhead | 当 compound 为true时,lhead用于指定边指向的cluster | |
ltail | 与ltail相似 |
digraph demo {
bgcolor="floralwhite"
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]
}
复制代码
一个图能够包含多个子图,以及子图也能够嵌套子图。子图的名字须为cluster*
,不然就直接当节点渲染了。字体
digraph demo {
bgcolor="beige"
subgraph cluster_husband {
node[color="grey"]
{"爸爸", "妈妈"} -> "我"
}
subgraph cluster_wife {
{"岳父", "岳母"} -> "老婆"
}
"我" -> "老婆"[label="夫妻", dir="both"]
{rank=same; "我", "老婆"}
}
复制代码
渲染效果以下:spa
digraph demo {
bgcolor="beige"
rankdir=LR;
node [shape="record", height=0.1]
node0[label="{<f1> G | <f2> next}"]
node1[label="{<f1> E | <f2> next}"]
node0:f2 -> node1:f1
}
复制代码
rankdir=LR
描述图像为横向node0[label="{<f1> G | <f2> next}"]
中,{}描述同个节点中的多个分割位与rankdir
一致digraph demo {
bgcolor="beige"
node [shape="record", height=.1]
node0[label="<f1> A | <f2> B | <f3> C | <f4> "]
node1[label="<f1> D | <f2> E | <f3> F"]
node2[label="<f1> G | <f2> H | <f3> I"]
node0:f1 -> node1:f1
node0:f2 -> node2:f1
}
复制代码