10

我无法显示具有自动布局的 JGraphX。

我的程序(下面的代码)创建如下输出: 在此处输入图像描述

一个足够的结果可能是这样的(我用手移动它们): 在此处输入图像描述

我不必坚持JGraphX。我也测试了JUNGJGraphT. 但这是我迄今为止最好的成绩。我需要的是我的数据视图,上面有定向的 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();
                }

        }
}

从示例生成的图表:

在此处输入图像描述

4

1 回答 1

3

而不是使用你的morphGraph方法尝试使用这样的东西:

new mxHierarchicalLayout(graph).execute(graph.getDefaultParent());

这将使用 mxGraph 中的默认布局管理器(版本3.9.3

于 2018-04-22T19:29:01.450 回答