53

Graphviz dot简明教程

 5 years ago
source link: https://jiajunhuang.com/articles/2019_04_20-graphviz_dot.md.html?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

graphviz是一个用特殊的文本语言来生成图片的应用,可以生成有向图和无向图。dot就是那个特殊的文本语言。

dot大概长这样:

graph {
    a -- b;
    c -- d;
}

生成图片:

$ dot -Tpng -o dot_demo.png dot_demo.dot

图片长这样:

YRNrQb2.png!web

安装

$ sudo pacman -S graphviz

语法

首先我们要声明一个图,可以使用 graph ,代表无向图,也可以使用 digraph 代表有向图。

graph graph_name {
    a -- b;
    c -- d;
}

graph_name 是可选的,如果没有,就是一个匿名图。其中, a , b , c , d 都是一个节点,可以使用 node 关键字来进行属性设定,例如上面的变成:

graph graph_name {
    node [color=Red];

    a -- b;
    c -- d;
}

生成的图片就会变成:

mM7Jjea.png!web

这些关键字包括:

node
edge

而常见的可以修饰的属性包括下面这些:

bgcolor
shape
size

详见: https://graphviz.gitlab.io/_pages/doc/info/attrs.html

标注和变量声明

有的时候我们需要在连线上做一些标注,或者是声明节点以便重复使用,看个例子就晓得了:

digraph graph_name {
    T [label="Teacher" color=Blue, fontcolor=Red, fontsize=24, shape=box]      // node T
    P [label="Pupil" color=Blue, fontcolor=Red, fontsize=24, shape=box]  // node P

    T->P [label="Instructions", fontcolor=darkgreen] // edge T->P
}

生成的图片是:

yuaQfqY.png!web

好了,就介绍这么多,其余的还是需要自己翻文档,不过这些知识已经够日常使用了。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK