2015-05-25 15 views
2

here一樣,我的Prefuse圖過於密集,無法看到任何東西。所以我在接受的答案中嘗試了@bcr提出的方法。但是,它不適合我。這是我試過的:Prefuse圖手動設置力參數

我檢索了默認設置。然後,我將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"); 
} 

回答

2

一種方法可能是添加一個JForcePanel,這是在給定的ForceSimulator配置的Force功能參數

Swing組件。在製作可視化文件時用於探索不同的參數化。

這可能會幫助您找到在實施getForceSimulator()時使用的最佳參數。

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

在包括在分佈demosprefuse.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); 

image

+0

非常感謝你。 JForcePanel非常有用,並且是一個有趣的玩具,同時以您完美運行的方式設置部隊參數。 – anjuta

+0

對於那些可能會遇到同樣問題的人,我做了一個評論:我必須做fs.addForce(新的SpringForce(SpringForce.DEFAULT_SPRING_COEFF,)); NBody和DragForces的構造函數只接受float,而不是double,這是Java中的默認值。 – anjuta

+0

還有一點評論:你可以使用比JForcePanel建議的值更廣泛的值,並且它們會生效。 – anjuta