Problem
Earlier versions (1.x) of JGraphX offered the attribute layoutFromSinks in the mxHierarchicalLayout class. This isn't available anymore and from what I've seen hasn't been for a long time.
The problem of the layout difference is best explained in pictures:
Old JGraphX version, layout from sinks enabled, produces this:
Newer JGraph versions (most current I tried is 3.4.1.2) produce this:
As you can see, the old version produces a much cleaner result.
Question
Is this hierarchical layout also possible in the latest version of JGraphX and which settings do you need to apply to achieve the same layout?
Code
Here's example code that produced the above layouts
import java.awt.BorderLayout;
import javax.swing.JFrame;
import com.mxgraph.layout.mxIGraphLayout;
import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
import com.mxgraph.model.mxICell;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;
public class HierarchyLayoutExample extends JFrame {
mxICell a,b,c,d,e,f,g,h;
public HierarchyLayoutExample() {
final mxGraph graph = new mxGraph();
Object parent = graph.getDefaultParent();
graph.getModel().beginUpdate();
try {
a = (mxICell) graph.insertVertex(parent, null, "a", 0, 0, 80, 30);
b = (mxICell) graph.insertVertex(parent, null, "b", 0, 0, 80, 30);
c = (mxICell) graph.insertVertex(parent, null, "c", 0, 0, 80, 30);
d = (mxICell) graph.insertVertex(parent, null, "d", 0, 0, 80, 30);
g = (mxICell) graph.insertVertex(parent, null, "g", 0, 0, 80, 30);
h = (mxICell) graph.insertVertex(parent, null, "h", 0, 0, 80, 30);
e = (mxICell) graph.insertVertex(parent, null, "e", 0, 0, 80, 30);
graph.insertEdge(parent, null, "", a, b);
graph.insertEdge(parent, null, "", a, c);
graph.insertEdge(parent, null, "", c, d);
graph.insertEdge(parent, null, "", e, d);
graph.insertEdge(parent, null, "", g, a);
graph.insertEdge(parent, null, "", b, d);
graph.insertEdge(parent, null, "", h, e);
} finally {
graph.getModel().endUpdate();
}
// define layout
mxHierarchicalLayout layout = new mxHierarchicalLayout(graph);
// layout.setLayoutFromSinks(true); // <== not available anymore
layout.execute(graph.getDefaultParent());
mxGraphComponent graphComponent = new mxGraphComponent(graph);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(graphComponent, BorderLayout.CENTER);
}
public static void main(String[] args) {
HierarchyLayoutExample frame = new HierarchyLayoutExample();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(460, 400);
frame.setVisible(true);
}
}