Categories
discuss

MPAndroidChart – How to display text to the left of LimitLine?

I’m trying to display text on the left of the LimitLine like this: However these are the only options I’m getting for setting the position of the Label for limit line. I’m using LimitLine….

I’m trying to display text on the left of the LimitLine like this:

enter image description here

However these are the only options I’m getting for setting the position of the Label for limit line.

enter image description here

I’m using LimitLine.LimitLabelPosition.LEFT_TOP and it only displays the Label above the Limit line.

enter image description here

YAxis leftAxis = mChart.getAxisLeft();
LimitLine minimumLimit = new LimitLine(50f, "Minimum Limit");
minimumLimit.setLineWidth(0.5f);
minimumLimit.setTextColor(ContextCompat.getColor(this, R.color.white_60_opacity));
minimumLimit.setLabelPosition(LimitLine.LimitLabelPosition.LEFT_TOP);
leftAxis.addLimitLine(minimumLimit);

How do I display the LimitLine‘s Label to the left of the LimitLine?

Edit:

I have also tried used the methods .setXOffset(50f) and .setYOffset(50f) but this only shifts the position of the label and not the line minimumLimit.

enter image description here

Answer

You can achieve this by using a custom YAxisRenderer with a little modification of the override method public void renderLimitLines(Canvas c).

The modifications needed for this purpose are:

1.To calculate the label width of each limit line to be able to move the limit line to the correct x position like below:

limitLinePath.moveTo(mViewPortHandler.contentLeft()+getLabelTextWidth(l), pts[1]);

2.To draw the label to the new x,y position something like this:

c.drawText(label, mViewPortHandler.contentLeft() + xOffset, pts[1]+l.getYOffset(), mLimitLinePaint);

Below is a custom MyYAxisRenderer containing the above modifications:

public class MyYAxisRenderer extends YAxisRenderer {

    private final Paint textPaint;

    public MyYAxisRenderer(ViewPortHandler viewPortHandler, YAxis yAxis, Transformer trans) {
        super(viewPortHandler, yAxis, trans);
        textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    @Override
    public void renderLimitLines(Canvas c) {

        List<LimitLine> limitLines = mYAxis.getLimitLines();

        if (limitLines == null || limitLines.size() <= 0)
            return;

        float[] pts = mRenderLimitLinesBuffer;
        pts[0] = 0;
        pts[1] = 0;
        Path limitLinePath = mRenderLimitLines;
        limitLinePath.reset();

        for (int i = 0; i < limitLines.size(); i++) {

            LimitLine l = limitLines.get(i);

            if (!l.isEnabled())
                continue;

            int clipRestoreCount = c.save();
            mLimitLineClippingRect.set(mViewPortHandler.getContentRect());
            mLimitLineClippingRect.inset(0.f, -l.getLineWidth());
            c.clipRect(mLimitLineClippingRect);

            mLimitLinePaint.setStyle(Paint.Style.STROKE);
            mLimitLinePaint.setColor(l.getLineColor());
            mLimitLinePaint.setStrokeWidth(l.getLineWidth());
            mLimitLinePaint.setPathEffect(l.getDashPathEffect());

            pts[1] = l.getLimit();

            mTrans.pointValuesToPixel(pts);

            limitLinePath.moveTo(mViewPortHandler.contentLeft()+getLabelTextWidth(l), pts[1]);
            limitLinePath.lineTo(mViewPortHandler.contentRight(), pts[1]);

            c.drawPath(limitLinePath, mLimitLinePaint);
            limitLinePath.reset();

            String label = l.getLabel();

            // if drawing the limit-value label is enabled
            if (label != null && !label.equals("")) {

                mLimitLinePaint.setStyle(l.getTextStyle());
                mLimitLinePaint.setPathEffect(null);
                mLimitLinePaint.setColor(l.getTextColor());
                mLimitLinePaint.setTypeface(l.getTypeface());
                mLimitLinePaint.setStrokeWidth(0.5f);
                mLimitLinePaint.setTextSize(l.getTextSize());

                final float labelLineHeight = Utils.calcTextHeight(mLimitLinePaint, label);
                float xOffset = getLimitLineXOffset(l);
                float yOffset = l.getLineWidth() + labelLineHeight + l.getYOffset();

                final LimitLine.LimitLabelPosition position = l.getLabelPosition();

                //draw the label on the left in the same y position of the limit line
                mLimitLinePaint.setTextAlign(Paint.Align.LEFT);
                c.drawText(label,
                        mViewPortHandler.contentLeft() + xOffset,
                        pts[1]+l.getYOffset(), mLimitLinePaint);
            }

            c.restoreToCount(clipRestoreCount);
        }
    }

    private float getLimitLineXOffset(LimitLine l){
        return Utils.convertDpToPixel(4f) + l.getXOffset();
    }

    private float getLabelTextWidth(LimitLine l) {

        String label = l.getLabel();
        if (label != null && !label.equals("")) {

            textPaint.setStyle(l.getTextStyle());
            textPaint.setPathEffect(null);
            textPaint.setColor(l.getTextColor());
            textPaint.setTypeface(l.getTypeface());
            textPaint.setStrokeWidth(0.5f);
            textPaint.setTextSize(l.getTextSize());

            int textWidth = Utils.calcTextWidth(textPaint, label);
            float xOffset = getLimitLineXOffset(l);

            return textWidth + (xOffset*2);
        }
        return 0;
    }
}

In the above renderer i have added two helper functions one for the calculation of the label text width private float getLabelTextWidth(LimitLine l) for a specific limit line and one to get the x offset of each limit line private float getLimitLineXOffset(LimitLine l) which you can modify based on your needs.

And you can use the above Renderer like the below:

lineChart.setRendererLeftYAxis(new MyYAxisRenderer(lineChart.getViewPortHandler(), lineChart.getAxisLeft(), lineChart.getTransformer(YAxis.AxisDependency.LEFT)));

Result:

limit_lines

Note: This was tested with v3.1.0 (‘com.github.PhilJay:MPAndroidChart:v3.1.0’)

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..