基于y轴数值,在柱状图中为柱子设置不同的颜色 - MPAndroidChart

23

我正在使用MPAndroid图表绘制条形图。现在所有的条都是相同颜色的,但我想根据Y轴值为条形设置不同的颜色,比如如果值>100,颜色=红色,就像下面的图片一样。这可能吗?请有人帮忙。

输入图像描述

谢谢。

6个回答

49
您可以重写BarDataSet类以实现此目的。
public class MyBarDataSet extends BarDataSet {


    public MyBarDataSet(List<BarEntry> yVals, String label) {
        super(yVals, label);
    }

    @Override
    public int getColor(int index) {
        if(getEntryForXIndex(index).getVal() < 95) // less than 95 green
            return mColors.get(0);
        else if(getEntryForXIndex(index).getVal() < 100) // less than 100 orange
            return mColors.get(1);
        else // greater or equal than 100 red
            return mColors.get(2);
    }

}

你需要像这样定义你的颜色:

MyBarDataSet set = new MyBarDataSet(yVals, "");
set.setColors(new int[]{ContextCompat.getColor(context, R.color.green), 
                ContextCompat.getColor(context, R.color.orange), 
                ContextCompat.getColor(context, R.color.red)});
ArrayList<BarDataSet> dataSets = new ArrayList<>();
dataSets.add(set);


BarData data = new BarData(xVals, dataSets);

2
确保在数据集上不要调用set.setColor(..)。这样会导致getColor永远不会被调用。 - Kalel Wade
@m4n3k4s 这是一个非常需要的解决方案,适用于柱状图中柱子颜色取决于 Y 值的情况。感谢加10分。 - Amit Trivedi
BarData data = new BarData(xVals, set);?因为dataSets没有被赋值。 - Shadow
我正在使用这个代码:getApplicationContext().getResources().getColor(android.R.color.holo_orange_dark),但是它似乎已经过时了...有没有其他的代码行可以替换它?我只是想获取不同的颜色,比如黄色,但是使用我附加的代码行不起作用。 - neo
嗨@neo,你应该使用ContextCompat.getColor(context, R.color.color_name)。同时我更新了答案。 - m4n3k4s
显示剩余4条评论

6
您可以从此链接找到有关在MPAndroidChart中设置颜色的文档。
    LineDataSet setComp1 = new LineDataSet(valsComp1, "Company 1");
  // sets colors for the dataset, resolution of the resource name to a "real" color is done internally
  setComp1.setColors(new int[] { R.color.red1, R.color.red2, R.color.red3, R.color.red4 }, Context);

  LineDataSet setComp2 = new LineDataSet(valsComp2, "Company 2");
  setComp2.setColors(new int[] { R.color.green1, R.color.green2, R.color.green3, R.color.green4 }, Context);

很高兴听到这个消息 :) - Alex Chengalan
@AlexChengalan,我们能否根据MPAndroidChart中的条目(y轴)值设置条形颜色?如果不能,请建议支持此功能的库。 - Karthikeyan Ve
1
@Alex Chengalan,我们能否根据 MPAndroidChart 中的条目(y 轴)值设置条形颜色?如果不能,请建议支持此功能的库。 - Karthikeyan Ve
如何根据数据值设置颜色。例如,如果数据小于50,我会给绿色,如果数据大于50,我会给红色。 - viper

5

向@m4n3k4s所提供的答案点赞。只是想进一步澄清这些行,因为我花了一段时间才明白:

set.setColors(new int[]{ContextCompat.getColor(context, R.color.green), 
            ContextCompat.getColor(context, R.color.orange), 
            ContextCompat.getColor(context, R.color.red)});

在我找到这个帖子之前,我不知道什么是context或如何使用它。 我将上面的行替换为:

barDataSet.setColors(
                ContextCompat.getColor(barchart.getContext(), R.color.green),
                ContextCompat.getColor(barchart.getContext(), R.color.yellow),
                ContextCompat.getColor(barchart.getContext(), R.color.red)
        );

考虑到BarChartView的一个子类,而getContext()View类的一个方法。因此,我的完整代码看起来像这样,其中KpBarDataSet是我自定义的类,它覆盖了BarDataSet,并且DateKp是一个自定义类。
BarChart barchart = (BarChart) findViewById(R.id.barchart);

    List<BarEntry> entries = new ArrayList<BarEntry>();
    List<String> dateLabels = new ArrayList<>();

    int i = 0;
    for (DateKp day : data) {
        // turn your data into Entry objects
        entries.add(new BarEntry(i, day.getValueY()));
        dateLabels.add(day.getLabel());
        i++;
    }

    KpBarDataSet barDataSet = new KpBarDataSet(entries, null);

    barDataSet.setColors(
            ContextCompat.getColor(barchart.getContext(), R.color.green),
            ContextCompat.getColor(barchart.getContext(), R.color.yellow),
            ContextCompat.getColor(barchart.getContext(), R.color.red)
    );

最后,R.color.greenR.color.red等在/res/values/colors.xml中定义之前是不存在的。
希望这能帮到你。

2
我已经为3种颜色做了这个,而且它有效。
if(floatArray.get(i) >= 0.0 && floatArray.get(i) <= max1)
            {
                barColorArray1[i] = Color.rgb(0, 128, 0);           
            }
            else if(floatArray.get(i) > max1 && floatArray.get(i) <= max2)
            {
                barColorArray1[i] = Color.rgb(247, 207, 19);                
            }
            else if(floatArray.get(i) > max2 )
            {
                barColorArray1[i] = Color.rgb(199, 0, 15);              
            }

我已创建一个包含颜色整数值的数组。最后将其作为BarDataSet传递给barDataSet1.setColors(barColorArray1);


这是应用自定义条件的最简单方法。 - Ravi Yadav

2
这是MPAndroidChart:v3.0.2的类:
class MyBarDataSet extends BarDataSet {
    public MyBarDataSet(List<BarEntry> yVals, String label) {
        super(yVals, label);
    }

    @Override
    public int getColor(int index) {
        if(getEntryForIndex(index).getY() < 140)
            return mColors.get(0);
        else if(getEntryForIndex(index).getY() > 145)
            return mColors.get(1);
        else
            return mColors.get(2);
    }

}


getEntryForIndex 是从哪里来的? - unkwndev
从这里开始 "extends BarDataSet" - Diego Schmaedech

0

更新

来自m4n3k4的答案

public class MyBarDataSet extends BarDataSet {


    public MyBarDataSet(List<BarEntry> yVals, String label) {
        super(yVals, label);
    }

     @Override
    public int getColor(int index) {
        if(getEntryForIndex(index).getY() ==40) // less than 95 green
            return mColors.get(0);
        else if(getEntryForIndex(index).getY() ==30) // less than 100 orange
            return mColors.get(1);
        else // greater or equal than 100 red
            return mColors.get(2);
    }
}

并且在Java代码中

 set1.setColors(ContextCompat.getColor(StepCountsActivity.this, R.color.purple),
                ContextCompat.getColor(StepCountsActivity.this, R.color.light_purple),
                ContextCompat.getColor(StepCountsActivity.this, R.color.blue));

    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接