Prefuse graph手动设置force参数

Prefuse graph manually set force parameters

here一样,我的 Prefuse 图太密集了,看不到任何东西。所以我尝试了@bcr 在接受的答案中建议的方法。但是,它对我不起作用。这是我试过的:

我找回了默认设置。然后我将 NBodyForce 的第二个参数从 ForceSimulator(称为 Distance)和 SpringForce 的第二个参数(称为 DefaultSpringLength)更改为其他默认值 - 进入我的新 ForceSimulator。但输出中没有任何变化。我哪里错了?

这是我的代码:

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");
}

一种方法可能是添加一个 JForcePanel,即

Swing component for configuring the parameters of the Force functions in a given ForceSimulator. Useful for exploring different parameterizations when crafting a visualization.

这可能会帮助您找到用于实施 getForceSimulator() 的最佳参数。

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

在发行版中包含的demos中,prefuse.demos.GraphView是一个完整的例子。根据经验,似乎某些参数或多或少会根据所选数据集产生影响。

附录:仔细观察,我发现您的方法没有改变 fdl 的内部状态。相反,创建一个新的 ForceSimulator 并在布局和强制面板中使用它;下面的示例将 SpringForcedefaultLengthDEFAULT_SPRING_LENGTH 更改为 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);

或者,直接更新 SpringForce,如图 here

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