mpChart如何根据y值的范围赋给bar需要的颜色值

这篇转载自http://stackoverflow.com/questions/29791086/how-to-set-colors-in-mpandroidchart

我们需要继承BarDataSet ,重写下其中的getColor,
getEntryForXIndex(index).getVal() ,可以得到当前的y值
mColors.get(0),根据赋予的colors集合的位置赋予相应的颜色

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

}

设置颜色在这里

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


BarData data = new BarData(xVals, dataSets);
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值