MPAndroidChart icon indicating copy to clipboard operation
MPAndroidChart copied to clipboard

How to add functionality so we can change Label Rotation Angle for Y-Axis similar to X-Axis

Open gitam2869 opened this issue 4 years ago • 1 comments

I am able to extend YAxisRenderer.java and I and tried following:

**protected void drawYLabels(Canvas c, float fixedPosition, float[] positions, float offset) { super.drawYLabels(c, fixedPosition, positions, offset);

    final int from = mYAxis.isDrawBottomYLabelEntryEnabled() ? 0 : 1;
    final int to = mYAxis.isDrawTopYLabelEntryEnabled()
            ? mYAxis.mEntryCount
            : (mYAxis.mEntryCount - 1);

    // draw
    for (int i = from; i < 1; i++) {

        String text = mYAxis.getFormattedLabel(i);

        c.rotate(15f);

        c.drawText(text, fixedPosition, positions[i * 2 + 1] + offset, mAxisLabelPaint);
    }
}**

But Unable to achieve expected output. Please give me some idea. Thanks in advance.

gitam2869 avatar Feb 08 '22 08:02 gitam2869

the same text need more space after rotate.maybe you can reference Utils class drawXAxisValue mothod.

key code

if (angleDegrees != 0.f) {

            // Move the text drawing rect in a way that it always rotates around its center
            drawOffsetX -= mDrawTextRectBuffer.width() * 0.5f;
            drawOffsetY -= lineHeight * 0.5f;

            float translateX = x;
            float translateY = y;

            // Move the "outer" rect relative to the anchor, assuming its centered
            if (anchor.x != 0.5f || anchor.y != 0.5f) {
                final FSize rotatedSize = getSizeOfRotatedRectangleByDegrees(
                        mDrawTextRectBuffer.width(),
                        lineHeight,
                        angleDegrees);

                translateX -= rotatedSize.width * (anchor.x - 0.5f);
                translateY -= rotatedSize.height * (anchor.y - 0.5f);
                FSize.recycleInstance(rotatedSize);
            }

            c.save();
            c.translate(translateX, translateY);
            c.rotate(angleDegrees);

            c.drawText(text, drawOffsetX, drawOffsetY, paint);

            c.restore();
        } else {
            if (anchor.x != 0.f || anchor.y != 0.f) {

                drawOffsetX -= mDrawTextRectBuffer.width() * anchor.x;
                drawOffsetY -= lineHeight * anchor.y;
            }

            drawOffsetX += x;
            drawOffsetY += y;

            c.drawText(text, drawOffsetX, drawOffsetY, paint);
        }

gs666 avatar Jun 09 '22 06:06 gs666