Groups break hierarchical layout in JGraphX

1.8k Views Asked by At

I find that if I introduce groups in to a simple graph laid out with hierarchical layout, the group is incorrectly positioned.

For example, here is a simple graph:

public class HierarchicalLayoutWithGrouping extends JFrame {

    public HierarchicalLayoutWithGrouping() {
        super("Hierarchical Layout With Grouping Test");

        mxGraph graph = new mxGraph();
        Object parent = graph.getDefaultParent();

        graph.getModel().beginUpdate();
        try {
            Object transfer1 = graph.insertVertex(parent, null, "Transfer Data1.csv", 0, 0, 100, 40);
            Object transfer2 = graph.insertVertex(parent, null, "Transfer Data2.csv", 0, 0, 100, 40);
            Object load1 = graph.insertVertex(parent, null, "Load Data1.csv", 0, 0, 100, 40);
            Object load2 = graph.insertVertex(parent, null, "Load Data1.csv", 0, 0, 100, 40);
            graph.insertEdge(parent, null, null, transfer1, load1);
            graph.insertEdge(parent, null, null, transfer2, load2);
            Object calculate = graph.insertVertex(parent, null, "Calculate", 0, 0, 100, 40);
            graph.insertEdge(parent, null, null, load1, calculate);
            graph.insertEdge(parent, null, null, load2, calculate);

            // Object loads = graph.insertVertex(parent, null, "Loads", 0, 0, 200, 400, "shape=swimlane;");
            // graph.groupCells(loads, 5, new Object[] { load1, load2 });

            mxHierarchicalLayout layout = new mxHierarchicalLayout(graph, SwingConstants.WEST);
            layout.execute(parent);

        } finally {
            graph.getModel().endUpdate();
        }

        mxGraphComponent graphComponent = new mxGraphComponent(graph);
        getContentPane().add(graphComponent);
    }

    public static void main(String[] args) {
        HierarchicalLayoutWithGrouping frame = new HierarchicalLayoutWithGrouping();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 450);
        frame.setVisible(true);
    }
}

Yields this beautiful graph:

enter image description here

but if I uncomment the following lines:

            Object loads = graph.insertVertex(parent, null, "Loads", 0, 0, 200, 400, "shape=swimlane;");
            graph.groupCells(loads, 5, new Object[] { load1, load2 });

I get this:

enter image description here

I've tried various options on mxHierarchicalLayout but haven't found anything that works yet. How do I get it to layout the group on the correct position?

Thanks.

1

There are 1 best solutions below

0
On

I found out, that the grouping box gets another hierarchy as the nodes that are ungrouped. This is the reason why each of the group boxes are moved to a new row. I think there's no other possibility then writing your own layout algorithm or just to override the method that generates the hierarchy attributes during the layout execution.