Prefuse reads data from the database

Source: Internet
Author: User
Use Prefuse With database

Keywords:PrefuseDatabase

PrefuseIs a very good open-source visualization project, especially on the social network/complex network, I personally feel better than Jung. Unfortunately,PrefuseThe user manual is still under construction, and Google resource is also poor. Fortunately, open source provides the source code, so you have to look at the source code.

PrefuseA simple example is provided on User Manual. The data in this example comes fromGraphml standard XML file (Socialnet. XML), roughly as follows:

XML Code
  1. XML Version="1.0" Encoding=UTF-8"?>
  2. <Graphml Xmlns=Http://graphml.graphdrawing.org/xmlns">
  3. <Graph Edgedefault="Undirected">
  4. key id = "name" for = "Node" ATTR. name = "name" ATTR. type = "string" />
  5. <Key ID="Gender" For="Node" ATTR. Name="Gender" ATTR. Type="String"/>
  6. <Node ID="1">
  7. <Data Key="Name">JeffData>
  8. <Data Key="Gender">MData>
  9. Node>
  10. <Node ID="2">
  11. <Data Key="Name">EdData>
  12. <Data Key="Gender">MData>
  13. Node>
  14. ...........................
  15. <Edge Source="1" Target="2">Edge>
  16. <Edge Source="1" Target="3">Edge>
  17. ............................
  18. Graph>
  19. Graphml>

It looks like this,ProgramIt is also relatively simple. However, it is impossible to visualize the data of a Community network from XML. In fact, most of the data is from databases. Prefuse supports direct data acquisition from the database. But it's not mentioned in user manual. I had to explore it myself.

First, create a database for MySQL testing. One represents a node and the other represents an edge.

SQL code
  1. Drop TableIf exists 'test'. 'node ';
  2. Create Table'Test'. 'node '(
  3. 'Id'Int(10) unsignedNot NullAuto_increment,
  4. 'Name'Varchar(45)Not Null,
  5. 'Gender'Varchar(45)Not Null,
  6. Primary Key('Id ')
  7. ) Engine = InnoDBDefaultCharset = utf8;
  8. Drop TableIf exists 'test'. 'edge ';
  9. Create Table'Test'. 'edge '(
  10. 'Id'Int(10) unsignedNot NullAuto_increment,
  11. 'Sid'Int(10) unsignedNot Null,
  12. 'Tid'Int(10) unsignedNot Null,
  13. Primary Key('Id ')
  14. ) Engine = InnoDBDefaultCharset = utf8;

Next, insert several data entries in the two tables. Note that in the edge table, Sid and TID must exist in the node table (that is, the node table has an ID corresponding to it ), otherwise, the prefuse will throw an exception during reading.

Next is the program:

Java code
  1. Public ClassTestmysql {
  2. Public Static FinalString drivername ="Com. MySQL. JDBC. Driver";
  3. Public Static FinalString dburl ="JDBC: mysql: // localhost: 3306/test";
  4. Public Static FinalString username ="Root";
  5. Public Static FinalString userpwd ="123456";
  6. Public Static VoidMain (string [] ARGs ){
  7. Databasedatasource datasrc =Null;
  8. Try{
  9. // Get database connection
  10. Datasrc = connectionfactory. getdatabaseconnection (
  11. Drivername, dburl, username, userpwd );
  12. // Create a table of data
  13. Table nodes = datasrc. getdata ("Select * from node");
  14. Table edges = datasrc. getdata ("Select * from edge");
  15. Graph graph =NewGraph (nodes, edges,False,"ID","Sid","TID");
  16. Visualization vis =NewVisualization ();
  17. VIS. Add ("Graph", Graph );
  18. Labelrenderer r =NewLabelrenderer ("Name");
  19. R. setroundedcorner (8,8);
  20. VIS. setrendererfactory (NewDefaultrendererfactory (R ));
  21. Int[] Palette =New Int[] {
  22. Colorlib. RGB (255,180,180), Colorlib. RGB (190,190,255)
  23. };
  24. Datacolpartition tion fill =NewDatacolpartition tion ("Graph. Nodes","Gender",
  25. Constants. Nominal, visualitem. fillcolor, palette );
  26. Colequaltion textcolor =NewColpartition tion ("Graph. Nodes",
  27. Visualitem. textcolor, colorlib. Gray (0));
  28. Colequaltion edgescolor =NewColpartition tion ("Graph. edges",
  29. Visualitem. strokecolor, colorlib. Gray (200));
  30. Actionlist color =NewActionlist ();
  31. Color. Add (fill );
  32. Color. Add (textcolor );
  33. Color. Add (edgescolor );
  34. Actionlist layout =NewActionlist (activity. Infinity );
  35. Layout. Add (NewForcedirectedlayout ("Graph"));
  36. Layout. Add (NewRepaintaction ());
  37. VIS. putaction ("Color", Color );
  38. VIS. putaction ("Layout", Layout );
  39. Display d =NewDisplay (VIS );
  40. D. setsize (720,500);
  41. D. addcontrollistener (NewDragcontrol ());
  42. D. addcontrollistener (NewPancontrol ());
  43. D. addcontrollistener (NewZoomcontrol ());
  44. D. addcontrollistener (NewNeighborhighlightcontrol ());
  45. Jframe frame =NewJframe ("Haha");
  46. Frame. setdefaclocloseoperation (jframe. exit_on_close );
  47. Frame. setsize (800,600);
  48. Frame. Add (d );
  49. Frame. setvisible (True);
  50. VIS. Run ("Color");
  51. VIS. Run ("Layout");
  52. }Catch(Exception e ){
  53. E. printstacktrace ();
  54. }
  55. }

Row 3:Generating a graph object is the key to visualization. Graph objects can be generated in multiple ways. There are several ways to generate them simply from a database (or from a table. However, we recommend that you generate this example. The more information you provide, the more accurate you can describe yourself. The parameter meanings are: The table of the node, the table of the edge, whether it is directed (true with, false without), the primary key of the node, edge indicates the source column name (corresponding to the edge table), and edge indicates the target column name (corresponding to the edge table ).

Nothing else can be said, which is similar to that on user manual. I personally think that another advantage of prefuse is that there are few final classes, that is, the scalability is relatively strong. For example, if you add an action (in the prefuse. Controls package) to lines, you can generate your own control.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.