Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPAndroidChart: Can I set different colours for the x-axis labels?

So I want to be able to select a bar in my bar chart, and when I select a bar, it changes the colour of the bar (which I know how to do), but also change the colour of the corresponding x-axis label. Is there a way to do this, if so, can someone help me please?

like image 888
rohan Avatar asked Sep 29 '16 14:09

rohan


People also ask

How do you change the color of the X-axis?

Data Visualization using R Programming To change the color of X-axis line for a graph using ggplot2, we can use theme function where we can set the axis. line. x. bottom argument color to desired color with element_line.

How you can display label in X-axis?

Right-click the category labels to change, and click Select Data. In Horizontal (Category) Axis Labels, click Edit. In Axis label range, enter the labels you want to use, separated by commas.

Which function lets you set label for X-axis in charts?

To include numeric variables with text in a label, use the num2str function. For example: x = 42; txt = ['The value is ',num2str(x)];


1 Answers

Yes it is possible to set different colors for the xAxis labels. You will have to use a custom renderer, something like below:

import android.graphics.Canvas;

import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.renderer.XAxisRenderer;
import com.github.mikephil.charting.utils.MPPointF;
import com.github.mikephil.charting.utils.Transformer;
import com.github.mikephil.charting.utils.Utils;
import com.github.mikephil.charting.utils.ViewPortHandler;

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

/**
 * Created by rawsond on 29/01/17.
 */

public class ColoredLabelXAxisRenderer extends XAxisRenderer {

    List<Integer> labelColors;

    public ColoredLabelXAxisRenderer(ViewPortHandler viewPortHandler, XAxis xAxis, Transformer trans) {
        super(viewPortHandler, xAxis, trans);
        labelColors = Collections.EMPTY_LIST;
    }

    public ColoredLabelXAxisRenderer(ViewPortHandler viewPortHandler, XAxis xAxis, Transformer trans, List<Integer> colors) {
        super(viewPortHandler, xAxis, trans);
        this.labelColors = colors;
    }

    @Override
    protected void drawLabels(Canvas c, float pos, MPPointF anchor) {
        final float labelRotationAngleDegrees = mXAxis.getLabelRotationAngle();
        boolean centeringEnabled = mXAxis.isCenterAxisLabelsEnabled();

        float[] positions = new float[mXAxis.mEntryCount * 2];

        for (int i = 0; i < positions.length; i += 2) {

            // only fill x values
            if (centeringEnabled) {
                positions[i] = mXAxis.mCenteredEntries[i / 2];
            } else {
                positions[i] = mXAxis.mEntries[i / 2];
            }
        }

        mTrans.pointValuesToPixel(positions);

        for (int i = 0; i < positions.length; i += 2) {

            float x = positions[i];

            if (mViewPortHandler.isInBoundsX(x)) {

                String label = mXAxis.getValueFormatter().getFormattedValue(mXAxis.mEntries[i / 2], mXAxis);
                int color = getColorForXValue(mXAxis.mEntries[i / 2]); //added

                mAxisLabelPaint.setColor(color);

                if (mXAxis.isAvoidFirstLastClippingEnabled()) {

                    // avoid clipping of the last
                    if (i == mXAxis.mEntryCount - 1 && mXAxis.mEntryCount > 1) {
                        float width = Utils.calcTextWidth(mAxisLabelPaint, label);

                        if (width > mViewPortHandler.offsetRight() * 2
                                && x + width > mViewPortHandler.getChartWidth())
                            x -= width / 2;

                        // avoid clipping of the first
                    } else if (i == 0) {

                        float width = Utils.calcTextWidth(mAxisLabelPaint, label);
                        x += width / 2;
                    }
                }

                drawLabel(c, label, x, pos, anchor, labelRotationAngleDegrees);
            }
        }
    }

    private int getColorForXValue(int index) {
        if (index >= labelColors.size()) return mXAxis.getTextColor();

        if (index < 0) return mXAxis.getTextColor();

        return labelColors.get(index);
    }
}

Consume it like this:

mChart.setXAxisRenderer(new ColoredLabelXAxisRenderer(mChart.getViewPortHandler(), mChart.getXAxis(), mChart.getTransformer(AxisDependency.LEFT), colors));

where colors is a List<Integer> of resolved colors (not resource ids) the same size as the number of entries in the IDataSet. Since you already know how to change the color of the bar on a highlight, that part is up to you. Just manipulate colors in the same way you would normally. Here is a sample output:

bar chart with colors matching the bars

like image 69
David Rawson Avatar answered Sep 28 '22 10:09

David Rawson