Graphviz使用入門教程


原文: https://wangxin93.github.io/linux/2018/09/21/graphviz-tutor.html


前言

Graphviz是一個“所見即所想”的繪制有向圖,無向圖的工具。它使用了布局算法對節點位置進行自動排版,可以導出為jpg,svg,pdf等不同格式,使用dot語言作為繪圖指令。

了解關於dot文件的指令,可以使用man dot查詢到更多,比如dot, neato, circo。
安裝

Mac 系統使用:$ brew install graphviz

Ubuntu 系統使用:$ sudo apt-get install graphviz
快速開始
無向圖

創建一個dot語言腳本文件並命名為demo.dot:

graph graphname {
a -- b;
b -- c;
b -- d;
d -- a;
}

我們將使用該腳本繪制一個無向圖,創建一個Makefile文件:

demo.jpg:demo.dot
dot -Tjpg demo.dot -o demo.jpg

clean:
rm demo.jpg

然后在同一個目錄下執行make指令,可以看到graphviz為我們輸出了一個jpg格式的文件。

EXTRA: 如果將Makefile中的指令替換為circo -Tjpg demo.dot -o demo.jpg,可以得到一個不同的layout的輸出圖像。
有向圖

再來繪制一個有向圖,需要注意的是無向圖使用graph關鍵字來定義,而有向圖使用digraph關鍵字來定義,同時需要使用->來表示圖上邊的方向:

digraph graphname {
a -> b;
b -> c;
b -> d;
d -> a;
}

節點與邊的屬性

如果希望在圖上顯示節點的屬性或者邊上的屬性,可以分開記錄節點的屬性和節點的關系:

digraph graphname {
/* Entities */
a [label="First" shape="circle" fontcolor="green" color="red"]
b [label="Second" shape="square" fontcolor="blue"]
c [label="thrid" shape="box"]

    /* Relationships */
    a -> b;
    b -> c;
    b -> d;
    d -> a [label="Edge Attribute"];

}

所有的屬性的可選項可以在這里找到。
排版方向

如果你不希望圖的分布是從上到下的而是從左到右的,可以更改代碼為:

digraph graphname {
rankdir="LR";
a -> b;
b -> c;
b -> d;
d -> a;
}

Rank

Graphviz使用rank系統來對圖進行排序,即一個節點的rank大於指向它對最大rank的節點的rank,因此一個有向圖中最大的rank的那個節點在最底部。如果希望改變rank值來改變分布,可以使用類似於{rank=same b d}的指令:

digraph graphname {
/* Entities */
a [label="First" shape="circle" fontcolor="green" color="red"]
b [label="Second" shape="square" fontcolor="blue"]
c [label="thrid" shape="box"]

    /* Relationships */
    a -> b;
    b -> c;
    b -> d;
    d -> a [label="Edge Attribute"];

    /* Ranks */

i {rank=same b d}
}

這是b和d節點會被排列到同一條水平線上。
Subgraph

Subgraph在graph中用一對花括號來標示。

digraph graphname {
a [label="First" shape="circle" fontcolor="green" color="red"]
b [label="Second" shape="square" fontcolor="blue"]
c [label="thrid" shape="box"]
a -> b;
b -> c;
b -> d;
d -> a;

    subgraph subs {
        d -> b;
        c;
    }

}

如果subgraph的名字使用cluster開頭,那么它就是一個cluster,一個不是cluster的subgraph只有一個屬性可以設置rank。

cluster會在周圍有一個方形邊框,你可以給cluster設置一個名字:

digraph graphname {
a [label="First" shape="circle" fontcolor="green" color="red"]
b [label="Second" shape="square" fontcolor="blue"]
c [label="thrid" shape="box"]
a -> b;
b -> c;
b -> d;
d -> a;

    subgraph cluster_subs {
        label="b & c & d";
        d -> b;
        c;
    }

}

Records

Node形狀中的record非常適合用於創建UML圖,例如:

digraph {
rankdir="BT"

Hero [
shape="record"
label="{Hero|+ health : int\l|+ save(k : Kingdom) : bool\l}"
]
Villain [
shape="record"
label="{Villain|+ health : int\l|+ brood() : void\l}"
]
Character [ shape="record" ]

Hero -> Character [ arrowhead="empty" ]
Villain -> Character [ arrowhead="empty" ]
}

參考鏈接

Official DOT Documentation
graphviz教程
Quick Introduction to graphviz
VSCode Markdown Enhanced Previewed 支持在markdown文件中插入dot文件代碼塊來畫圖


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM