我正在使用MPAndroid图表绘制条形图。现在我的所有条形都有相同的颜色,但我想根据Y轴值为条形设置不同的颜色,如果值> 100。 color = red,如下图所示。那可能吗?有人请帮帮我。
问候。
答案 0 :(得分:36)
您可以覆盖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);
答案 1 :(得分:5)
您可以在this link
中找到有关在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);
答案 2 :(得分: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);
答案 3 :(得分: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); } }
答案 4 :(得分:2)
赞同@ m4n3k4s的答案。只是想对这些内容添加一些说明,因为我花了一段时间才弄明白:
set.setColors(new int[]{ContextCompat.getColor(context, R.color.green),
ContextCompat.getColor(context, R.color.orange),
ContextCompat.getColor(context, R.color.red)});
在找到this thread之前,我还不知道context
是什么或如何使用它。我用以下代码替换了上面的行:
barDataSet.setColors(
ContextCompat.getColor(barchart.getContext(), R.color.green),
ContextCompat.getColor(barchart.getContext(), R.color.yellow),
ContextCompat.getColor(barchart.getContext(), R.color.red)
);
鉴于BarChart
是View
的子类,而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.green
,R.color.red
等在您在/res/values/colors.xml中定义之前不会存在。
希望这有帮助。
答案 5 :(得分: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);
}
}
和JavaCode
set1.setColors(ContextCompat.getColor(StepCountsActivity.this, R.color.purple),
ContextCompat.getColor(StepCountsActivity.this, R.color.light_purple),
ContextCompat.getColor(StepCountsActivity.this, R.color.blue));
}