The preprocessing graph manually sets the force parameters

Like here , my Prefuse graph is too tight to see. So I tried the approach suggested by @bcr in the accepted answer. However, this doesn't work for me. This is what I tried:

I got the default settings. Then I changed the second parameter NBodyForce

from ForceSimulator

(called Distance

) and the second parameter SpringForce

(called DefaultSpringLength

) and fed them along with other defaults - to my new one ForceSimulator

. But nothing in the output has changed. What am I wrong?

This is my code:

private static void visualiseGraph(Graph graph) {
    Visualization vis = new Visualization();
    vis.add("graph", graph);
    LabelRenderer r = new LabelRenderer("someLabel");
    r.setRoundedCorner(8, 8);
    vis.setRendererFactory(new DefaultRendererFactory(r));
    ColorAction fill = new ColorAction("graph.nodes",
            VisualItem.FILLCOLOR, ColorLib.rgb(190,190,255));
    ColorAction text = new ColorAction("graph.nodes",
        VisualItem.TEXTCOLOR, ColorLib.gray(0));
    ColorAction edges = new ColorAction("graph.edges",
        VisualItem.STROKECOLOR, ColorLib.rgb(255,180,180)); 
    ActionList color = new ActionList();
    ActionList layout = new ActionList(Activity.INFINITY);
    Force[] originalForces = new ForceDirectedLayout("").getForceSimulator().getForces();
    ForceDirectedLayout fdl = new ForceDirectedLayout("graph"){
        public ForceSimulator getForceSimulator() {
            ForceSimulator fs = new ForceSimulator();
            fs.addForce(new NBodyForce(originalForces[0].getParameter(0), 100, originalForces[0].getParameter(2)));
            fs.addForce(new SpringForce(originalForces[2].getParameter(0), 100));
            return fs;
    layout.add(new RepaintAction());
    vis.putAction("color", color);
    vis.putAction("layout", layout);
    Display display = new Display(vis) {
        public Dimension getPreferredSize() {
            return new Dimension(W, H);
    display.pan(W / 2, H / 2);
    display.addControlListener(new DragControl()); // drag items around
    display.addControlListener(new PanControl());  // pan with background left-drag
    display.addControlListener(new ZoomControl()); // zoom with vertical right-drag
    JFrame frame = new JFrame("prefuse example");



source to share

1 answer

One approach might be to add JForcePanel

, which is

Swing component for setting parameters of functions Force

in a given ForceSimulator

. Useful for exploring various options when creating a visualization.

This can help you find the optimal parameters to use in your implementation getForceSimulator()


ForceSimulator fsim = ((ForceDirectedLayout) layout.get(0)).getForceSimulator();
JForcePanel fpanel = new JForcePanel(fsim);
frame.add(fpanel, BorderLayout.SOUTH);


Among those demos

included in the distribution prefuse.demos.GraphView

is a complete example. Empirically, some parameters seem to have more or less effect depending on the dataset selected.

Adding . Coming closer, I can see that your approach leaves the internal state fdl

unchanged. Instead, create a new one ForceSimulator

and use it in the layout and strength panel; the example below changes defaultLength

a SpringForce


to 42


ForceDirectedLayout fdl = new ForceDirectedLayout("graph");
ForceSimulator fs = new ForceSimulator();
fs.addForce(new NBodyForce());
fs.addForce(new DragForce());
fs.addForce(new SpringForce(DEFAULT_SPRING_COEFF, 42));


Alternatively, update SpringForce

directly as shown here .

ForceDirectedLayout fdl = new ForceDirectedLayout("graph");
ForceSimulator fs = fdl.getForceSimulator();
Force[] forces = fs.getForces();
SpringForce sf = (SpringForce) forces[2];
sf.setParameter(SPRING_LENGTH, 42);





All Articles