【问题标题】:MPAndroidChart: Can I set different colours for the x-axis labels?MPAndroidChart:我可以为 x 轴标签设置不同的颜色吗?
【发布时间】:2016-09-29 14:08:34
【问题描述】:

所以我希望能够在我的条形图中选择一个条形,当我选择一个条形时,它会改变条形的颜色(我知道怎么做),同时也会改变相应 x 的颜色-轴标签。有没有办法做到这一点,如果可以,有人可以帮助我吗?

【问题讨论】:

标签: android mpandroidchart


【解决方案1】:

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

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&lt;Integer&gt;(不是资源ID),其大小与IDataSet 中的条目数相同。由于您已经知道如何更改高亮条的颜色,所以这部分取决于您。只需像平常一样操作colors。这是一个示例输出:

【讨论】:

  • 非常有帮助!很抱歉花了很长时间来验证它。
  • 您好我正在尝试使用此代码,但缺少 MPPointF 等内容...我怎样才能获得此代码
  • @SunishaSindhu 哪个版本的 MPAndroidChart?这里的答案适用于 MPAndroidChart 3.0.1 及更高版本
  • @DavidRawson 我也可以通过点击更改颜色吗?
猜你喜欢
  • 2015-04-22
  • 1970-01-01
  • 1970-01-01
  • 2017-09-04
  • 1970-01-01
  • 1970-01-01
  • 2014-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多