使用 Graphviz 畫拓撲圖


使用 Graphviz 畫拓撲圖


0)前述

本文着重講一下 Graphviz 的風格控制,基礎一些的就不在這里講啦。

graphviz 的主頁是http://www.graphviz.org/

Graphviz 的安裝和使用請看這里:http://www.cnblogs.com/fengbohello/p/4689131.html

1)從 Record 開始

下面通過一個簡單示例開始吧:

在 Graphviz 的文檔頁有如下一張圖,下面就用它里開始練習了。

簡單的 Record 風格

這里

這幅圖的dot代碼如下:

01 digraph A {
02     node [shape=record];
03     struct1 [label="<f0> left|<f1> middle|<f2> right"];
04     struct2 [label="<f0> one|<f1> two"];
05     struct3 [label="hello \n world |{ b |{c|<here> d|e}| f}| g | h"];
06     struct1:f1 -> struct2:f0;
07     struct1:f2 -> struct3:here;
08 }

第一行,聲明圖的類型為 "digraph" 和圖的ID 為 "A"

第二行,聲明本圖的node的類型均為Record

第三行,聲明了一個結構體,並且結構體分為三部分,分別為f0,f1,f2。f0的名稱為left,f1的名稱為middle,f2的名稱為right。這三部分用"|"隔開,並且他們都屬於這個struct1這個結構體的label.

第四行,聲明了struct2。

第五行,看起來稍微復雜些。這一行聲明了結構體struct3,並且使用了三層嵌套。第一層是“helloworld”、“b,c,d,e,f”、“g”和“h”。第二層是“b,c,d,e,f”這一組被分割成了三部分:“b”、"c,d,e"和"f"。第三層是"c,d,e"又被分割成了"c"、“d“和“e”三部分。嵌套的時候,使用大括號進行隔離。

第六~七行,開始建立node間的關系。struct1的f0指向struct2的f0;struct1 的 f2 指向 struct3 的 here。

從這段腳本代碼的直觀感受,dot腳本語言是一個描述型語言。 相信參照這個例子,已經可以寫出關系清晰的拓撲圖了。


接下來我們再來看一下另一種寫法。

here

digraph structs {
    node [shape=plaintext]
    struct1 [label=<
    <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
    <TR>
        <TD>left</TD>
        <TD PORT="f1">middle</TD>
        <TD PORT="f2">right</TD>
    </TR>
    </TABLE>>];

    struct2 [label=<
    <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
    <TR>
        <TD PORT="f0">one</TD>
        <TD>two</TD>
    </TR>
    </TABLE>>];

    struct3 [label=<
    <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
    <TR>
        <TD ROWSPAN="3">hello<BR/>world</TD>
        <TD COLSPAN="3">b</TD>
        <TD ROWSPAN="3">g</TD>
        <TD ROWSPAN="3">h</TD>
    </TR>
    <TR>
        <TD>c</TD>
        <TD PORT="here">d</TD>
        <TD>e</TD>
    </TR>
    <TR>
        <TD COLSPAN="3">f</TD>
    </TR>
    </TABLE>>];

    struct1:f1 -> struct2:f0;
    struct1:f2 -> struct3:here;
}

這段代碼就不詳細解釋了,對照着圖像仔細想一想,應該也能想明白。熟悉HTML同學應該一眼就看明白了,其中的table、tr、td等,就是HTML里面的table。照着這個思路去看,應該能理解的更快。其中的COLSPAN等關鍵字在文檔頁中可以找到其具體含義。


我想分析一下下面這個圖:

more

代碼如下(或者這里

01  digraph G {
02      rankdir=LR
03      node [shape=plaintext]
04
05      a [
06          label=<
07          <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
08          <TR>
09              <TD ROWSPAN="3" BGCOLOR="yellow">class</TD>
10          </TR>
11          <TR>
12              <TD PORT="here" BGCOLOR="lightblue">qualifier</TD>
13          </TR>
14          </TABLE>>
15      ]
16
17     b [shape=ellipse style=filled
18     label=<
19     <TABLE BGCOLOR="bisque">
20     <TR>
21         <TD COLSPAN="3">elephant</TD> 
22         <TD ROWSPAN="2" BGCOLOR="chartreuse" 
23             VALIGN="bottom" ALIGN="right">two</TD> 
24     </TR>
25     <TR>
26         <TD COLSPAN="2" ROWSPAN="2">
27             <TABLE BGCOLOR="grey">
28             <TR> 
29                 <TD>corn</TD> 
30             </TR> 
31             <TR> 
32                 <TD BGCOLOR="yellow">c</TD> 
33             </TR> 
34             <TR> 
35                 <TD>f</TD> 
36             </TR> 
37             </TABLE> 
38         </TD>
39         <TD BGCOLOR="white">penguin</TD> 
40      </TR> 
41      <TR> 
42          <TD COLSPAN="2" BORDER="4" ALIGN="right" PORT="there">4</TD> 
43      </TR>
44      </TABLE>>
45      ]
46
47      c [ 
48          label=<
49              long line 1<BR/>line 2<BR ALIGN="LEFT"/>line 3<BR ALIGN="RIGHT"/>>
50      ]
51  
52      subgraph { rank=same b c }
53      a:here -> b:there [dir=both arrowtail = diamond]
54      c -> b
55      d [shape=triangle]
56      d -> c [label=<
57      <TABLE>
58      <TR>
59          <TD BGCOLOR="red" WIDTH="10"> </TD>
60          <TD>Edge labels<BR/>also</TD>
61          <TD BGCOLOR="blue" WIDTH="10"> </TD>
62      </TR>
63      </TABLE>>
64      ]
65  }

 

分析

第5行到第15行,定義了一個 node,這個 node 的label是一個table。圖片的左下角就是這個node啦。

第7行,定義了這個table的屬性,屬性值如下:

屬性 具體含義
BORDER="value" specifies the width of the border around the object in points
CELLBORDER="value" specifies the width of the border for all cells in a table. It can be overridden by a BORDER tag in a cell.
CELLSPACING="value" specifies the space, in points, between cells in a table and between a cell and the table's border.
ROWSPAN="value" specifies the number of rows spanned by the cell.
BGCOLOR="color" sets the color of the background. This color can be overridden by a BGCOLOR attribute in descendents.
PORT="value" attaches a portname to the object. (See portPos.)
COLSPAN="value" specifies the number of columns spanned by the cell.

第8~13行,定義了兩個單元格。

第17~45行 定義了node :b。

第17行,指明了node的shape是ellipse(橢圓),style是filled。

第19~40行,這段代碼直接扔到html頁面里面,也可以看到差不多相同的效果:

elephant two
corn
c
f
penguin
4

感覺不需要再寫下去了,再寫下去的話就是在寫html了。所以放一個html的table教程鏈接吧。http://www.w3school.com.cn/tags/tag_table.asp

所以學好html是關鍵!!!

第53~56行,建立這幾個node之間的關系。

 

Graphviz 官網的幾個文檔

Node Shapes

attrs

SDL


 

同步發表:https://www.fengbohello.top/archives/graphviz-practice

 


免責聲明!

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



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