简单介绍GraphViz的使用以及Java调用GraphViz的方法
Graphviz的是AT&T Labs Research开发的图形绘制工具,他可以很方便的用来绘制结构化的图形网络,支持多种格式输出,生成图片的质量和速度都不错.Graphviz本身是开源的产品,下载可以到 这里,以及他的演示界面 Graphviz在windows上和Linux上都可以顺利运行. —— [ 百度百科 ]
官方下载:http://www.graphviz.org/Download..php
安装:傻瓜式安装,一路next(设置路径),记住安装的路径配置环境变量(配置环境变量的目的是cmd下能直接使用,也可以不用配置)
配置环境变量:与java环境变量的配置一样,将安装目录+bin目录添加到path中即可(主要用到了bin目录下的dot.exe程序)
编辑dot脚本(此处做简单的示范,dot脚本大家可以百度下,还是比较简单的)
就是这么简单,更多功能大家自己发掘哦,可以更改结点显示的样式,弧的样式,此处就不示范了。
接下来介绍下用cmd执行dot.exe文件生成网络图
首先编写脚本文件:
digraph G {
node [style=filled color="#C0FF3E"]
edge [color="sienna" fontcolor="green"]
A -> B -> C[label=a style=dashed arrowtail=diamond];
A -> D [arrowhead = box];
A -> H -> A;
C -> E[label=1 style=dashed arrowtail=diamond];
C -> F [shape=box];
D -> E[label=2 style=dashed arrowtail=diamond];
A -> F[dir=none];
C -> G[dir=both];
}
在该文件目录下执行dot命令(我在E根目录下测试):
执行结果:
Java调用GraphViz主要是用Java的Runtime.exec调用之前提到的安装目录下的dot.exe函数,当然调用的时候要加上参数喽
调用命令:dot in.txt -T gif -o out.gif
in.txt 输入的dot脚本,就是之前编辑器里边写的东西
-o out.gif 输出名为out的gif图片
以下附上简单的调用API,复制到工程中可以直接使用
测试GraphViz代码:
package GraphVizTest;
import java.io.IOException;
public class GraphVizTest {
public static void main(String[] args) {
GraphViz gViz=new GraphViz("C:\\Users\\Baratta-Desktoop\\Desktop\\eee", "D:\\Graphviz2.38\\bin\\dot.exe");
gViz.start_graph();
gViz.addln("A->B;");
gViz.addln("A->C;");
gViz.addln("C->B;");
gViz.addln("B->D;");
gViz.addln("C->E;");
gViz.end_graph();
try {
gViz.run();
} catch (Exception e) {
e.printStackTrace();
}
}
}
GraphViz调用API代码:
package GraphVizTest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
class GraphViz{
private String runPath = "";
private String dotPath = "";
private String runOrder="";
private String dotCodeFile="dotcode.txt";
private String resultGif="dotGif";
private StringBuilder graph = new StringBuilder();
Runtime runtime=Runtime.getRuntime();
public void run() {
File file=new File(runPath);
file.mkdirs();
writeGraphToFile(graph.toString(), runPath);
creatOrder();
try {
runtime.exec(runOrder);
} catch (IOException e) {
e.printStackTrace();
}
}
public void creatOrder(){
runOrder+=dotPath+" ";
runOrder+=runPath;
runOrder+="\\"+dotCodeFile+" ";
runOrder+="-T gif ";
runOrder+="-o ";
runOrder+=runPath;
runOrder+="\\"+resultGif+".gif";
System.out.println(runOrder);
}
public void writeGraphToFile(String dotcode, String filename) {
try {
File file = new File(filename+"\\"+dotCodeFile);
if(!file.exists()){
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(dotcode.getBytes());
fos.close();
} catch (java.io.IOException ioe) {
ioe.printStackTrace();
}
}
public GraphViz(String runPath,String dotPath) {
this.runPath=runPath;
this.dotPath=dotPath;
}
public void add(String line) {
graph.append("\t"+line);
}
public void addln(String line) {
graph.append("\t"+line + "\n");
}
public void addln() {
graph.append('\n');
}
public void start_graph() {
graph.append("digraph G {\n") ;
}
public void end_graph() {
graph.append("}") ;
}
}