问题
Like here, my Prefuse graph is too dense to see anything. So I tried the approach suggested by @bcr in the accepted answer. However, it does not work for me. This is what I tried:
I retrieved the default settings. Then I changed the 2nd parameter of NBodyForce
from ForceSimulator
(called Distance
) and the second parameter of SpringForce
(called DefaultSpringLength
) and fed them—along with the other default values—into my new ForceSimulator
. But nothing in the output changed. What am I getting 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();
color.add(fill);
color.add(text);
color.add(edges);
ActionList layout = new ActionList(Activity.INFINITY);
Force[] originalForces = new ForceDirectedLayout("").getForceSimulator().getForces();
ForceDirectedLayout fdl = new ForceDirectedLayout("graph"){
@Override
public ForceSimulator getForceSimulator() {
ForceSimulator fs = new ForceSimulator();
fs.addForce(new NBodyForce(originalForces[0].getParameter(0), 100, originalForces[0].getParameter(2)));
fs.addForce(originalForces[1]);
fs.addForce(new SpringForce(originalForces[2].getParameter(0), 100));
return fs;
}
};
layout.add(fdl);
layout.add(new RepaintAction());
vis.putAction("color", color);
vis.putAction("layout", layout);
Display display = new Display(vis) {
@Override
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");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(display);
frame.pack();
frame.setVisible(true);
vis.run("color");
vis.run("layout");
}
回答1:
One approach might be to add a JForcePanel
, which is a
Swing component for configuring the parameters of the
Force
functions in a givenForceSimulator
. Useful for exploring different parameterizations when crafting a visualization.
This might help you to find the optimal parameters to use in your implementation of getForceSimulator()
.
ForceSimulator fsim = ((ForceDirectedLayout) layout.get(0)).getForceSimulator();
JForcePanel fpanel = new JForcePanel(fsim);
frame.add(fpanel, BorderLayout.SOUTH);
Among the demos
included in the distribution, prefuse.demos.GraphView
is a complete example. Empirically, it seems as though some parameters have more or less effect depending on the chosen data set.
Addendum: Looking closer, I see that your approach leaves the internal state of fdl
unchanged. Instead, create a new ForceSimulator
and use it in the layout and force panel; the example below changes the defaultLength
of a SpringForce
from DEFAULT_SPRING_LENGTH
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));
fdl.setForceSimulator(fs);
Alternatively, update the 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);
来源:https://stackoverflow.com/questions/30441734/prefuse-graph-manually-set-force-parameters