GraphViz的使用

前言

在使用GraphViz之前,一直在使用Xmind,還有OmniGraffle,但是拖來(lái)拖去,完全不符合我們程序員的style啊,所以我也不喜歡用Xcode的SB,xib。
GraphViz,簡(jiǎn)單,而且方便,容易學(xué)習(xí)。

  • GraphViz是什么?
    GraphViz是一個(gè)開(kāi)源的圖像可視化的軟件,是貝爾實(shí)驗(yàn)室開(kāi)發(fā)的一個(gè)開(kāi)源的工具包,它使用一個(gè)特定的DSL(領(lǐng)域特定語(yǔ)言): dot作為腳本語(yǔ)言,然后使用布局引擎來(lái)解析此腳本,并完成自動(dòng)布局。graphviz提供豐富的導(dǎo)出格式,如常用的圖片格式,SVG,PDF格式等。
  • 怎么使用GraphViz?
    GraphViz使用DOT語(yǔ)言,而DOT是純文本圖像描述語(yǔ)言,簡(jiǎn)單易學(xué),還容易閱讀。

1、安裝

我是使用Sublime Text3配套使用的,只要在Sublime Text中安裝plugin即可。首先使用shift+command+p,輸入搜索選中Package Control: Install Package,然后輸入GraphViz,然后安裝GraphVizPreview即可,安裝完成后,只要全選中代碼,然后按shift+command+g就可以預(yù)覽了。
其實(shí)也可以直接用brew安裝GraphViz,但是每次要敲命令行,太麻煩了。

2、使用

GraphViz中包含多種布局:

  • dot 默認(rèn)布局,用于有向圖
  • neato 基于spring-model算法(force-based)
  • twopo 徑向布局
  • circo 圓形布局
  • fdp 用于無(wú)向圖

2.1 簡(jiǎn)單使用

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

效果圖:

enter description here

是不是很簡(jiǎn)單呢,還有有向圖如下:

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

效果圖:

2.2 簡(jiǎn)單的語(yǔ)法介紹

在這里就不啰嗦了,鏈接在此:語(yǔ)法介紹

2.3 稍微復(fù)雜點(diǎn)的例子

2.3.1 帶標(biāo)簽

digraph {

    player[label = "player"];
    game[label = "game"];

    player -> game[label = "play"]
}
enter description here

2.3.2 不同顏色

digraph {

    player[label = "player", color = Blue, fontcolor = Red, fontsize = 24, shape = box];
    game[label = "game", color = Red, fontcolor = Blue, fontsize = 24, shape = ellipse];

    player -> game[label = "play"]
}

enter description here

更多shape,看這里

2.4 一些技巧

2.4.1 插入圖片

digraph {

    c[shape = none, image = "./pic.png"]
    a -> b -> c;
    c -> d;
}
enter description here

注:需要用命令行dot test.dot -T png -o test.png生成,前提是用brew安裝了GraphViz

2.4.2 統(tǒng)一節(jié)點(diǎn)和連線

digraph {

    node[color = Red, fontsize = 24, shape = box]
    edge[color = Blue, style = "dashed"]

    c[shape = none, image = "./pic.png"]
    a -> b -> c;
    c -> d;
}

2.4.3 子視圖

digraph {

    label = visitNet

    rankdir = LR

    node[color = Red, fontsize = 24, shape = box]
    edge[color = Blue, style = "dashed"]

    user[style = "filled", color = "yellow", fillcolor = "chartreuse"]
    subgraph cluster_cd{
        label = "server and browser"
        bgcolor = green;

        browser -> server
    }

    user -> computer;
    computer -> browser;
}
enter description here

2.4.4 結(jié)構(gòu)視圖

digraph {

    node[shape = record];
    struct1[label = "<f0> left|<f1> mid&#92; dle|<f2> right"];
    struct2[label = "<f0> one|<f1> two"];
    struct3[label = "hello&#92;nworld | {b|{c|<here> d|e}|f}|g|h"];
    struct1:f1 -> struct2:f0;
    struct1:f2 -> struct3:here;
}

這里引用了官方的一個(gè)例子,更多。

2.5.5 樹(shù)形結(jié)構(gòu)

digraph tree {
  
  fontname = "PingFang-SC-Light"
  fontsize = 24

  node[shape = "plaintext"]

  1 -> 2;
  1 -> 3;
  2 -> 4;
  2 -> 5;
  3 -> 6;
  3 -> 7;
  4 -> 8;
  4 -> 9;
  5 -> 10;
  5 -> 11;
  6 -> 12;
  6 -> 13;
  7 -> 14;
  7 -> 15;
}
enter description here

2.4.6 繼承

digraph UML {

    node[fontname = "Courier New", fontsize = 10, shape = record];
    edge[fontname = "Courier New", fontsize = 10, arrowhead = "empty"];

    Car[label = "{Car | v : float\nt : float | run() : float}"]

    subgraph clusterSome{
        bgcolor = "yellow";
        Bus[label = "{Bus | | carryPeople() : void}"];
        Bike[label = "{bike | | ride() : void}"];
    }

    Bus -> Car
    Bike -> Car

}

2.4.7 時(shí)序圖

digraph time {

    rankdir = "LR";
    node[shape = "point", width = 0, height = 0];
    edge[arrowhead = "none", style = "dashed"];

    {
        rank = "same"
        edge[style = "solided"];
        APP[shape = "plaintext"];
        APP -> step00 -> step01 -> step02 -> step03 -> step04 -> step05;
    }
    
    {
        rank="same";
        edge[style="solided"];
        SDK[shape="plaintext"];
        SDK -> step10 -> step11 -> step12 -> step13 -> step14 -> step15;
    }
    {
        rank="same";
        edge[style="solided"];
        AliPay[shape="plaintext"];
        AliPay -> step20 -> step21 -> step22 -> step23 -> step24 -> step25;
    }
    {
        rank="same";
        edge[style="solided"];
        Server[shape="plaintext"];
        Server -> step30 -> step31 -> step32 -> step33 -> step34 -> step35;
    }

    step00 -> step10 [label="sends order info", arrowhead="normal"];
    step11 -> step21 [label="open AliPay", arrowhead="normal"];
    step22 -> step12 [label="pay success", arrowhead="normal"];
    step13 -> step03 [label="pay success", arrowhead="normal"];
    step24 -> step34 [label="pay success", arrowhead="normal"];
}

參考

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容