这篇转载自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);

本文介绍如何使用MPAndroidChart库自定义柱状图的颜色。通过继承BarDataSet并重写getColor方法,可以根据y值的不同区间为柱状图设置绿色、橙色和红色三种颜色。
2696

被折叠的 条评论
为什么被折叠?



