在JgraphX中旋转边缘标签

时间:2016-03-10 12:53:54

标签: java jgraphx

我的边缘是垂直的,我想相应地旋转边缘标签。

例如,将“helloworld.java”绘图部件替换为:

try
{
    Object v1 = graph.insertVertex(parent, "1", "TopLeft",
        20, 20, 80, 80);
    Object v3 = graph.insertVertex(parent, "2", "BottomLeft",
        20, 240, 80, 80);
    Object e1 = graph.insertEdge(parent, null, "edgelabel", 
        v1, v3, "dashed=true;endArrow=none;rotation=0");
}

设置rotation=90旋转边缘但不旋转标签。 我找到mxCurveLabelShape,但我不明白如何使用它。我尝试了一下,但是edge-mxCell不包含mxCurve。我试过了:

 List<mxPoint> pl = ((mxCell)e1).getGeometry().getPoints();
 mxCellState ecs1 = new mxCellState(graph.getView(),e1,null);
 mxCurveLabelShape cls = new mxCurveLabelShape(ecs1,(mxCurve)pl);

这显然无法工作,因为边缘mxCell也不包含非空的mxPoints。我可以使用边缘连接到其末端的位置吗? 有没有办法只处理标签并使用rotation

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

好的,我已经弄明白了,它并不漂亮,但它(还)不适用于正交边,即由水平和垂直线段组成的边。

我能让它发挥作用的方法是覆盖一个并为mxGraphComponent引入一个函数。也就是说,drawLabel函数现在检查标签是否来自边缘,如果是,则选择不同的createTemporaryGraphics函数。在后者中,边缘的框以及标签是已知的,并且在给定边框的情况下计算旋转。传递了一个布尔值(quadrant),它告诉边缘是在矩形(quadrant = true)的NorthEast和SouthWest之间,还是介于NorthWest和SouthEast(quadrant = false之间)。这在位置意义上目前不是最佳的(即,垂直标签可能在边缘的顶部结束)。但是我会为想要使用它的人留下调整;)

我们开始:(将其放在graphComponent

的任何地方
mxGraphComponent graphComponent = new mxGraphComponent(graph){
public mxInteractiveCanvas createCanvas()
    {
        return new mxInteractiveCanvas(){
            @Override
            public Object drawLabel(String text, mxCellState state, boolean html)
            {
                Map<String, Object> style = state.getStyle();
                mxIShape shapeL = getShape(style);
                mxITextShape shape = getTextShape(style, html);
                if (g != null && shape != null && drawLabels && text != null
                        && text.length() > 0)
                {
                    // Creates a temporary graphics instance for drawing this shape
                    float opacity = mxUtils.getFloat(style,
                            mxConstants.STYLE_TEXT_OPACITY, 100);
                    Graphics2D previousGraphics = g;
                    if(((mxCell) state.getCell()).isVertex()){
                        g = createTemporaryGraphics(style, opacity, null);                                  
                    }else{
                        //quadrant will be true if the edge is NE or SW
                        Object target = ((mxCell) state.getCell()).getTarget();
                        Object source = ((mxCell) state.getCell()).getSource();
                        boolean quadrant = false;
                        if(((mxCell)target).getGeometry().getCenterX()<((mxCell)source).getGeometry().getCenterX()){
                            if(((mxCell)target).getGeometry().getCenterY()>((mxCell)source).getGeometry().getCenterY()){
                                quadrant =  true;
                            }
                        }
                        if(((mxCell)target).getGeometry().getCenterX()>((mxCell)source).getGeometry().getCenterX()){
                            if(((mxCell)target).getGeometry().getCenterY()<((mxCell)source).getGeometry().getCenterY()){
                                quadrant =  true;
                            }
                        }
                        g = createTemporaryGraphics(style, opacity, state, state.getLabelBounds(),quadrant);
                    }

                    // Draws the label background and border
                    Color bg = mxUtils.getColor(style,
                            mxConstants.STYLE_LABEL_BACKGROUNDCOLOR);
                    Color border = mxUtils.getColor(style,
                            mxConstants.STYLE_LABEL_BORDERCOLOR);
                    paintRectangle(state.getLabelBounds().getRectangle(), bg, border);

                    // Paints the label and restores the graphics object
                    shape.paintShape(this, text, state, style);
                    g.dispose();
                    g = previousGraphics;
                }

                return shape;
            }
            public Graphics2D createTemporaryGraphics(Map<String, Object> style,
                    float opacity, mxRectangle bounds, mxRectangle labelbounds, boolean quad)
            {
                Graphics2D temporaryGraphics = (Graphics2D) g.create();

                // Applies the default translate
                temporaryGraphics.translate(translate.x, translate.y);

                // setup the rotation for the label
                double angle = java.lang.Math.atan(bounds.getHeight()/bounds.getWidth());
                double rotation = Math.toDegrees(angle);
                if(quad){
                    rotation = - rotation;
                }
                //get the translation needed
                double diff = labelbounds.getHeight()*(1-Math.cos(angle));
                double plusy = diff * Math.sin(angle);
                double plusx = diff * Math.cos(angle);
                // Applies the rotation and translation on the graphics object
                if (bounds != null)
                {
                    if (rotation != 0)
                    {
                        temporaryGraphics.rotate(Math.toRadians(rotation),
                                labelbounds.getCenterX(), labelbounds.getCenterY());
                        temporaryGraphics.translate(
                                - plusx, plusy);
                    }
                }

                // Applies the opacity to the graphics object
                if (opacity != 100)
                {
                    temporaryGraphics.setComposite(AlphaComposite.getInstance(
                            AlphaComposite.SRC_OVER, opacity / 100));
                }

                return temporaryGraphics;
            }
        };
    }

};