当前位置: 首页 > 工具软件 > prefuse > 使用案例 >

记一次prefuse应用实践(数据读入部分详细说明)

蔺德曜
2023-12-01

prefuse是一个Java可视化工具包。优点是功能强大,缺点是官网文档不全&代码没注释(要命)……
花了两天时间熟悉了操作并写完了自己的工程,专门来写篇文,希望能帮上某些掉坑的小伙伴www。

1、如何获取与使用

Github地址在这里,直接clone下来,运行build脚本,会获得三个jar包,把其中的prefuse.jar加入工程Java Build Path即可。

2、数据读入

介绍三种方式:

  • 常见的xml格式数据的读入,详见prefuse源码里的GraphView.java文件(因为其他demo我没看)。
  • 直接输入数据(主要介绍的部分)。
  • 数据库读入(见大神博客)。

2.1 xml数据读入

没什么好说的,按照demo里的socialnet.xml来,如果需要有向图,可以在文件开头设置<graph edgedefault="directed">

2.2 直接输入读入

假设我们需要的做一张有向图,由点和边组成,点是名字(唯一name)。

先读入点:

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));
}

3、展示

这部分就很简单了。大部分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);

其他

这部分代码画画流程图肯定够了,其他更高深的功能兄弟们自己探♂索吧。我用不上所以没接着看下去……

 类似资料: