fro*_*hli 10 java graph jgrapht jung jgraphx
我无法显示具有自动布局的JGraphX.
我的程序(下面的代码)创建一个这样的输出:

一个足够的结果可能看起来像这样(我用手移动它们):

我不必坚持JGraphX.我还测试JUNG和JGraphT.但到目前为止,这是我最好的结果.我需要的是我的数据视图,其上有定向的eges和一些标签.
我制作了一个示例代码,展示了它是如何创建的.它类似于http://forum.jgraph.com/questions/4810/how-to-layout-nodes-automatically-using-fast-organic-layout.有一个评论从2012年,提到相同的问题"[...]但是我注意到节点有时重叠,你知道一种方法来解决这个问题,我已经玩了属性,它似乎很随意.一般来说如何改善外观的任何建议?"
public class Test extends JFrame {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setSize(800, 800);
f.setLocation(300, 200);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final mxGraph graph = new mxGraph();
mxGraphComponent graphComponent = new mxGraphComponent(graph);
f.getContentPane().add(graphComponent, BorderLayout.CENTER);
f.setVisible(true);
Object parent = graph.getDefaultParent();
graph.getModel().beginUpdate();
try {
Object start = graph.insertVertex(parent, "start", "start", 100,
100, 80, 30);
for (int i = 0; i < 10; i++) {
Object a = graph.insertVertex(parent, "A" + i, "A" + i, 100,
100, 80, 30);
graph.insertEdge(parent, null, "E" + i, start, a);
Object b = graph.insertVertex(parent, "B" + i, "B" + i, 100,
100, 80, 30);
graph.insertEdge(parent, null, "E" + i, a, b);
start = a;
}
} finally {
graph.getModel().endUpdate();
}
morphGraph(graph, graphComponent);
}
private static void morphGraph(mxGraph graph,
mxGraphComponent graphComponent) {
// define layout
mxIGraphLayout layout = new mxFastOrganicLayout(graph);
// layout using morphing
graph.getModel().beginUpdate();
try {
layout.execute(graph.getDefaultParent());
} finally {
mxMorphing morph = new mxMorphing(graphComponent, 20, 1.5, 20);
morph.addListener(mxEvent.DONE, new mxIEventListener() {
@Override
public void invoke(Object arg0, mxEventObject arg1) {
graph.getModel().endUpdate();
// fitViewport();
}
});
morph.startAnimation();
}
}
}
Run Code Online (Sandbox Code Playgroud)
从示例生成的图表:

而不是使用你的morphGraph方法尝试使用这样的东西:
new mxHierarchicalLayout(graph).execute(graph.getDefaultParent());
Run Code Online (Sandbox Code Playgroud)
3.9.3这将使用 mxGraph (版本)中的默认布局管理器
| 归档时间: |
|
| 查看次数: |
3661 次 |
| 最近记录: |