漫談可視化Prefuse(一)---從SQL Server數據庫讀取數據


  上篇《可視化工具solo show-----Prefuse自帶例子GraphView講解》主要介紹了整個Prefuse工具集具有的一些特征、框架的運行流程,分析並展現了官方提供的例子GraphView.java。

  這幾天相繼的看了prefuse.data、prefuse.data.expression等包的具體接口,大致了解了prefuse框架是如何完成外部數據與prefuse數據之間的映射關系轉換;如何通過prefuse.data.expression包中的各個類完成對於邏輯表達式、字符串表達式、常見函數(如加減乘除正弦余弦預算)等的解析。

在看到prefuse.data.io.sql時發現了幾個主要的類ConnectionFactory、DatabaseDataSource、DataSourceWorker,仔細端詳一番,發現與Java連接Sql server數據庫的方式應有異曲同工之妙,所以准備着手那這塊做個application。網上搜了一番,發現已有前輩們嘗過鮮了,參見這里,但是連接的是mysql數據庫。通過prefuse api可以看出此項目編寫者對於mysql也是情有獨鍾的。那么這里還是來介紹下如何連接sql server,具體分為以下幾步:

  1.下載需要連接的sql server2005的驅動包,其中包括sqljdbc.jar和mssqlserver.jar兩個jar,導入到工程中。

  2.在sql server2005中分別創建表nodes和edges並填充值,具體如下圖所示:

  nodes表:

  

  edges表:

  

  nodes表數據:

  

  edges表數據:

  

  3.利用prefuse.data.io.sql包中的類編寫程序代碼,具體代碼如下:

public class Example1121_1 {

    /**
     * @param args
     * @throws DataIOException 
     */
    public static void main(String[] args) throws DataIOException {
        
        //------------   1    ------------
        
        String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String url = "jdbc:sqlserver://localhost:1433;DatabaseName=test";
        String username = "sa";
        String password = "123456";
        DatabaseDataSource dbds = null;
        try {
            dbds = ConnectionFactory.getDatabaseConnection(driver, url, username, password);
        } catch (SQLException e1) {
            e1.printStackTrace();
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        }
        Table nodes = dbds.getData("select * from nodes");
        Table edges = dbds.getData("select * from edges");
        Visualization vis = new Visualization();
        Graph graph = new Graph(nodes, edges, false, "id", "sid", "tid");
        
        vis.add("graph", graph);

        LabelRenderer label = new LabelRenderer("name");
        label.setRoundedCorner(10, 10);
         
        vis.setRendererFactory(new DefaultRendererFactory(label));
        
        int[] palette = new int[]{ColorLib.rgb(255, 180, 180),ColorLib.rgb(190, 190, 255)};
        DataColorAction fill = new DataColorAction("graph.nodes" , "gender" , Constants.NOMINAL, VisualItem.FILLCOLOR,palette);
        ColorAction text = new ColorAction("graph.nodes", VisualItem.TEXTCOLOR, ColorLib.gray(0));
        ColorAction edges1 = new ColorAction("graph.edges", VisualItem.STROKECOLOR, ColorLib.gray(200));
        
        ActionList color = new ActionList();
        color.add(fill);
        color.add(text);
        color.add(edges1);
        
        ActionList layout = new ActionList(Activity.INFINITY);
        layout.add(new ForceDirectedLayout("graph"));
        layout.add(new RepaintAction());
        
        vis.putAction("color", color);
        vis.putAction("layout", layout);
        
        Display display = new Display(vis);
        display.setSize(700, 600);
        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 jf = new JFrame();
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.add(display);
        jf.pack();
        jf.setVisible(true);
        
        vis.run("color");
        vis.run("layout");
        
    }

}

  以上代碼主要實現的功能時從nodes表中讀取節點的信息;從edges表中讀取邊的信息;並通過語句

LabelRenderer label = new LabelRenderer("name")讀取nodes表中那么的屬性賦給每一個節點;根據表nodes中的gender屬性為男女性別分別着不同的顏色;添加一些控制器比如拖拽、縮放、平移等。

  3.代碼運行的結果展示如下:

 

通過以上幾步,完成了prefuse與數據庫sql server2005的連接,並讀取圖形所需點和邊的信息進行圖形化的展示。所以只要掌握了prefuse連接數據庫的思想,連接其他數據庫產品也是同樣的道理,prefuse還支持jdbc/odbc數據庫的連接。

原文鏈接:http://www.cnblogs.com/bigdataZJ/p/VisualizationPrefuse1.html

 

友情贊助

 

如果你覺得博主的文章對你那么一點小幫助,恰巧你又有想打賞博主的小沖動,那么事不宜遲,趕緊掃一掃,小額地贊助下,攢個奶粉錢,也是讓博主有動力繼續努力,寫出更好的文章^^。

 

    1. 支付寶                          2. 微信

 

                      

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM