Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPAndroidChart barchart y axis label colors

Is it possible to set different label color for y-axis label in mpandroidchart ? is possible how? any help appreciated.

like image 276
Joe Avatar asked Sep 07 '25 04:09

Joe


2 Answers

Add below code of line

chart.getAxisLeft().setTextColor(ContextCompat.getColor(this, R.color.red)); // left y-axis

enter image description here

like image 143
Jyot Avatar answered Sep 09 '25 02:09

Jyot


You can custom YAxisRenderer like this to achieve it. Add it to your chart chart.setRendererLeftYAxis(new MyYAxisLeftRenderer(chart.getViewPortHandler(), chart.getAxisLeft(),chart.getTransformer(YAxis.AxisDependency.LEFT)));

public class MyYAxisLeftRenderer extends YAxisRenderer {


@Override
protected void drawYLabels(Canvas c, float fixedPosition, float[] positions, float offset) {
    final int from = mYAxis.isDrawBottomYLabelEntryEnabled() ? 0 : 1;
    final int to = mYAxis.isDrawTopYLabelEntryEnabled()
            ? mYAxis.mEntryCount
            : (mYAxis.mEntryCount - 1);

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

        String text = mYAxis.getFormattedLabel(i);

        // change y label color here before drawing (i is an index of label)
         mAxisLabelPaint.setColor();

        c.drawText(text, fixedPosition, positions[i * 2 + 1] + offset, mAxisLabelPaint);
    }
}
like image 44
tho nguyen Avatar answered Sep 09 '25 00:09

tho nguyen