首頁 > 軟體

程式設計師繪圖利器 — Graphviz

2020-06-16 17:42:27

概述

官網: http://www.graphviz.org/

Graphviz (Graph Visualization Software) 是一個由AT&T實驗室啟動的開源工具包。DOT是一種圖形描述語言,非常簡單的,

Graphviz就是用來處理這種語言的工具。只需要簡單了解一下DOT語言,就可以用Graphviz繪圖了,它對程式設計師特別有用。

So in short, if you are a programmer, it is born for you。

無向圖

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

 

有向圖

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

 

屬性

//DOT語言中,可以對節點和邊新增不同的屬性。

digraph graphname {
    //節點的屬性,節點的名稱
    a [lable = "Foo"];

    //節點的屬性,節點的形狀
    b [shape = box];

    //邊的屬性,邊的顏色
    a -> b -> c [color = blue];

    //邊的屬性,邊的線狀
    b -> d [style = dotted];
}

 

基本圖形

digraph G {
//把圖片的尺寸設為4inch * 4inch
size = "4,4";
main [shape = box];

//邊的重要程度,預設是1
main->parse [weight = 8];
parse->execute;

//點狀線
main->init[style = dotted];
main->cleanup;

//連線了兩條線
execute->{make_string;printf}
init->make_string;

//把邊的預設顏色設為red
edge [color = red];
main->printf [sytle=bold, label = "100times"];

//節點的名稱
make_string [label = "make anstring"];

//設定節點的預設屬性
node [shape=box,style =filled,color=lightgrey];
execute->compare;
}

 

 

多邊形

digraph G{
a -> b -> c;
b -> d;

/* 形狀為多邊形,邊數為5,外框為3條,顏色為淡藍,樣式為填充 */
a [shape = polygon, sides = 5, peripheries = 3, color = lightblue, style = filled];

/* 形狀為多邊形,邊數為4,角的傾斜度為0.4,內容為hellow world*/
c [shape = polygon, sides = 4, skew = 0.4, label = "hello world"];

/* 形狀為倒三角,整體旋轉30度 */
d [shape = invtriangle,orientation = 30];

/* 形狀為多邊形,邊數為4,扭曲度為0.7 */
e [shape = polygon, sides = 4, distortion = 0.7];
}

 

 

資料結構

 

(1)複雜的標籤

digraph structs {
/* 把節點的預設形狀設為矩形record,預設的是圓角矩形Mrecord */
node [shape = record];

struct1 [label = "left|middle|right"];
struct2 [label = "one|two"];
struct3 [label = "hellonworld|{b|{c|d|e}|f}|g|h"];

struct1 -> struct2;
struct1 -> struct3;
}


 

graph picture {
//這幅圖的名字
label = "I love you";

//圖名字的位置在bottom,也可以是t
labelloc = b;

//圖名字的位置在left,也可以是r
labeljust = l;

edge[decorate = true];

C -- D [label = "s1"];
C -- E [label = "s2"];
C -- F [label = "s3"];
D -- E [label = "s4"];
D -- F [label = "s5"];

edge[decorate = false, labelfontcolor = blue, fontcolor = red];
C1 -- D1 [headlabel = "c1",taillabel = "d1",label = "c1 - d1"];
}


 

 

(2)行列對齊

digraph html {
rankdir = LR;
{
node[shape = plaintext];
1995 -> 1996 -> 1997 -> 1998 -> 1999 -> 2000 -> 2001;
}
{
node[shape = box, style = filled];
WAR3 -> Xhero -> Footman -> DOTA:
WAR3 -> Battleship;
}
{rank = same; 1996; WAR3;}
{rank = same; 1998; Xhero; Battleship;}
{rank = same; 1999; Footman;}
{rank = same; 2001; DOTA;}
}

 

 

(3)二元樹

digraph G {
label = "Binary search tree";
node [shape = record];

A [label = "<f0>|<f1>A|<f2>"];
B [label = "<f0>|<f1>B|<f2>"];
C [label = "<f0>|<f1>C|<f2>"];
D [label = "<f0>|<f1>D|<f2>"];
E [label = "<f0>|<f1>E|<f2>"];
F [label = "<f0>|<f1>F|<f2>"];
G [label = "<f0>|<f1>G|<f2>"];

A:f0 -> B:f1;
A:f2 -> C:f1;
B:f0 -> D:f1;
B:f2 -> E:f1;
C:f0 -> F:f1;
C:f2 -> G:f1;
}

 

 

(4)雜湊表

digraph G{
nodesep = .05;
rankdir = LR;

node [shape = record,width = .1,height = .1];
node0 [label = "<f0>|<f1>|<f2>|<f3>|<f4>|<f5>|<f6>|",height = 2.5];

node [width = 1.5];
node1 [label = "{<n>n14|719|<p>}"];
node2 [label = "{<n>a1|805|<p>}"];
node3 [label = "{<n>i9|718|<p>}"];
node4 [label = "{<n>e5|989|<p>}"];
node5 [label = "{<n>t20|959|<p>}"];
node6 [label = "{<n>o15|794|<p>}"];
node7 [label = "{<n>s19|659|<p>}"];

node0:f0 -> node1:n;
node0:f1 -> node2:n;
node0:f2 -> node3:n;
node0:f5 -> node4:n;
node0:f6 -> node5:n;
node2:p -> node6:n;
node4:p -> node7:n;
}


 

 

流程圖

digraph G{
subgraph cluster0 {
node [style = filled,color = white];
style = filled;
color = lightgrey;
a0 -> a1 -> a2 -> a3;
label = "process #1";
}

subgraph cluster1 {
node [style = filled];
b0 -> b1 -> b2 -> b3;
label = "process #2";
color = blue;
}

start -> a0;
start -> b0;
a1 -> b3;
b2 -> a3;
a3 -> a0;
a3 -> end;
b3 -> end;
start [shape = Mdiamond];
end [shape = Msquare];
}


 

Reference

[1] http://zh.wikipedia.org/zh-cn/DOT 語言,DOT語言簡明介紹。

[2] http://zh.wikipedia.org/zh/Graphviz ,簡單背景知識。

本文永久更新連結地址http://www.linuxidc.com/Linux/2016-05/131060.htm


IT145.com E-mail:sddin#qq.com