digraph例子

digraph G {
  rankdir=LR;
  node [style=filled, fillcolor=lightblue, shape=box, fontname="Microsoft YaHei"];
  edge [color=gray, fontname="Microsoft YaHei", fontsize=11];

  "n.jello.top" [fillcolor=lightyellow, shape=note];

  "n.jello.top" -> nginx [label="HTTPS"];
  nginx -> nps [label="proxy_pass :18080"];
  nps -> npc [label="隧道"];
  npc -> edith [label=":8080"];

  nginx [fillcolor=lightgreen];
  nps [fillcolor=lightgreen];
  npc [fillcolor=lightgreen];
  edith [fillcolor=lightpink, style="filled,bold"];

  subgraph cluster_tencent {
    label="腾讯云";
    style=dashed;
    color=gray;
    fontname="Microsoft YaHei";
    nginx; nps;
  }

  subgraph cluster_router {
    label="软路由";
    style=dashed;
    color=gray;
    fontname="Microsoft YaHei";
    npc; edith;
  }
}

基本概念

概念 说明
节点 (Node) 图中的顶点,可设置形状、颜色、标签等
边 (Edge) 节点之间的连线,可设置方向、线型、颜色等
图 (Graph) 节点的集合,有有向图和无向图两种
子图 (Subgraph) 图的子集,可用于分组展示

DOT 语言基础

有向图 digraph

digraph G {
    // 节点定义
    A;
    B;

    // 边:A -> B 表示从 A 指向 B
    A -> B;
}

无向图 graph

graph G {
    A -- B;
}

节点 (Node)

基本节点

digraph {
    node1;              // 节点标签 = "node1"
    node2 [label="自定义标签"];
    node3 [label="带颜色的", style=filled, fillcolor=lightblue];
}

常见形状

digraph {
    a [shape=box,       label="矩形"];
    b [shape=ellipse,   label="椭圆 (默认)"];
    c [shape=diamond,   label="菱形"];
    d [shape=parallelogram, label="平行四边形"];
    e [shape=hexagon,   label="六边形"];
    f [shape=circle,    label="圆"];
    g [shape=record,    label="{记录|A|B}"];
    h [shape=plaintext, label="纯文本 (无边框)"];
    i [shape=note,      label="便签"];
    j [shape=Mrecord,   label="{圆角记录|X|Y}"];
}

记录型节点 (record)

digraph {
    node [
        shape=record,
        style=filled,
        fillcolor=lightyellow
    ];

    struct1 [label="<f0> 左 | <f1> 中 | <f2> 右"];
    struct2 [label="<f0> 头 | { <f1> 左 | <f2> 右 }"];

    // 通过端口引用连接的特定位置
    struct1:f0 -> struct2:f1;
    struct1:f1 -> struct2:f2;
    struct1:f2 -> struct2:f0;
}

HTML 标签

支持类 HTML 语法设置复杂标签,需 digraph 且 label=<...>:

digraph {
    node [shape=none, margin=0];

    tbl [
        label=<
            <table border="1" cellspacing="0" cellpadding="5">
                <tr><td bgcolor="lightblue">姓名</td><td>张三</td></tr>
                <tr><td bgcolor="lightblue">年龄</td><td>30</td></tr>
            </table>
        >
    ];
}

边 (Edge)

基本边

digraph {
    A -> B;                    // 实线箭头
    A -> C [style=dashed];     // 虚线箭头
    A -> D [style=dotted];     // 点线箭头
    A -> E [style=bold];       // 粗线箭头
    A -> F [color=red];        // 红色箭头
}

边的属性

digraph {
    A -> B [
        label="边标签",         // 边上的文字
        color=blue,             // 颜色
        fontcolor=red,          // 标签文字颜色
        style=dashed,           // 线型:dashed/dotted/bold/solid
        penwidth=2.0,           // 线宽
        arrowhead=diamond,      // 箭头形状
        dir=both,               // 双向箭头
    ];
}

常见线型与箭头

箭头形状 值
普通 normal (默认)
空心 vee, open
菱形 diamond
圆点 dot
无箭头 none
反方向 inv

全局属性设置

graph / node / edge 关键字

digraph {
    // 图级属性
    rankdir=LR;               // 布局方向:TB(上下,默认) / LR(左右) / BT / RL
    splines=ortho;            // 连线风格:ortho(直角) / curved(曲线) / polyline
    bgcolor=lightyellow;      // 背景色
    label="我的图";            // 图标题
    fontsize=20;

    // 所有节点的默认属性
    node [
        shape=box,
        style=filled,
        fillcolor=lightblue,
        fontname="Microsoft YaHei"
    ];

    // 所有边的默认属性
    edge [
        color=gray,
        arrowhead=vee
    ];

    A -> B -> C -> D;
}

子图与集群 (Subgraph & Cluster)

子图名前缀 cluster_ 会产生一个带边框的簇:

digraph {
    rankdir=LR;

    subgraph cluster_0 {
        label="模块 A";
        style=filled;
        fillcolor=lightgrey;
        a1 -> a2;
    }

    subgraph cluster_1 {
        label="模块 B";
        style=filled;
        fillcolor=lightyellow;
        b1 -> b2;
    }

    a2 -> b1 [label="跨模块调用"];
}

非 cluster_ 前缀的子图不产生边框,但可以约束布局(如 rank=same):

digraph {
    // 将 d 和 e 放在同一层
    { rank=same; d; e; }

    a -> b -> c;
    d -> e -> f;
    a -> d;
}

常用属性

节点属性

属性 说明 示例值
label 标签文字 "节点A"
shape 形状 box, ellipse, circle, diamond, record
style 样式 filled, dashed, bold, rounded
fillcolor 填充色 lightblue, "#FF0000"
color 边框/线颜色 red, blue, "#333333"
fontcolor 文字颜色 white, black
fontname 字体 "Microsoft YaHei"
fontsize 字号 12
width / height 宽高 1.5
penwidth 边框线宽 2.0

边属性

属性 说明 示例值
label 边标签 "调用"
color 颜色 red
style 线型 dashed, dotted, bold
penwidth 线宽 2.0
arrowhead 箭头形状 normal, vee, diamond, dot, none
arrowtail 尾端箭头 dot
dir 方向 forward, back, both, none
constraint 是否影响层级 true / false

图属性

属性 说明 示例值
rankdir 布局方向 TB, LR, BT, RL
ranksep 层间距 0.5
nodesep 节点水平间距 0.5
splines 连线风格 ortho, curved, polyline, spline
bgcolor 背景色 white, transparent
label 图标题 "流程图"
compound 允许边连接簇 true
concentrate 合并公共边 true
dpi 分辨率 300
ratio 比例 fill, compress, auto

案例

基础流程图

digraph flowchart {
    rankdir=TB;
    node [shape=box, style=rounded, fontname="Microsoft YaHei"];

    start [label="开始", shape=ellipse, style=filled, fillcolor=lightgreen];
    input [label="输入数据"];
    check [label="数据校验", shape=diamond, style=filled, fillcolor=lightyellow];
    process [label="处理数据"];
    output [label="输出结果", shape=ellipse, style=filled, fillcolor=lightcoral];
    err [label="错误提示", shape=box, style=filled, fillcolor=red, fontcolor=white];

    start -> input;
    input -> check;
    check -> process [label="通过"];
    check -> err [label="不通过"];
    process -> output;
}

有限状态机

digraph fsm {
    rankdir=LR;
    node [shape=circle, fontname="Microsoft YaHei"];

    // 初始状态用箭头进入
    idle [label="空闲", style=filled, fillcolor=lightgreen];
    running [label="运行中"];
    paused [label="暂停"];
    stopped [label="已停止", style=filled, fillcolor=lightcoral];

    start_point [shape=point, width=0];
    start_point -> idle;

    idle -> running [label="启动"];
    running -> paused [label="暂停"];
    paused -> running [label="恢复"];
    running -> stopped [label="停止"];
    idle -> stopped [label="强制终止"];
}

树形结构

digraph tree {
    rankdir=TB;
    node [shape=box, style=rounded, fontname="Microsoft YaHei"];

    root [label="根节点", style=filled, fillcolor=orange];

    root -> {
        left,
        right
    };

    left [label="左子节点", style=filled, fillcolor=lightblue];
    right [label="右子节点", style=filled, fillcolor=lightblue];

    left -> ll [label="左"];
    left -> lr [label="右"];
    right -> rl [label="左"];
    right -> rr [label="右"];

    ll [label="LL"];
    lr [label="LR"];
    rl [label="RL"];
    rr [label="RR"];
}

网络拓扑

graph network {
    rankdir=LR;
    node [shape=box, style=rounded, fontname="Microsoft YaHei"];

    subgraph cluster_internet {
        label="互联网";
        gw [label="网关", shape=hexagon, style=filled, fillcolor=orange];
    }

    subgraph cluster_wan {
        label="内网";
        fw [label="防火墙", shape=hexagon, style=filled, fillcolor=yellow];

        subgraph cluster_dmz {
            label="DMZ";
            web [label="Web 服务器"];
        }

        subgraph cluster_lan {
            label="内部网络";
            db [label="数据库"];
            app [label="应用服务器"];
        }

        gw -- fw;
        fw -- web;
        fw -- app;
        app -- db;
    }
}

类图(使用 record)

digraph uml {
    rankdir=BT;
    node [shape=record, fontname="Microsoft YaHei"];

    Animal [
        label="{Animal|+ name : string\l+ age : int\l|+ eat()\l+ sleep()\l}"
    ];

    Dog [
        label="{Dog|+ breed : string\l|+ bark()\l}"
    ];

    Cat [
        label="{Cat|+ color : string\l|+ meow()\l}"
    ];

    // 空心三角箭头表示继承
    edge [arrowhead=onormal, style=dashed];
    Dog -> Animal [label="继承"];
    Cat -> Animal [label="继承"];
}

注释

  • 单行注释//,# 。

  • 多行注释/* ... */ 。