Forums

Full Version: adding data "manually" to a graph
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This demo shows how to add data "manually" to a graph, should mean not reading the data from a file.

More or less a cut down of the AggregateDemo of the prefuse download.

The steps which create the graph:
Code:
Graph g = new Graph();

g.addColumn("label", String.class);

Node n1 = g.addNode();    
Node n2 = g.addNode();
Node n3 = g.addNode();
        
n1.setString("label","self");
n2.setString("label","arisen");
n3.setString("label","graph");
            
g.addEdge(n1, n2);
g.addEdge(n1, n3);
        
m_vis.addGraph(GRAPH, g);


(Additional hint: Please notice that in this minimalistic demo the node color doesn't change at the mouse hover event, so the "draw" ColorAction doesn't need to be included in the infinite active "animate" action. Still all interaction with the graph like zoom, pan and and drag work fine since these are working on the display and not on the visualization level. Check the AggregateDemo source code to see the difference for the case of more active node coloring, which works on the visualization level. If you haven't yet don't miss to memorize the prefuse toolkit structure documentation, which is an essential prerequisite in order to get a delighted prefusian)
Hi!
I'm really new to prefuse and I'm trying to create a graph but I'm having some difficulties.
I'm creating the graph this way

Code:
Table nodes = new Table();
Table edges = new Table();

nodes.addColumn("label", String.class);
nodes.addColumn("ID", int.class);
...
nodes.addColumn("#Elements", int.class);

edges.addColumn("IDp", int.class);
edges.addColumn("IDc", int.class);



for (int i=0; i<visibleFrames.size(); i++)
{
    nodesRow = nodes.addRow();

    nodes.set(nodesRow, "ID", id);
    nodes.set(nodesRow, "label", "sdsdsd");
    nodes.set(nodesRow, "#Elements", 1234);

    if ((parentID = hasParent()))
    {
        edgesRow = edges.addRow();
        edges.set(edgesRow, "IDp", parentID);
        edges.set(edgesRow, "IDc", id);
    }
}

Graph g= new Graph(nodes, edges, false, "ID", "IDp", "IDc");

This seems to work fine but when I had the graph to a visualization this exception is thrown:

Code:
java.lang.ClassCastException: prefuse.visual.tuple.TableEdgeItem cannot be cast to prefuse.data.Node
    at prefuse.action.layout.GridLayout.analyzeGraphGrid(GridLayout.java:96)
    at prefuse.action.layout.GridLayout.run(GridLayout.java:62)
    at prefuse.action.ActionList.run(ActionList.java:79)
    at prefuse.action.Action.run(Action.java:122)
    at prefuse.activity.Activity.runActivity(Activity.java:165)
    at prefuse.activity.ActivityManager.run(ActivityManager.java:365)

But if I create a Graph from GraphLib the visualization works just fine.

What am I doing wrong?

TIA
José Tavares
I know usually this would be a problem for the sourceforge forum but since it sucks I´ll answer here.

Code:
if ((parentID = hasParent()))
Since I can´t see clearly what´s the trick behind this I got to ask:
You assign hastParent() to parentID and catch if the assignment worked.
I´d think parentID is some int while hasParent sounds like a boolean. I´m confused Wink
Obviously it shall work as "if it has a parent then add the edge" but maybe you could explain this a little.

The exception message
Code:
prefuse.visual.tuple.TableEdgeItem cannot be cast to prefuse.data.Node
sounds like prefuse tries to use a edge as a node, and that sounds like you put some edge into the node table.

It would be the easiest to post a working code where one could see the problem live but maybe it´s better to post it in a new thread.

Marcus
Reference URL's