强曰为道

与天地相似,故不违。知周乎万物,而道济天下,故不过。旁行而不流,乐天知命,故不忧.
文档目录

04 - 节点详解

第 04 章 · 节点详解

4.1 节点形状 (Shape)

Graphviz 提供丰富的内置形状,通过 shape 属性设置。

常用形状一览

形状名说明典型场景
box矩形流程步骤、模块
box3d3D 立方体数据库、服务器
polygon多边形通用
ellipse椭圆(默认)开始/结束节点
oval椭圆同 ellipse
circle正圆状态节点
doublecircle双圆终止状态
diamond菱形判断/条件
trapezium梯形输入/输出
parallelogram平行四边形数据
hexagon六边形准备/预处理
octagon八边形中断
pentagon五边形
house房形子程序入口
invhouse倒房形子程序出口
cylinder圆柱数据库、存储
note便签注释
tab标签页HTML 页面
folder文件夹文件系统
component组件UML 组件
record记录结构体
Mrecord圆角记录同上,圆角
plaintext纯文本无边框标签
point连接点
star星形重要节点
underline下划线文本纯文本
none无形状隐藏节点
plain纯文本同 plaintext

形状展示图

digraph Shapes {
    rankdir=LR
    node [fontname="Microsoft YaHei" fontsize=10 style=filled]
    edge [style=invis]  // 隐藏边,仅用于排列

    box           [shape=box label="box\n矩形"          fillcolor="#E3F2FD" color="#1976D2"]
    ellipse       [shape=ellipse label="ellipse\n椭圆"  fillcolor="#E8F5E9" color="#388E3C"]
    circle        [shape=circle label="circle\n正圆"    fillcolor="#E8F5E9" color="#388E3C"]
    diamond       [shape=diamond label="diamond\n菱形"  fillcolor="#FFF3E0" color="#FF9800"]
    cylinder      [shape=cylinder label="cylinder\n圆柱" fillcolor="#F3E5F5" color="#7B1FA2"]
    hexagon       [shape=hexagon label="hexagon\n六边形" fillcolor="#FFEBEE" color="#C62828"]
    parallelogram [shape=parallelogram label="parallelogram\n平行四边形" fillcolor="#E0F7FA" color="#00838F"]
    trapezium     [shape=trapezium label="trapezium\n梯形" fillcolor="#FFF8E1" color="#F57F17"]
    star          [shape=star label="star\n星形"        fillcolor="#FCE4EC" color="#AD1457"]
    note          [shape=note label="note\n便签"        fillcolor="#FFFDE7" color="#F9A825"]

    box -> ellipse -> circle -> diamond -> cylinder
    cylinder -> hexagon -> parallelogram -> trapezium -> star -> note
}

4.2 节点样式 (Style)

通过 style 属性组合多种视觉效果:

样式属性值

样式值效果说明
filled填充背景色需配合 fillcolor
rounded圆角矩形仅对 box 有效
dashed虚线边框
dotted点线边框
bold加粗边框
solid实线边框默认
filled,rounded圆角+填充组合使用
diagonals对角线装饰
striped条纹填充多色条纹
wedged楔形填充渐变效果
invis不可见隐藏节点

样式组合示例

digraph NodeStyles {
    node [fontname="Microsoft YaHei" fontsize=11]

    // 基本样式
    default  [label="默认"]
    filled   [label="filled\n填充" style=filled fillcolor="#E3F2FD"]
    rounded  [label="rounded\n圆角" style="filled,rounded" fillcolor="#E8F5E9"]
    dashed   [label="dashed\n虚线" style=dashed]
    bold     [label="bold\n加粗" style=bold penwidth=2]

    // 组合样式
    combo1 [label="filled+rounded+dashed" style="filled,rounded,dashed" fillcolor="#FFF3E0"]
    combo2 [label="filled+bold" style="filled,bold" fillcolor="#FFEBEE" penwidth=3]

    // 高级样式
    striped [label="striped\n条纹" style=striped fillcolor="#E3F2FD:#E8F5E9"]
    wedge   [label="wedged\n楔形" style=wedged fillcolor="#1976D2:#388E3C"]

    default -> filled -> rounded -> dashed -> bold
    bold -> combo1 -> combo2 -> striped -> wedge
}

4.3 节点标签 (Label)

基本标签

digraph Labels {
    // 默认标签 = 节点 ID
    A  // 标签显示 "A"

    // 自定义标签
    B [label="自定义标签"]

    // 多行标签(使用 \n)
    C [label="第一行\n第二行\n第三行"]
}

标签中的特殊字符

需要转义或引号包裹的字符:

字符转义方式说明
{ }"\{" "\}" 或用引号Record 中有特殊含义
< >使用 HTML 标签开启 HTML 标签模式
``"|"
""\""引号本身
\"\\"反斜杠
换行\n多行文本

4.4 HTML 标签 (HTML-Like Labels)

Graphviz 支持使用 HTML 语法创建复杂标签,用 <> 包裹(不是 ")。

基本 HTML 标签

digraph HTMLLabels {
    node [fontname="Microsoft YaHei"]

    // HTML 标签 — 注意用 < > 而非 " "
    simple [label=<简单<b>粗体</b>文本>]

    // 表格布局
    table [label=<
        <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="8">
            <TR>
                <TD COLSPAN="2" BGCOLOR="#1976D2"><FONT COLOR="white"><B>用户信息</B></FONT></TD>
            </TR>
            <TR>
                <TD>姓名</TD>
                <TD>张三</TD>
            </TR>
            <TR>
                <TD>年龄</TD>
                <TD>28</TD>
            </TR>
        </TABLE>
    >]

    simple -> table
}

HTML 标签参考

HTML 标签说明常用属性
<B>...</B>粗体
<I>...</I>斜体
<U>...</U>下划线
<O>...</O>上划线
<S>...</S>删除线
<SUB>...</SUB>下标
<SUP>...</SUP>上标
<BR/>换行
<FONT>字体FACESIZECOLOR
<IMG>图片SRCSCALE
<TABLE>表格BORDERCELLBORDERCELLSPACINGCELLPADDING
<TR>VALIGNBGCOLOR
<TD>单元格COLSPANROWSPANBGCOLORALIGNPORT
<HR/>水平线

HTML 表格详解

digraph HTMLTable {
    node [fontname="Microsoft YaHei" shape=plain]

    // 复杂表格示例
    record [label=<
        <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="6">
            <TR>
                <TD COLSPAN="3" BGCOLOR="#4CAF50">
                    <FONT COLOR="white" POINT-SIZE="14"><B>系统架构</B></FONT>
                </TD>
            </TR>
            <TR>
                <TD BGCOLOR="#E3F2FD" PORT="frontend">
                    <B>前端层</B><BR/>
                    <FONT POINT-SIZE="9">React / Vue</FONT>
                </TD>
                <TD BGCOLOR="#E8F5E9" PORT="backend">
                    <B>后端层</B><BR/>
                    <FONT POINT-SIZE="9">Go / Java</FONT>
                </TD>
                <TD BGCOLOR="#F3E5F5" PORT="data">
                    <B>数据层</B><BR/>
                    <FONT POINT-SIZE="9">MySQL / Redis</FONT>
                </TD>
            </TR>
            <TR>
                <TD COLSPAN="3" BGCOLOR="#FFF3E0">
                    <FONT POINT-SIZE="9">基础设施:Docker + Kubernetes</FONT>
                </TD>
            </TR>
        </TABLE>
    >]

    client [label="客户端" shape=box style=rounded]
    client -> record:frontend [label="HTTP"]
    client -> record:backend [label="API"]
}

4.5 Record 形状

recordMrecord 形状用于表示结构化数据,使用 | 分隔字段。

基本 Record

digraph RecordDemo {
    node [fontname="Microsoft YaHei" fontsize=11]

    // record 形状 — 用 | 分隔字段
    struct [shape=record label="<f0> 左侧 |<f1> 中间 |<f2> 右侧"]

    // 带标题的 record
    titled [shape=record label="{标题|{<f0> 字段1|<f1> 字段2|<f2> 字段3}}"]

    // Mrecord — 圆角版本
    rounded_rec [shape=Mrecord label="<f0> 圆角记录|<f1> 字段"]
}

Record 字段方向

{} 控制字段排列方向:

  • 外层 {...} → 行(水平排列)
  • 内层 {...} → 列(垂直排列)
digraph RecordFields {
    node [shape=record fontname="Microsoft YaHei"]

    // 横向排列(用 | 分隔)
    horizontal [label="A | B | C"]

    // 纵向排列(用 {} 包裹)
    vertical [label="{{ A }|{ B }|{ C }}"]

    // 混合布局
    mixed [label="{
        标题 |
        {
            行1左 | 行1右
        } |
        {
            行2左 | 行2右
        }
    }"]
}

Record 端口连接

digraph RecordPorts {
    node [shape=record fontname="Microsoft YaHei"]

    struct1 [label="{<f0> 头部|<f1> 数据|<f2> 尾部}"]
    struct2 [label="{<f0> 输入|<f1> 处理|<f2> 输出}"]

    // 通过 端口名 连接
    struct1:f2 -> struct2:f0 [label="传输"]
    struct1:f1 -> struct2:f1 [label="直接连接" style=dashed]
}

4.6 端口 (Port)

端口允许精确控制边连接到节点的哪个位置。

位置端口

digraph PositionPorts {
    node [shape=box style=filled fillcolor="#E3F2FD" fontname="Microsoft YaHei"]

    center [label="中心节点"]
    top    [label="上方"]
    bottom [label="下方"]
    left   [label="左边"]
    right  [label="右边"]

    // 使用 :port 语法
    center -> top [taillabel="n" headport=n]
    center -> bottom [taillabel="s" headport=s]
    center -> left [taillabel="w" headport=w]
    center -> right [taillabel="e" headport=e]
}

端口方向

端口方向说明
n北(上)节点顶部中心
s南(下)节点底部中心
e东(右)节点右侧中心
w西(左)节点左侧中心
ne东北右上角
nw西北左上角
se东南右下角
sw西南左下角
c中心节点中心

Record 端口 vs 位置端口

digraph PortTypes {
    node [fontname="Microsoft YaHei"]

    // Record 端口
    rec [shape=record label="{<in> 输入|处理|<out> 输出}"]

    // HTML 端口
    html [label=<
        <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
            <TR><TD PORT="in">输入</TD><TD>处理</TD><TD PORT="out">输出</TD></TR>
        </TABLE>
    >]

    // 外部节点
    input [shape=box label="数据源"]
    output [shape=box label="目标"]

    input -> rec:in
    rec:out -> html:in
    html:out -> output
}

4.7 节点尺寸控制

digraph NodeSize {
    node [fontname="Microsoft YaHei" style=filled fillcolor="#E3F2FD" color="#1976D2"]

    // 固定宽高(英寸)
    fixed [label="固定大小\nwidth=2\nheight=1" width=2 height=1]

    // 最小宽度/高度
    min [label="最小尺寸\n0.5x0.5" fixedsize=true width=0.5 height=0.5]

    // 自适应(默认)
    auto [label="自适应大小\n节点根据内容调整"]

    fixed -> min -> auto
}
属性说明默认值
width宽度(英寸)根据内容
height高度(英寸)根据内容
fixedsize是否固定尺寸false(自适应)
margin内边距0.11, 0.055

4.8 业务场景:UML 类图

digraph UMLClass {
    rankdir=TB
    node [shape=plain fontname="Microsoft YaHei"]
    edge [fontname="Microsoft YaHei" fontsize=9]

    // 基类
    Animal [label=<
        <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
            <TR><TD BGCOLOR="#E8EAF6"><B>Animal</B></TD></TR>
            <TR><TD ALIGN="LEFT">
                # name: String<BR/>
                # age: int<BR/>
            </TD></TR>
            <TR><TD ALIGN="LEFT">
                + getName(): String<BR/>
                + makeSound(): void<BR/>
            </TD></TR>
        </TABLE>
    >]

    // 子类
    Dog [label=<
        <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
            <TR><TD BGCOLOR="#C8E6C9"><B>Dog</B></TD></TR>
            <TR><TD ALIGN="LEFT">- breed: String</TD></TR>
            <TR><TD ALIGN="LEFT">+ fetch(): void</TD></TR>
        </TABLE>
    >]

    Cat [label=<
        <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
            <TR><TD BGCOLOR="#C8E6C9"><B>Cat</B></TD></TR>
            <TR><TD ALIGN="LEFT">- indoor: boolean</TD></TR>
            <TR><TD ALIGN="LEFT">+ purr(): void</TD></TR>
        </TABLE>
    >]

    // 继承关系(空心三角箭头)
    Dog -> Animal [arrowhead=empty label="extends"]
    Cat -> Animal [arrowhead=empty label="extends"]
}

注意事项

⚠️ Record vs HTML 标签:HTML 标签功能更强大,推荐新项目优先使用 HTML 标签。

⚠️ Record 中的特殊字符{}|<> 在 Record 标签中有特殊含义,需转义。

⚠️ HTML 标签不用引号label=<...> 而不是 label="<...>"

⚠️ fixedsize=true:设置固定尺寸时,如果文本太长会被截断,需手动调整。

⚠️ 字体渲染:不同操作系统对同一字体名的渲染可能不同。


扩展阅读


下一章05 - 边详解 — 深入学习边的箭头、标签和约束。