I have trouble to display a JGraphX with automatic layout.
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
. 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: