數據結構與算法可視化(二)——DOT用法 | 八月更文挑戰

DOT中使用圖(digraph/graph/subgraph)、節點(node)和邊(edge)來描述關係圖/流程圖,在配合一些屬性的設置完成繪圖。node

一、圖的基本用法

DOT基本語法以下:

digraph/graph/subgraph graphName{
    label="示例";
    bgcolor="beige"
    ......
}
複製代碼

其中,digraph/graph/subgraph分別表明無向圖、有向圖和子圖,graphName則是圖片的名稱。bash

  • digraph用來描述有向圖,關係使用 -> 來描述
  • graph 用來描述無向圖,關係使用 --來描述
  • labelbgcolor爲定義的圖片屬性
  • graphName定義圖片的名稱
  • 註釋和C語言相似,//# 註釋單行, /* */多行註釋。
  • DOT的寫法不算嚴格,好比結束能夠有分號,屬性能夠沒有引號。

經常使用圖屬性

屬性名 默認值 說明
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}
}
複製代碼
  • rootleftrightsubnode爲節點。節點能夠提早定義如root[label="根節點", shape="box"],也能夠在定義邊的關係時直接使用如left -> subnode[label="父子"]
  • node用來定義節點的默認屬性,做用域從本次定義到下一次定義截住。特定節點設置的屬性會覆蓋默認值。
  • []內屬性,屬性能夠針對圖、節點、邊來設置。
  • rank 定義設置節點處在同一行,輔助渲染出來的圖的效果。

實例圖一.png

經常使用節點屬性

屬性名 默認值 說明
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 背景圖片地址

shape示例

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]
}
複製代碼

示例圖二.png

三、邊(edge)的用法

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

示例圖三.png

經常使用邊屬性

屬性名 默認值 說明
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相似

arrowhead示例

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]
}
複製代碼

示例圖四.png

四、一些例子

子圖

一個圖能夠包含多個子圖,以及子圖也能夠嵌套子圖。子圖的名字須爲cluster*,不然就直接當節點渲染了。字體

digraph demo {
    bgcolor="beige"
    subgraph cluster_husband {
        node[color="grey"]
        {"爸爸", "媽媽"} -> "我"
    }
     subgraph cluster_wife {
         {"岳父", "岳母"} -> "老婆"
     }
     "我" -> "老婆"[label="夫妻", dir="both"]
     {rank=same; "我", "老婆"}
}
複製代碼

渲染效果以下:spa

示例圖五.png

鏈表

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一致

示例圖六.png

B樹

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
}
複製代碼

示例圖七.png

相關文章
相關標籤/搜索