記一次prefuse應用實踐(數據讀入部分詳細說明)

prefuse是一個Java可視化工具包。優勢是功能強大,缺點是官網文檔不全&代碼沒註釋(要命)……
花了兩天時間熟悉了操做並寫完了本身的工程,專門來寫篇文,但願能幫上某些掉坑的小夥伴www。html

一、如何獲取與使用

Github地址在這裏,直接clone下來,運行build腳本,會得到三個jar包,把其中的prefuse.jar加入工程Java Build Path便可。java

二、數據讀入

介紹三種方式:node

  • 常見的xml格式數據的讀入,詳見prefuse源碼裏的GraphView.java文件(由於其餘demo我沒看)。
  • 直接輸入數據(主要介紹的部分)。
  • 數據庫讀入(見大神博客)。

2.1 xml數據讀入

沒什麼好說的,按照demo裏的socialnet.xml來,若是須要有向圖,能夠在文件開頭設置<graph edgedefault="directed">git

2.2 直接輸入讀入

假設咱們須要的作一張有向圖,由點和邊組成,點是名字(惟一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);

其餘

這部分代碼畫畫流程圖確定夠了,其餘更高深的功能兄弟們本身探♂索吧。我用不上因此沒接着看下去……測試

相關文章
相關標籤/搜索