开发者

Problem in displaying nodes through PREFUSE library for Java?

开发者 https://www.devze.com 2022-12-20 02:51 出处:网络
I am developing a graphical view of data using PREFUSE library. I have 3 kinds of nodes in my graph: APPLICATION

I am developing a graphical view of data using PREFUSE library.

I have 3 kinds of nodes in my graph:

  1. APPLICATION
  2. DATABASE
  3. INTERFACE

Below are some excerpts from my .xml file containing graph

<node id="AP-1">
<data key="name">Application1</data>
<data key="type">APPLICATION</data>
</node>

<node id="DB-1">
<data key="name">Database1</data>
<data key="type">DATABASE</data>
</node>

<node id="IT-1">
<data key="name">Interface1</data>
<data key="type">INTERFACE</data>
</node>

I want to show the above 3 diff. kinds of nodes with 3 diff. kinds of shapes as follows:

  1. APPLICATION by Rectangle
  2. DATABASE by custom shape (shape of a cylinder, usually used to denote a database)
  3. INTERFACE by circle

I have first read the .xml file in a

Graph g

Q1. Now how can I distinguish these 3 kinds of nodes in a datagroup. I 开发者_开发问答think I should write predicates. I have read the whole predicates and expressions manuals for prefuse but couldn't write a predicate to distinguish them. So what will be the predicate for that?

Q2. How to specify my custom shape and how to set a renderer that can render the custom shape developed by me?


The paradigm for assigning different shapes to nodes is with a DataShapeAction

E.g. in the "Congress" demo (the same applies to Nodes as Tables):

int[] shapes = new int[]
            { Constants.SHAPE_RECTANGLE, Constants.SHAPE_DIAMOND };
DataShapeAction shape = new DataShapeAction(group, "Senate", shapes);

This assigns different shapes to data points based on the value in the "Senate" data field, i.e. senators are one shape, and congressmen are another shape, in some order (there are various controls for this in the API, see Constants.ORDINAL for an example).

So, in other words, you would probably use your "type" data field to indicate what kind of node the node was, and then use a DataShapeAction to assign different shapes.

Defining a new shape is certainly possible, but will require some more tinkering. I'll try to get back to you with a better answer, but I'm guessing the most straightforward way would be to write your own subclass of the noderenderer that was capable of drawing your desired shape, and then perhaps extending DataShapeAction to handle some flag for your new data type. More on that later, though, hopefully.


You don't need predicates to assign shapes. In fact, in order to draw custom shapes you have to subclass the shape drawing renderer ShapeRenderer. ShapeRenderer distinguishes between shapes using id number (int). These ints are in structure Constants for all the standard shapes - like bcr wrote, for example Constants.SHAPE_RECTANGLE.

Internally prefuse calls ShapeRenderer's protected Shape getRawShape(VisualItem item) function. In turn, this function calls other internals from ShapeRenderer in order to get the shape to draw. For example:

  • to get the shape id, getRawShape calls int stype = item.getShape() (set by shape action DataShapeAction)
  • then, hawing the shape id at hand, there is switch statement selecting proper shape to draw

    switch ( stype )  
    {  
    case Constants.SHAPE_NONE:  
        return null;  
    case Constants.SHAPE_RECTANGLE:  
        return rectangle(x, y, width, width);  
    case Constants.SHAPE_ELLIPSE:  
        return ellipse(x, y, width, width);  
    case Constants.SHAPE_TRIANGLE_UP:  
        return triangle_up((float)x, (float)y, (float)width);  
    ...  
    

In order to draw some other shapes (custom ones) you subclass ShapeRenderer and provide your own implementation of shape to draw and override getRawShape.
If you recognize the shape id as your own you return your shape otherwise you call super(item) in your implementation of getRawShape in order to call the standard one.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号