前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >漫谈可视化Prefuse(一)---从SQL Server数据库读取数据

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

作者头像
JackieZheng
发布2018-01-08 16:43:25
1.3K0
发布2018-01-08 16:43:25
举报
文章被收录于专栏:JackieZhengJackieZheng

  上篇可视化工具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包中的类编写程序代码,具体代码如下:

代码语言:javascript
复制
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数据库的连接。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-11-23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档