A ramble on visualization Prefuse (vi)---change source custom edge thickness

Source: Internet
Author: User

  Visualize all the way, experience a lot, blog writing, the harvest is quite abundant, the code all the way, thinking more and more clear. After all, still understand an old saying: the paper came to the end of shallow, I know this matter to preach.

Stumbled into the integration of a visual small tool, bits and pieces to make a lot of like-minded people, stumbled into the creation of my "visual/prefuse" QQ Group, details can see the announcement section on the left. This group is designed to be a strong and more visible field of colleagues, but also hope to become an open source project to share and read the platform. From open source, open source provides us with the nutrients to help us grow rapidly, and every line of open source code is a crystallization of the efforts of our predecessors, and open source is the enrichment of wisdom.

Blogging in the blog Park is also so, from the beginning just blindly straightforward, thespeak, never estimated to also format adjustment, also did not think to layout beautiful, not to mention what concise, sharply. In short, to look at a good blog, as if exposed to an open source project, its opening layout, article content, text style are learning materials. Recent experience is more profound: blogging also reflects the characteristics of the blog, blogs, not papers, too much or too complex theoretical elaboration (unless necessary) will make most readers two eyes dim, limbs powerless; blogs, not elementary school sentences, too short or too little lack of overall coherence, just read a bit of feeling, okay, It's over without an orgasm, and it's confusing. So, I found that whenever the article like more than the content of high-quality, but also win in the expression and the length of the control, really stand in the reader's perspective to explain the problem, solve the problem, reader will be unable to control the point of praise. In a Word, the road is very long, to learn a lot of things, slowly smell.

Previous review: "Ramble on Visualization Prefuse (v)---a part of my own visualization tool" mainly introduced a visual tool of their own development, including some of the necessary functions of the visual tools, some netizens response to the web version, in fact, Prefuse is supported applet version, The reason for not taking the web here may be to follow our needs. To know that in the case of various visual libraries with JS as a solid backing, such as Gephi, IBM I2 and other products still have an immovable position, because the demand for the production of products, different needs will correspond to different product patterns. Some netizens reflect the details of disclosure tools, the following text will be reflected.

This article is mainly based on the Prefuse framework: 1. How to adjust the thickness of the edges according to the requirements of the visual tools; The ingenious use of 2.try...catch

  1. How to reflect the weight of edges by the thickness of edges

The thickness of the default edge of the Prefuse frame is uniform and is 1. But we often face the need to give the data format as follows

Source Target Weight
1 2 0.5

Then we can reflect this weight property by the thickness of the edge, to indicate the degree of intimacy between Node 1 and Node 2.

So, let's make a change to the source code for Prefuse, before we get to know the inheritance of the class:

Class Labelrenderer:

public class Labelrenderer extends Abstractshaperenderer {    ...    public void Render (graphics2d g, Visualitem Item) {Rectangularshape shape = (rectangularshape) getshape (item), if (shape = = NULL) return;                ......    }    ......  }

Class Edgerenderer:

public class Edgerenderer extends Abstractshaperenderer {    ...    public void Render (graphics2d g, Visualitem Item) {        //render the Edge line        super.render (g, item);        ......    }    ......}

Abstract class Abstractshaperenderer:

Public abstract class Abstractshaperenderer implements Renderer {    ...    public void Render (graphics2d g, Visualitem Item) {Shape shape = getshape (item);        ......    }    ......}    

From the above three classes, it can be found that the classes Labelrenderer and Edgerenderer are inherited from the abstract class Abstractshaperenderer, and the Labelrenderer and Edgerenderer classes overwrite the parent class The Render method.

The most significant difference between the two-class render methods is that the Edgerenderer class explicitly invokes the Render method of the abstract parent class Abstractshaperenderer. and by adding in the Render method of the Abstractshaperenderer class:

SYSTEM.OUT.PRINTLN ("The item belongs to:" + item.getgroup ())

The resulting printing information is "This item belongs to: Graph.edges", that is, the rendering is for the edge.

To understand the inheritance of the class, the following steps are given to modify the source code to achieve the assignment of edge weight through the configuration file:

  (1) in the Prefuseconfig class, add:

SetProperty ("Data.edge.weight", "weight");

Used in the XML file to determine the edge thickness of the indicator, here is weight;

  (2) Add in graph class:

public static final String WEIGHT = Prefuseconfig.get ("Data.edge.weight");

Used to get constants in Graphmlreader;

  (3) Add in Graphmlreader:

public static final String WEIGHT = graph.weight;public static final String weightid = WEIGHT + ' _ ' + ID;

  Method Startdocument () adds:

M_esch.addcolumn (WEIGHT, string.class);//edge thickness related m_esch.addcolumn (Weightid, String.class);

  Method Startelement () adds:

M_edges.setstring (M_row, WEIGHT, Atts.getvalue (WEIGHT)); M_edges.setstring (M_row, Weightid, Atts.getvalue (WEIGHTID) );

  

  (4) Add in Abstractshaperenderer:

Edgeitem edge = (edgeitem) item; Visualitem VI = (visualitem) edge.getsourcenode (); Item.setstrokecolor (Vi.getfillcolor ()); String weight = null;try {weight = Item.get (graphmlhandler.weight). toString ();     } catch (Exception e) {       weight = "1 ";} Item.setsize (double.parsedouble (weight) *10);    

  

  The configuration file used is in XML format, with the following content:

<?xml version= "1.0" encoding= "UTF-8"?><!--an excerpt of a egocentric social network--><graphml xmlns= " Http://graphml.graphdrawing.org/xmlns "><graph edgedefault=" undirected "> <!--Data schema--><key Id= "name" for= "Node" attr.name= "Name" attr.type= "string"/><key id= "gender" for= "node" attr.name= "gender" Attr.type= "string"/><key id= "id" for= "node" attr.name= "id" attr.type= "string"/> <!--nodes--<node Id= "1" > <data key= "name" >Jeff</data> <data key= "gender" >M</data> <data key= "id" >1 </data> </node><node id= "2" > <data key= "name" >Ed</data> <data key= "gender" >M< /data> <data key= "id" >2</data></node><node id= "3" > <data key= "name" >Christiaan< /data> <data key= "gender" >M</data> <data key= "id" >3</data></node><node id= "4" > <data key= "name" >Emily</data> <data key= "gender" >f</data> <data key= "id" >4</data></node><node id= "5" > <data key= "name" >adam</ data> <data key= "gender" >M</data> <data key= "id" >5</data></node><node id= "6" > <data key= "name" >Cynthia</data> <data key= "gender" >F</data> <data key= "id" >6</ Data></node><node id= "7" > <data key= "name" >Joylette</data> <data key= "gender" >f </data> <data key= "id" >7</data></node><node id= "8" > <data key= "name" >Amanda< /data> <data key= "gender" >F</data> <data key= "id" >8</data></node><node id= "9" > <data key= "name" >Nathaniel</data> <data key= "gender" >M</data> <data key= "id" >9 </data></node><!--edges--><edge source= "1" target= "2" directed= "true" weight= "0.1" ></ Edge><edge source= "2" target= "3" weight= "0.2" ></edge><edge source= "3" target= "1" weight= "0.5" ></edge><edge source= "4" target= "5" weight= "0.2" ></edge><edge source= "5 "target=" 6 "weight=" 0.1 "></edge><edge source=" 6 "target=" 4 "weight=" 0.2 "></edge><edge Source= "7" target= "8" weight= "0.3" ></edge><edge source= "8" target= "9" weight= "0.1" ></edge>< Edge source= "9" target= "7" weight= "0.4" ></edge><edge source= "1" target= "4" weight= "0.1" ></edge> <edge source= "4" target= "7" weight= "0.2" ></edge><edge source= "7" target= "1" weight= "0.1" ></edge ></graph></graphml>

  

The final graphic is displayed as:

2.The ingenious use of Try....catch

The ingenious use of the place is shown in 1 (4):

Edgeitem edge = (edgeitem) item; Visualitem VI = (visualitem) edge.getsourcenode (); Item.setstrokecolor (Vi.getfillcolor ()); String weight = null;try {weight = Item.get (graphmlhandler.weight). toString ();     } catch (Exception e) {       weight = "1 ";} Item.setsize (double.parsedouble (weight) *10);  

   Because in the actual situation, you will encounter some XML file does not have the weight attribute, or part of the XML part of the situation is not, this will be faced in abstractshaperenderer can not get weight value in the case, that is

Item.get (Graphmlhandler.weight)

This value does not exist or is empty, initially by adding a statement:

if (Item.get (graphmlhandler.weight). toString (). Equals ("") | | Item.get (graphmlhandler.weight). toString () = = null) {WEIGHT = "1";} Else{weight = Item.get (graphmlhandler.weight). toString ();}

  And so on, it is impossible to capture Item.get () without the value of all cases, and later thought, in view of the catch is an empty exception, simply use Try...catch directly capture, after the capture is processed. Thus, the above processing, that is, when the Item.get () function Gets the value of the catch exception, into the catch part of the execution, where we write the function we want to implement, that is, to get null when the weight set the default value of 1, which cleverly solves the case of considering all empty.

From the source of experience, it is necessary to identify the function you want, clear what classes need to be touched, how to ensure the integrity of the modified source code, not because of the modification of the error everywhere, but also need to think often, flexible to run some of the features of the statement for their own use. Come here today, if you feel useful, remember to like ^_^, the visualization (Gephi, Prefuse, distributed computing, open source) interested in adding group discussion.

This article link: "Ramble on Visualization Prefuse (vi)---change source custom edge thickness"

  

A ramble on visualization Prefuse (vi)---change source custom edge thickness

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.