zoukankan      html  css  js  c++  java
  • 使用 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 
     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

  • 相关阅读:
    5000 端口 转发
    程序的运行环境=内存+运行库+系统调用
    日志异步落库
    malloc分配的空间是连续的吗?
    PE/ELF文件里面存的是什么呢?
    [Python]编码声明:是coding:utf-8还是coding=utf-8呢
    基于可执行代码的缓冲区溢出检测模型
    SU Demos-06Selecting Traces
    SU Demos-05Sorting Traces-03susorty
    SU Demos-05Sorting Traces-02Demos
  • 原文地址:https://www.cnblogs.com/fengbohello/p/5856085.html
Copyright © 2011-2022 走看看