prefuse
是一个Java可视化工具包。优势是功能强大,缺点是官网文档不全&代码没注释(要命)……
花了两天时间熟悉了操做并写完了本身的工程,专门来写篇文,但愿能帮上某些掉坑的小伙伴www。html
Github地址在这里,直接clone下来,运行build脚本,会得到三个jar包,把其中的prefuse.jar加入工程Java Build Path便可。java
介绍三种方式:node
- 常见的xml格式数据的读入,详见prefuse源码里的GraphView.java文件(由于其余demo我没看)。
- 直接输入数据(主要介绍的部分)。
- 数据库读入(见大神博客)。
没什么好说的,按照demo里的socialnet.xml来,若是须要有向图,能够在文件开头设置<graph edgedefault="directed">
git
假设咱们须要的作一张有向图,由点和边组成,点是名字(惟一name
)。github
先读入点:数据库
HashMap<String, Integer> nodesRow = new HashMap<String, Integer>(); // 先建立表头,经测试不支持自定义类型 Schema n_sch = new Schema(); n_sch.addColumn("point", String.class); n_sch.lockSchema(); // 而后根据表头生成表 Table nodes = n_sch.instantiate(); // 而后存入点的数据 for (int i = 0; i < points.length; i++) { // 很奇葩,须要先建立一个空行,而后返回行号 int rid = nodes.addRow(); // 而后根据行号和列名设置对应数值 nodes.set(rid, "point", points[i]); // 记得把行号记下来,由于prefuse的边是用行号来惟一指定节点的,也很奇葩 nodesRow.put(points[i], rid); } // 同理,建立边的表 Schema e_sch = new Schema(); e_sch.addColumn("from", int.class); e_sch.addColumn("to", int.class); Table arrows = e_sch.instantiate(); // 而后存入边的数据 for (int i = 0; i < edges.length; i++) { int rid = arrows.addRow(); arrows.set(rid, "from", nodesRow.get(edges[i].from)); arrows.set(rid, "to", nodesRow.get(edges[i].to)); }
这部分就很简单了。大部分copy了大神博客里的代码,修改了一些,看看就好。工具
// 建立有向图(几个参数分别表示:点,边,是否有向,边的source列名,边的target列名) Graph g = new Graph(nodes, arrows, true, "from", "to"); // 建立可视化对象 Visualization vis = new Visualization(); vis.add("graph", g); // 设置点上面显示哪一个字段的值,固然是points里的point列 LabelRenderer label = new LabelRenderer("point"); // 圆角 label.setRoundedCorner(10, 10); vis.setRendererFactory(new DefaultRendererFactory(label)); // 设置点的颜色 ColorAction node_fill = new ColorAction("graph.nodes", VisualItem.FILLCOLOR, ColorLib.rgb(200, 200, 200)); ColorAction node_text = new ColorAction("graph.nodes", VisualItem.TEXTCOLOR, ColorLib.rgb(0, 0, 0)); ColorAction node_other = new ColorAction("graph.nodes", VisualItem.STROKECOLOR, 0); // 设置边的颜色 ColorAction edge_text = new ColorAction("graph.edges", VisualItem.TEXTCOLOR, ColorLib.rgb(0, 0, 0)); ColorAction edge_fill = new ColorAction("graph.edges", VisualItem.FILLCOLOR, ColorLib.rgb(20, 100, 100)); ColorAction edge_other = new ColorAction("graph.edges", VisualItem.STROKECOLOR, ColorLib.rgb(20, 100, 100)); // 颜色指令 ActionList color = new ActionList(); color.add(node_fill); color.add(node_text); color.add(node_other); color.add(edge_text); color.add(edge_fill); color.add(edge_other); // 画图指令 ActionList layout = new ActionList(Activity.INFINITY); layout.add(new ForceDirectedLayout("graph")); layout.add(new RepaintAction()); // 把指令告诉可视化对象 vis.putAction("color", color); vis.putAction("layout", layout); // 生成Java GUI对象 Display display = new Display(vis); display.setSize(800, 600); display.pan(250, 250); display.addControlListener(new DragControl()); display.addControlListener(new PanControl()); display.addControlListener(new ZoomControl()); display.addControlListener(new WheelZoomControl()); display.addControlListener(new FocusControl(1)); display.addControlListener(new ZoomToFitControl()); // 在一个JFrame上显示出来 JFrame jf = new JFrame(); jf.setSize(800, 600); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.add(display); // 记得运行画图指令 vis.run("color"); vis.run("layout"); // 最后设置为可见便可 jf.setVisible(true);
这部分代码画画流程图确定够了,其余更高深的功能兄弟们本身探♂索吧。我用不上因此没接着看下去……测试