有关graphviz的html-like-labels

最近需要画一些类似表格的图, 简单的了解下graphviz的html-like-labels, 着实感觉非常强大~ 它在Node Shapes中有非常详细的说明.

先来一张之前在了解IP fragmentation的图:

附上对应的dot代码:

dot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
digraph ip {
    label = <<B>ip datagram</B>>;

    ip [shape=none, width=0, height=0, margin=0, label=
<
    <table title="ip datagram" border="0" cellborder="1" cellspacing="0" cellpadding="4">
        <tr>
            <td colspan="4">version</td>
            <td colspan="4">length</td>
            <td colspan="8">type of service(TOS)</td>
            <td colspan="16" width="256">total length</td>
        </tr>
        <tr>
            <td colspan="16" bgcolor="bisque">identification</td>
            <td colspan="3"  bgcolor="bisque"><FONT color="red">flags</FONT></td>
            <td colspan="13" bgcolor="bisque"><FONT color="red">fragment offset</FONT></td>
        </tr>
        <tr>
            <td colspan="8">time to live(TTL)</td>
            <td colspan="8">protocol</td>
            <td colspan="16">header checksum</td>
        </tr>
        <tr>
            <td colspan="32">source ip address</td>
        </tr>
        <tr>
            <td colspan="32">destination ip address</td>
        </tr>
        <tr>
            <td colspan="32" bgcolor="grey">options(if any)</td>
        </tr>
        <tr>
            <td colspan="32" rowspan="3" height="64" bgcolor="grey">data</td>
        </tr>
    </table>
>
    ]
}

差不多效果就是这个样子~ 使用html的这种形式我们可以非常方便的定义图形, 文字以及它们的样式. 很关键的一点:

Although HTML labels are not, strictly speaking, a shape, they can be viewed as a generalization of the record shapes described above. In particular, if a node has set its shape attribute to none or plaintext, the HTML label will be the node’s shape. On the other hand, if the node has any other shape (except point), the HTML label will be embedded within the node the same way an ordinary label would be.

此外, 我们也可以利用它来绘制各种数据结构的图. 有兴趣的同学可以看看这个hash数据结构指向的问题.

Comments