漫谈可视化Prefuse(一)---从SQL Server数据库读取数据

看了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数据库的连接。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值