简体   繁体   English

图表的自动布局(JGraphX)

[英]Automatic Layout for a Graph (JGraphX)

I have trouble to display a JGraphX with automatic layout. 我无法显示具有自动布局的JGraphX。

My program (code below) creates an output like this: 我的程序(下面的代码)创建一个这样的输出: 在此输入图像描述

A sufficiant result could be look like this (I moved them by hand): 一个足够的结果可能看起来像这样(我用手移动它们): 在此输入图像描述

I do not have to stick with JGraphX . 我不必坚持使用JGraphX I also tested JUNG and JGraphT . 我还测试了JUNGJGraphT But this was my best result so far. 但到目前为止,这是我最好的结果。 What I need is a view for my data, with directed eges and some labels on it. 我需要的是我的数据视图,其上有定向的eges和一些标签。

I made an example code that shows how it is created. 我制作了一个示例代码,展示了它是如何创建的。 It is similar to http://forum.jgraph.com/questions/4810/how-to-layout-nodes-automatically-using-fast-organic-layout . 它类似于http://forum.jgraph.com/questions/4810/how-to-layout-nodes-automatically-using-fast-organic-layout There is a comment from 2012, that mentioned the same issue "[...]However I have noticed the nodes overlap sometimes, do you know a way to fix that, I've played around with the properties and it seems quite random. Any advice how to improve the looks in general?" 有一个评论从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();
                }

        }
}

Graph generated from example: 从示例生成的图表:

在此输入图像描述

instead of using your morphGraph method try using something like this: 而不是使用您的morphGraph方法尝试使用这样的东西:

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

This will use the default layout-manager in mxGraph (version 3.9.3 ) 这将使用mxGraph中的默认布局管理器(版本3.9.3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM