绘制图形的DOT语言简介

  1. DOT语言简介(摘抄自维基百科)

  2. DOT语言是一种文本图形描述语言。它提供了一种简单的描述图形的方法,并且可以为人类和计算机程序所理解。DOT语言文件通常是具有.gv或是.dot的文件扩展名。

    很多程序都可以处理DOT文件。其中的一些,例如dot,neato,twopi,circo, fdp与sfdp,会读取DOT文件并将之渲染成为图形格式。其它的一些,比如gvpr,gc,accyclic,ccomps,sccmap和tred,可以读取DOT文件并对它代表的图形进行一些处理。类似于GVedit,lefty,dotty和grappa则提供了交互式的界面。以上程序大部分都包括在了Graphviz软件包中。

  3. 绘图实例

    • 示例一(cluster)

    代码(保存为cluster.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
    
    digraph G {
     
    subgraph cluster_0 {
    style=filled;
    color=lightgrey;
    node [style=filled,color=white];
    a0 -> a1 -> a2 -> a3;
    label = "process #1";
    }
     
    subgraph cluster_1 {
    node [style=filled];
    b0 -> b1 -> b2 -> b3;
    label = "process #2";
    color=blue
    }
    start -> a0;
    start -> b0;
    a1 -> b3;
    b2 -> a3;
    a3 -> a0;
    a3 -> end;
    b3 -> end;
     
    start [shape=Mdiamond];
    end [shape=Msquare];
    }

    运行(在终端中运行):

    1
    
    dot -Tpng cluster.dot -o cluster.png

    输出:
    cluster

    • 示例二(unix)

    代码:

    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
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    
    digraph unix {
    size="6,6";
    node [color=lightblue2, style=filled];
    "5th Edition" -> "6th Edition";
    "5th Edition" -> "PWB 1.0";
    "6th Edition" -> "LSX";
    "6th Edition" -> "1 BSD";
    "6th Edition" -> "Mini Unix";
    "6th Edition" -> "Wollongong";
    "6th Edition" -> "Interdata";
    "Interdata" -> "Unix/TS 3.0";
    "Interdata" -> "PWB 2.0";
    "Interdata" -> "7th Edition";
    "7th Edition" -> "8th Edition";
    "7th Edition" -> "32V";
    "7th Edition" -> "V7M";
    "7th Edition" -> "Ultrix-11";
    "7th Edition" -> "Xenix";
    "7th Edition" -> "UniPlus+";
    "V7M" -> "Ultrix-11";
    "8th Edition" -> "9th Edition";
    "1 BSD" -> "2 BSD";
    "2 BSD" -> "2.8 BSD";
    "2.8 BSD" -> "Ultrix-11";
    "2.8 BSD" -> "2.9 BSD";
    "32V" -> "3 BSD";
    "3 BSD" -> "4 BSD";
    "4 BSD" -> "4.1 BSD";
    "4.1 BSD" -> "4.2 BSD";
    "4.1 BSD" -> "2.8 BSD";
    "4.1 BSD" -> "8th Edition";
    "4.2 BSD" -> "4.3 BSD";
    "4.2 BSD" -> "Ultrix-32";
    "PWB 1.0" -> "PWB 1.2";
    "PWB 1.0" -> "USG 1.0";
    "PWB 1.2" -> "PWB 2.0";
    "USG 1.0" -> "CB Unix 1";
    "USG 1.0" -> "USG 2.0";
    "CB Unix 1" -> "CB Unix 2";
    "CB Unix 2" -> "CB Unix 3";
    "CB Unix 3" -> "Unix/TS++";
    "CB Unix 3" -> "PDP-11 Sys V";
    "USG 2.0" -> "USG 3.0";
    "USG 3.0" -> "Unix/TS 3.0";
    "PWB 2.0" -> "Unix/TS 3.0";
    "Unix/TS 1.0" -> "Unix/TS 3.0";
    "Unix/TS 3.0" -> "TS 4.0";
    "Unix/TS++" -> "TS 4.0";
    "CB Unix 3" -> "TS 4.0";
    "TS 4.0" -> "System V.0";
    "System V.0" -> "System V.2";
    "System V.2" -> "System V.3";
    }

    运行:

    1
    
    dot -Tpng unix.dot -o unix.png

    输出:
    unix

  4. 扩展阅读

    • 中文

    DOT语言(维基百科)
    dot 画图语言介绍
    dot 学习笔记(一. 基础概念)
    dot 学习笔记(二. 记录 Shell 的变迁)

    • 英文

    The DOT Language
    Graphviz – Graph Visualization Software(官网)
    Gallery(示例)
    Documentation(文档)