MPAndroidChart:我可以为x轴标签设置不同的颜色吗? [英] MPAndroidChart: Can I set different colours for the x-axis labels?

查看:284
本文介绍了MPAndroidChart:我可以为x轴标签设置不同的颜色吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我希望能够在条形图中选择一个条形,当我选择一个条形时,它会更改条形的颜色(我知道该怎么做),而且还要更改相应x的颜色.轴标签.有没有办法做到这一点,如果可以,有人可以帮我吗?

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?

推荐答案

是的,可以为xAxis标签设置不同的颜色.您将必须使用自定义渲染器,如下所示:

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);
    }
}

像这样食用:

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

其中,colors是解析颜色的List<Integer>(不是资源ID),其大小与IDataSet中的条目数相同.由于您已经知道如何更改高光条的颜色,因此该部分由您决定.只需以与通常相同的方式操作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:

这篇关于MPAndroidChart:我可以为x轴标签设置不同的颜色吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆