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

Graphviz 图形可视化教程 / 08 - 属性系统

第 08 章 · 属性系统

8.1 属性层次结构

Graphviz 的属性分为三个层次,优先级从高到低:

层次 设置方式 作用范围 优先级
个别声明 A [color=red] 单个元素 最高
通用默认 node [color=red] 当前子图及后代
全局默认 系统默认值 所有元素 最低
digraph AttrHierarchy {
    // 全局(对所有 node 生效)
    node [fontname="Microsoft YaHei" fontsize=11 style=filled]

    // 子图级默认
    subgraph cluster_example {
        // 子图内默认(覆盖全局)
        node [fillcolor="#E3F2FD" color="#1976D2"]

        A [label="继承子图默认"]
        B [label="个别覆盖" fillcolor="#FFEBEE" color="#C62828"]
        C [label="继承子图默认"]

        A -> B -> C
    }

    D [label="继承全局默认" fillcolor="#E8F5E9" color="#388E3C"]
    C -> D
}

8.2 图 (Graph) 属性

布局与尺寸

属性 说明 常用值 默认值
size 最大尺寸(英寸) "8,5" 不限制
ratio 宽高比 fillcompressauto、数值 不限制
dpi 分辨率 7296150300 96
margin 边距 1.0"0.5,0.5" 0.5
pad 内边距 0.5 0.0555
nodesep 同层节点间距 0.25~1.0 0.25
ranksep 层间距离 0.5~1.5 0.5
newrank 新排名算法 truefalse false
searchsize 搜索限制 数值 30

标题与装饰

属性 说明 常用值
label 图标题 任意文本
labelloc 标题垂直位置 t(顶部)、b(底部)、c
labeljust 标题水平对齐 l(左)、r(右)、c(居中)
fontname 全局字体 "Microsoft YaHei"
fontsize 全局字号 1416
fontcolor 全局字体颜色 "black"
bgcolor 背景色 "white""transparent"
stylesheet CSS 样式表 URL(SVG 输出)
digraph GraphDecorations {
    // 标题设置
    label="系统架构图"
    labelloc=t        // 顶部
    labeljust=c       // 居中
    fontname="Microsoft YaHei"
    fontsize=18
    fontcolor="#1976D2"

    // 尺寸与间距
    size="10,7"
    dpi=150
    pad=0.5
    nodesep=0.5
    ranksep=0.8

    // 背景
    bgcolor="#FAFAFA"

    node [fontname="Microsoft YaHei" shape=box style=rounded]

    A -> B -> C -> D
}

8.3 节点 (Node) 属性

外观属性

属性 说明 示例值
shape 形状 boxellipsecirclediamond
style 样式 filledroundeddashedboldinvis
fillcolor 填充色 "#E3F2FD""lightblue"
color 边框色 "#1976D2""blue"
penwidth 边框宽度 123
peripheries 边框层数 12(双圈)

文本属性

属性 说明 示例值
label 标签 任意文本、HTML
fontname 字体 "Microsoft YaHei"
fontsize 字号 1112
fontcolor 字体颜色 "black""#333"
nojustify 不对齐 truefalse

尺寸属性

属性 说明 示例值
width 宽度(英寸) 1.5
height 高度(英寸) 0.8
fixedsize 固定尺寸 truefalseshape
margin 内边距 0.1"0.2,0.1"

位置属性

属性 说明 示例值
pos 位置坐标 "1.0,2.0""1.0,2.0!"
pin 固定位置 truefalse
group 分组 字符串标识
tooltip 鼠标提示 任意文本

8.4 边 (Edge) 属性

外观属性

属性 说明 示例值
style 线型 soliddasheddottedboldtapered
color 颜色 "#666""red""red:blue"(渐变)
penwidth 线宽 123
fillcolor 填充色 tapered 边的填充色

箭头属性

属性 说明 示例值
arrowhead 头部箭头 normalveediamondnone
arrowtail 尾部箭头 同上
arrowsize 箭头大小 0.81.01.5
dir 方向 forwardbackbothnone

布局属性

属性 说明 示例值
constraint 是否约束排名 truefalse
weight 权重 110
minlen 最小长度 123
samehead 同头约束 字符串
sametail 同尾约束 字符串
tailport 尾端口 nsew、端口名
headport 头端口 同上

8.5 颜色系统

颜色格式

Graphviz 支持多种颜色格式:

格式 示例 说明
命名颜色 redbluelightgray X11 颜色名
十六进制 RGB "#FF5733" 标准十六进制
十六进制 RGBA "#FF5733AA" 带透明度(2.42+)
HSV "0.5,0.8,0.9" 色相/饱和度/亮度
带透明度 "red;0.5" 50% 透明的红色

渐变色

digraph GradientColors {
    node [fontname="Microsoft YaHei" shape=box style=filled]

    // 渐变填充(支持多个颜色)
    g1 [fillcolor="blue:red" style=filled label="蓝→红渐变"]
    g2 [fillcolor="yellow:green:blue" style=filled label="三色渐变"]
    g3 [fillcolor="#E3F2FD:#1976D2" style=filled label="浅蓝→深蓝"]

    // 渐变边
    g1 -> g2 [color="red:blue" penwidth=3]
}

常用配色方案

Material Design 配色

digraph MaterialColors {
    node [fontname="Microsoft YaHei" shape=box style=filled fontsize=10]

    // Blue
    b100 [fillcolor="#BBDEFB" label="Blue 100"]
    b500 [fillcolor="#2196F3" label="Blue 500" fontcolor=white]
    b900 [fillcolor="#0D47A1" label="Blue 900" fontcolor=white]

    // Green
    g100 [fillcolor="#C8E6C9" label="Green 100"]
    g500 [fillcolor="#4CAF50" label="Green 500" fontcolor=white]
    g900 [fillcolor="#1B5E20" label="Green 900" fontcolor=white]

    // Red
    r100 [fillcolor="#FFCDD2" label="Red 100"]
    r500 [fillcolor="#F44336" label="Red 500" fontcolor=white]
    r900 [fillcolor="#B71C1C" label="Red 900" fontcolor=white]

    // Orange
    o100 [fillcolor="#FFE0B2" label="Orange 100"]
    o500 [fillcolor="#FF9800" label="Orange 500" fontcolor=white]
    o900 [fillcolor="#E65100" label="Orange 900" fontcolor=white]

    b100 -> b500 -> b900
    g100 -> g500 -> g900
    r100 -> r500 -> r900
    o100 -> o500 -> o900
}

8.6 字体系统

字体属性设置

digraph FontDemo {
    // 全局字体
    graph [fontname="Microsoft YaHei"]
    node  [fontname="Microsoft YaHei"]
    edge  [fontname="Microsoft YaHei"]

    // 各种字体样式
    normal [label="正常文本"]
    bold   [label=<粗体: <B>加粗文本</B>>]
    italic [label=<斜体: <I>斜体文本</I>>]
    sized  [label=<字号 14: <FONT POINT-SIZE="14">大字</FONT>>]
    colored [label=<彩色: <FONT COLOR="red">红</FONT> <FONT COLOR="blue">蓝</FONT>>]

    normal -> bold -> italic -> sized -> colored
}

字体栈配置

digraph FontStack {
    // 使用字体栈(优先使用第一个,找不到则回退)
    node [fontname="Microsoft YaHei, Noto Sans CJK SC, WenQuanYi Zen Hei, sans-serif"]
    edge [fontname="Microsoft YaHei, Noto Sans CJK SC, sans-serif"]

    A [label="中英文混排 English Chinese"]
    B [label="日文: こんにちは"]
    C [label="特殊字符: © ® ™ ♠ ♥"]

    A -> B -> C
}

8.7 样式组合技巧

状态指示器样式

digraph StatusIndicators {
    node [fontname="Microsoft YaHei" shape=circle style=filled fontsize=10 width=1.2 fixedsize=true]

    // 成功状态 — 绿色
    success [label="✓" fillcolor="#C8E6C9" color="#388E3C"]

    // 警告状态 — 橙色
    warning [label="!" fillcolor="#FFE0B2" color="#FF9800"]

    // 错误状态 — 红色
    error [label="✗" fillcolor="#FFCDD2" color="#C62828"]

    // 信息状态 — 蓝色
    info [label="i" fillcolor="#BBDEFB" color="#1976D2"]

    // 等待状态 — 灰色
    pending [label="…" fillcolor="#ECEFF1" color="#546E7A"]

    success -> warning -> error -> info -> pending
}

交互样式

digraph InteractionStyles {
    node [fontname="Microsoft YaHei" shape=box fontsize=10 style=filled]

    hover [label="鼠标悬停" tooltip="这是一个提示" fillcolor="#E3F2FD" color="#1976D2"]
    link [label="可点击" URL="https://example.com" fillcolor="#E8F5E9" color="#388E3C" target="_blank"]
    tooltip [label="有提示" tooltip="详细信息在这里" fillcolor="#FFF3E0" color="#FF9800"]

    hover -> link -> tooltip
}

8.8 业务场景:配置管理图

digraph ConfigManagement {
    rankdir=TB
    label="应用配置管理"
    labelloc=t
    fontname="Microsoft YaHei"
    fontsize=16
    fontcolor="#1976D2"
    bgcolor="#FAFAFA"
    pad=0.5

    node [fontname="Microsoft YaHei" fontsize=11]
    edge [fontname="Microsoft YaHei" fontsize=9 color="#666666"]

    // 配置源
    subgraph cluster_sources {
        label="配置源"
        style="filled,rounded"
        fillcolor="#E3F2FD"
        color="#1976D2"

        Env [shape=note style=filled fillcolor="#BBDEFB" label="环境变量"]
        YAML [shape=note style=filled fillcolor="#BBDEFB" label="YAML 文件"]
        Consul [shape=box3d style=filled fillcolor="#BBDEFB" label="Consul"]
    }

    // 配置处理
    subgraph cluster_process {
        label="配置处理"
        style="filled,rounded"
        fillcolor="#E8F5E9"
        color="#388E3C"

        Load [shape=box style="filled,rounded" fillcolor="#C8E6C9" label="加载器"]
        Validate [shape=diamond style=filled fillcolor="#C8E6C9" label="校验"]
        Merge [shape=box style="filled,rounded" fillcolor="#C8E6C9" label="合并"]
    }

    // 应用
    subgraph cluster_apps {
        label="应用实例"
        style="filled,rounded"
        fillcolor="#FFF3E0"
        color="#FF9800"

        App1 [shape=component style=filled fillcolor="#FFE0B2" label="App 1"]
        App2 [shape=component style=filled fillcolor="#FFE0B2" label="App 2"]
        App3 [shape=component style=filled fillcolor="#FFE0B2" label="App 3"]
    }

    Env -> Load [label="读取"]
    YAML -> Load
    Consul -> Load [label="监听"]
    Load -> Validate [label="原始配置"]
    Validate -> Merge [label="有效" color="#388E3C"]
    Validate -> Load [label="无效\n重试" color="#F44336" style=dashed constraint=false]
    Merge -> {App1 App2 App3} [label="推送"]
}

注意事项

⚠️ 属性继承node [color=red] 设置的是后续所有节点的默认值,不影响之前声明的节点。

⚠️ 颜色引号:命名颜色(red)不需要引号,十六进制颜色("#FF0000")需要引号。

⚠️ fontname 可用性:字体名必须在系统中存在,否则可能回退到默认字体。

⚠️ HTML 标签优先:使用 label=<...> 时,节点的 fontname/fontsize/fontcolor 属性被忽略,需在 HTML 内部指定。

⚠️ 渐变色支持:渐变色(多色填充)在不同输出格式中的渲染可能不同。

⚠️ 透明度:RGBA 格式(#RRGGBBAA)需要 Graphviz 2.42+。


扩展阅读


下一章09 - 高级特性 — HTML 表格、图片嵌入、端口与排名约束。