Graphviz佈局程序以簡單的文本語言獲取圖形描述,並以有用的格式製作圖表,例如用於網頁的圖像和SVG;PDF或Postscript包含在其他文檔中;或在交互式圖形瀏覽器中顯示。Graphviz具有用於具體圖表的許多有用功能,例如顏色,字體,表格節點佈局,線型,超鏈接和自定義形狀的選項。
開源項目地址:https://github.com/nidi3/graphviz-java
狀態圖繪製工具maven引入jar包:
<!--狀態循環圖繪製工具-->
<dependency>
<groupId>guru.nidi</groupId>
<artifactId>graphviz-java</artifactId>
<version>0.16.0</version>
</dependency>
樣例一:【hello world】
Graph g = graph("example1") // 圖形名稱
.directed() // 普通單向箭頭
.with(node("a").link(node("b"))); // 節點“a”連接節點“b”
Graphviz.fromGraph(g) // 解析圖形
.width(200) // 圖片寬度
.render(Format.PNG) // 圖片樣式
.toFile(new File("E:\\images\\ex1.png")); // 輸出圖片
樣例二:【多樣式圖】
Node
init = node("init"),
execute = node("execute"),
compare = node("compare")
// 形狀長方形,樣式填充,填充顏色
.with(Shape.RECTANGLE, Style.FILLED, Color.hsv(.7, .3, 1.0)),
mkString = node("mkString")
// 節點顯示標籤內容
.with(Label.of("make a\nstring")),
printf = node("printf");
Graph g = graph("example2").directed().with(
// 開始節點爲 長方形
node("main").with(Shape.RECTANGLE)
.link(
to(node("parse").link(execute))
.with("weight", 8),
to(init).with(Style.DOTTED),// 虛線
node("cleanup"),
to(printf).with(Style.BOLD, Label.of("100 times"), Color.RED)),// 加粗,跳轉內容,紅色
execute.link(
graph().with(mkString, printf),
to(compare).with(Color.RED)),
init.link(mkString));
Graphviz.fromGraph(g).width(900).render(Format.PNG).toFile(new File("E:\\images\\ex2.png"));
樣列三:【橫向圖】
Node
node0 = node("node0").with(Records.of(rec("f0", ""), rec("f1", ""), rec("f2", ""), rec("f3", ""), rec("f4", ""))),
node1 = node("node1").with(Records.of(turn(rec("n4"), rec("v", "719"), rec("")))),
node2 = node("node2").with(Records.of(turn(rec("a1"), rec("805"), rec("p", "")))),
node3 = node("node3").with(Records.of(turn(rec("i9"), rec("718"), rec("")))),
node4 = node("node4").with(Records.of(turn(rec("e5"), rec("989"), rec("p", "")))),
node5 = node("node5").with(Records.of(turn(rec("t2"), rec("v", "959"), rec("")))),
node6 = node("node6").with(Records.of(turn(rec("o1"), rec("794"), rec("")))),
node7 = node("node7").with(Records.of(turn(rec("s7"), rec("659"), rec(""))));
Graph g = graph("example3").directed()
.graphAttr().with(Rank.dir(LEFT_TO_RIGHT))
.with(
node0.link(
between(port("f0"), node1.port("v", SOUTH)),
between(port("f1"), node2.port(WEST)),
between(port("f2"), node3.port(WEST)),
between(port("f3"), node4.port(WEST)),
between(port("f4"), node5.port("v", NORTH))),
node2.link(between(port("p"), node6.port(NORTH_WEST))),
node4.link(between(port("p"), node7.port(SOUTH_WEST))));
Graphviz.fromGraph(g).width(900).render(Format.PNG).toFile(new File("E:\\images\\ex3.png"));}
樣例四:【實戰,動態生成狀態圖】
// 構造數據
List<Map<String, String>> mapList = new ArrayList<>();
Map<String, String> map = new HashMap<>(1);
// key是節點 value包含節點和 節點之間跳轉的信息
map.put("a", "b@a到b");
mapList.add(map);
Map<String, String> map1 = new HashMap<>(1);
map1.put("b","c@b到c");
mapList.add(map1);
Map<String, String> map2 = new HashMap<>(2);
map2.put("a","d@a到d");
map2.put("e",null);
mapList.add(map2);
MutableGraph g = mutGraph("lifeCycle").setDirected(true).use((gr, ctx) -> {
mapList.stream().forEach(list -> {
Iterator<Map.Entry<String, String>> iterator = list.entrySet().iterator();
while ( iterator.hasNext() ) {
Map.Entry<String, String> next = iterator.next();
String key = next.getKey();
String value = next.getValue();
// 這裏 value 中 包含了第二個節點和 節點之間跳轉的信息
String values = null;
String lable = null;
if(StrUtil.isNotEmpty(value)){
int i = value.indexOf("@");
values = StringUtils.substring(value, 0, i);
lable = StringUtils.substring(value, i + 1, value.length());
}
MutableNode add = null;
add = mutNode(key)
.add(Shape.ELLIPSE) // 選擇形狀
.add(Color.rgb(168, 214, 255)); // 設置顏色
if(StrUtil.isNotEmpty(value)){
//antd的藍色 24, 144, 255
MutableNode add2 = null;
add2 = mutNode(values)
.add(Shape.ELLIPSE) // 選擇形狀
.add(Color.rgb(168, 214, 255)) // 設置顏色
.add(Style.RADIAL); // 設置填充
add.addLink(to((add2)) // 節點之間對接
.add(Label.of(lable)) // 節點之間跳轉時候的標籤值
.add(Color.rgb(168, 214, 255))); // 顏色
}
}
});
});
// 輸出到本地
Graphviz.fromGraph(g).width(900).render(Format.PNG)
.toFile(new File("E:\\images\\ex4.png"));
// 輸出到網頁
// BufferedImage bufferedImage = Graphviz.fromGraph(g).render(Format.PNG).toImage();
// ByteArrayOutputStream bs =new ByteArrayOutputStream();
// ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
// ImageIO.write(bufferedImage,"png",imOut);
// InputStream inputStream =new ByteArrayInputStream(bs.toByteArray());