Note that there are some explanatory texts on larger screens.

plurals
  1. POGraphML Node Coloring Prefuse
    text
    copied!<p>I have a prefuse application that loads GraphML files where the nodes have a "Color" string property. ie.</p> <pre><code>&lt;node id="1"&gt; &lt;data key="Color"&gt;Green&lt;/data&gt; &lt;node&gt; </code></pre> <p>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.</p> <p>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:</p> <pre><code>// Add new property to Graph g g.addColumn("ColorNumber", int.class); // Add property to each node Iterator&lt;Node&gt; 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 &lt; colorMap.length; i++) { if(type.equalsIgnoreCase(colorMap[i])) { n.setInt("ColorNumber", i); break; } } } </code></pre> <p>Then, you can use prefuse's default color manager to link these integers to array indices.</p> <pre><code>draw.add(new DataColorAction("graph.nodes", "ColorNumber", Constants.NUMERICAL, VisualItem.FILLCOLOR, fillArray)); </code></pre> <p>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.</p> <p>My other solution is to manually set each node's color.</p> <pre><code>// Iterate over VisualItems in Visualization vis Iterator&lt;VisualItem&gt; 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 &lt; typeMap.length; i++) { if(type.equalsIgnoreCase(typeMap[i])) { item.setFillColor(fill[i]); item.setEndFillColor(fill[i]); break; } } } vis.repaint(); </code></pre> <p>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.</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload