如何在androidmpchart中显示相应颜色的标签?

时间:2016-05-18 11:53:49

标签: android graph mpandroidchart

嗨我根据条件和图表显示不同颜色的图表也很好。现在我的问题是,如何根据标签中相应的颜色显示文字? 我附上了截图。

在下面的屏幕截图中,三种颜色和三种颜色后,文本显示为低,平均和高显示。 相反,我需要显示为绿色,相应的文字为低。红色,相应的文字为平均值,紫色,相应文字为高。

enter image description here

我尝试的是,

public class CustomBarDataSet extends BarDataSet {

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

@Override
public int getColor(int index) {
    if(getEntryForXIndex(index).getVal() < 20) //  green.. low
        return mColors.get(0);
    else if(getEntryForXIndex(index).getVal() < 50) // 50  red average
        return mColors.get(1);
    else if(getEntryForXIndex(index).getVal() < 100) // 50  violet high
        return mColors.get(2);
    else // greater or equal than 100 red
        return mColors.get(2); //violet
}
//i don't know how to use this. 
@Override
public String getLabel() {
    return super.getLabel();
}

}

在MainActivity中:

   CustomBarDataSet set = new CustomBarDataSet(entries, "low,average,high");
                set.setColors(new int[]{getResources().getColor(R.color.green_color),
                        getResources().getColor(R.color.red_button),
                        getResources().getColor(R.color.violet_color)});
                ArrayList<BarDataSet> dataSets = new ArrayList<>();
                dataSets.add(set);
                BarDataSet dataset = new BarDataSet(entries, "");
                BarData data = new BarData(labels, set);

1 个答案:

答案 0 :(得分:0)

这可以帮到你:

Legend l = chart.getLegend();
l.setCustom(new int[] {Color.GREEN, Color.RED, Color.rgb(238,130,238)},new String[] {"low", "average", "high"});