Question

I have a prefuse application that loads GraphML files where the nodes have a "Color" string property. ie.

<node id="1">
  <data key="Color">Green</data>
<node>

I want to allow a range of predefined colors to be specified, and I don't want to force the program that creates the GraphML to use all of the colors.

I thought this would be a simple task (and maybe it is?), but prefuse seems to fight me at every turn. I've come up with two partial solutions, but they both have their issues. My first approach was to manually add a new Integer property that correlated to the "Color" strings, like so:

// Add new property to Graph g
g.addColumn("ColorNumber", int.class);

// Add property to each node   
Iterator<Node> it = g.nodes();
while(it.hasNext()) {
    Node n = it.next();
    String type = n.getString("Color");
    // Compare to an array of accepted types
    for(int i=0; i < colorMap.length; i++) {
        if(type.equalsIgnoreCase(colorMap[i])) {
            n.setInt("ColorNumber", i);
            break;
        }
    }
}

Then, you can use prefuse's default color manager to link these integers to array indices.

draw.add(new DataColorAction("graph.nodes", "ColorNumber", Constants.NUMERICAL, VisualItem.FILLCOLOR, fillArray));

The biggest issue with this approach (on top of it being inelegant), is that it fails when less than three unique colors are specified by the user. If two colors are specified, prefuse uses the first and last elements of the color array. If one, the first. Meh.

My other solution is to manually set each node's color.

// Iterate over VisualItems in Visualization vis
Iterator<VisualItem> v_it = vis.items("graph.nodes");
while(v_it.hasNext()) {
    VisualItem item = v_it.next();
    String type = item.getString("Color");
    for(int i=0; i < typeMap.length; i++) {
        if(type.equalsIgnoreCase(typeMap[i])) {
            item.setFillColor(fill[i]);
            item.setEndFillColor(fill[i]);
            break;
        }
    }
}
vis.repaint();

This works for any number of colors, but randomly messes up. I think that prefuse handles these methods on their own thread, which, for some reason, runs slowly. For smaller networks, I can set a fixed wait time (I haven't found a Thread.join() method to use). But, this fails hilariously (read: crashes my computer) for large networks.

So, there you have it. An unnecessarily large time commitment for what should be a trivial task. Has someone managed to accomplish this? Is there an easy way? Please tell me I'm overthinking this.

Was it helpful?

Solution

I know such experiences about prefuse very well :-/

The first approach is on the right way, but specified the wrong data type. Color is a NOMINAL variable.

DataColorAction color = new DataColorAction("graph.nodes", "Color",
    Constants.NOMINAL, VisualItem.FILLCOLOR, fillArray);

The ColorNumber field is not necessary.

Your conclusions about the second approach are correct. prefuse runs its ActionList in a separate thread. Therefore one should not set visual properties manually.

If more fine-grained code is desired, you can extend DataColorAction.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top