How to make the edges of needed form in jgraphx's graph?

蓝咒 提交于 2019-12-06 07:25:14

I've found an answer.

In general, if you failed to achieve the result using edge styles or something else, then you should write your own layout.

It can be the layout written from scratch, or the layout extending or combining functionality of existing layout implementations.

Regarding my problem, I extend mxCompactTreeLayout overriding setEdgePoints method.

Source code:

PtCompactTreeLayout.java

package org.jsc.core.visualization.jgraphx;

import com.mxgraph.layout.mxCompactTreeLayout;
import com.mxgraph.model.mxGeometry;
import com.mxgraph.util.mxPoint;
import com.mxgraph.view.mxGraph;

import java.util.Collections;
import java.util.List;

public class PtCompactTreeLayout extends mxCompactTreeLayout
{
    public PtCompactTreeLayout( mxGraph graph )
    {
        super( graph, false );
    }

    @Override
    public void setEdgePoints( Object edge, List<mxPoint> points )
    {
        if ( !( edge instanceof IfElseEdge ) )
        {
            super.setEdgePoints( edge, points );

            return;
        }
        IfElseEdge ifElseEdge = ( IfElseEdge ) edge;
        IfElseNodeVertex v = ( IfElseNodeVertex ) ifElseEdge.getSource();
        mxGeometry geo = v.getGeometry();
        mxGeometry vb = ifElseEdge.getGeometry();
        if ( vb == null )
        {
            super.setEdgePoints( edge, points );

            return;
        }

        double xc = geo.getCenterX();
        double yc = geo.getCenterY();
        double w = geo.getWidth();
        double h = geo.getHeight();
        double xt;

        double yt;
        int i = ( ifElseEdge == v.getEdge( 0 ) ? 0 : 1 );
        PtNodeVertex vs = v.getSuccessor( i );

        mxGeometry sgeo0 = v.getSuccessor( 0 ).getGeometry();
        mxGeometry sgeo1 = v.getSuccessor( 1 ).getGeometry();

        double ws0 = sgeo0.getWidth();

        xt = ( i == 0 ? sgeo0.getCenterX() : sgeo1.getCenterX());
        yt = ( i == 0 ? sgeo0.getY() : sgeo1.getY() );

        vb.setTargetPoint( new mxPoint( xt, yt ) );

        double xm = xt;

        mxPoint mp = new mxPoint( xm, yc );
        vb.setPoints( Collections.singletonList( mp ) );
        vb.setSourcePoint( calcSourcePoint( v, i ) );
    }

    private mxPoint calcSourcePoint( PtNodeVertex v, int i )
    {
        mxGeometry geom = v.getGeometry();

        double w = geom.getWidth();
        double xs = ( i == 0 ? geom.getX() : geom.getX() + geom.getWidth() );
        double ys = geom.getCenterY();

        return new mxPoint( xs, ys );
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!