Shell layout in hierarchical layout

Problem

Earlier versions (1.x) of JGraphX ​​proposed the layoutFromSinks attribute in the mxHierarchicalLayout class. This is no longer available, and from what I saw was not for a long time.

The layout difference problem is best explained in the photos:

An old version of JGraphX, a layout from receivers enabled, creates the following:

enter image description here

Newer versions of JGraph (most of which I tried - 3.4.1.2) produce this:

enter image description here

As you can see, the old version gives a much cleaner result.

Question

Is this hierarchical layout also possible in the latest version of JGraphX, and what settings must be applied to achieve the same layout?

code

Here is the sample 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);
    }
}
+4
source share

Source: https://habr.com/ru/post/1624439/


All Articles