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);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章