Automatic Layout for a Graph (JGraphX)
Asked Answered
R

1

11

I have trouble to display a JGraphX with automatic layout.

My program (code below) creates an output like this: enter image description here

A sufficiant result could be look like this (I moved them by hand): enter image description here

I do not have to stick with JGraphX. I also tested JUNG and JGraphT. 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.

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. 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?"

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:

enter image description here

Ruthenious answered 19/3, 2015 at 16:27 Comment(1)
I used graphstream-project.org and you can set coordinates for your graph nodes. Here are the details about my software dachb.com/softwares/itacm . Check the fourth screenshot. This is the repository of client code bitbucket.org/vishrantg/dachb.git .Biddle
F
4

instead of using your morphGraph method try using something like this:

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

This will use the default layout-manager in mxGraph (version 3.9.3)

Fineman answered 22/4, 2018 at 19:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.