绘图工具:Graphviz

再介绍一个绘图工具:Graphviz

简介

官网 http://www.graphviz.org/

示例 https://www.tonyballantyne.com/graphs.html

VSCode 使用 Graphviz

Extensions 搜索 Graphviz,安装人气高的即可。注意可能不同插件使用方法不一样,参考安装说明,这里用的是 Graphviz Markdown Preview.

示例:

Sublime 使用 Graphviz

没有 VSCode 也可以使用 Sublime Text 3 来绘制 Graphviz

参考 https://blog.csdn.net/yao5hed/article/details/81055586

python 绘制 Graphviz

参考文档: https://pypi.org/project/graphviz/

安装

1
pip/pip3 install graphviz

简单粗暴,这几句就可以绘制一张 pdf

1
2
3
4
5
6
7
8
9
10
11
from graphviz import Digraph

dot = Digraph(comment='The Round Table')
dot.node('A', 'King Arthur')
dot.node('B', 'Sir Bedevere the Wise')
dot.node('L', 'Sir Lancelot the Brave')
dot.edges(['AB', 'AL'])
dot.edge('B', 'L', constraint='false')

print(dot.source) # doctest: +NORMALIZE_WHITESPACE
dot.render('test-output/round-table.gv', view=True) # doctest: +SKIP

效果:

Java 绘制 Graphviz

参考开源项目 https://github.com/nidi3/graphviz-java

依赖

1
2
3
4
5
<dependency>
<groupId>guru.nidi</groupId>
<artifactId>graphviz-java</artifactId>
<version>0.17.0</version>
</dependency>

代码:

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
package com.tracenote.tools;
import guru.nidi.graphviz.attribute.Color;
import guru.nidi.graphviz.attribute.Font;
import guru.nidi.graphviz.attribute.Rank;
import guru.nidi.graphviz.attribute.Style;
import guru.nidi.graphviz.engine.Format;
import guru.nidi.graphviz.engine.Graphviz;
import guru.nidi.graphviz.model.Graph;

import java.io.File;
import java.io.IOException;

import static guru.nidi.graphviz.attribute.Attributes.attr;
import static guru.nidi.graphviz.attribute.Rank.RankDir.LEFT_TO_RIGHT;
import static guru.nidi.graphviz.model.Factory.*;

public class GraphvizHelper {

public static void main(String[] args) throws IOException {
Graph g = graph("example1").directed()
.graphAttr().with(Rank.dir(LEFT_TO_RIGHT))
.nodeAttr().with(Font.name("arial"))
.linkAttr().with("class", "link-class")
.with(
node("a").with(Color.RED).link(node("b")),
node("b").link(
to(node("c")).with(attr("weight", 5), Style.DASHED)
)
);
Graphviz.fromGraph(g).height(100).render(Format.PNG).toFile(new File("example/ex1.png"));
}
}

效果: