【发布时间】:2015-07-05 11:59:03
【问题描述】:
我正在使用 MPAndroid 图表绘制条形图。现在我所有的条都具有相同的颜色,但我希望基于 Y 轴值的条具有不同的颜色,例如值 >100。颜色 = 红色,如下图所示。那可能吗?有人请帮助我。
问候。
【问题讨论】:
标签: android
我正在使用 MPAndroid 图表绘制条形图。现在我所有的条都具有相同的颜色,但我希望基于 Y 轴值的条具有不同的颜色,例如值 >100。颜色 = 红色,如下图所示。那可能吗?有人请帮助我。
问候。
【问题讨论】:
标签: android
您可以重写 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);
【讨论】:
您可以从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);
【讨论】:
赞许@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 中定义它们。
希望这会有所帮助。
【讨论】:
我已经为 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);
【讨论】:
这里是 MPAndroidChart:v3.0.2 的类
类 MyBarDataSet 扩展 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);
}
}
【讨论】:
更新
表格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);
}
}
在Java代码中
set1.setColors(ContextCompat.getColor(StepCountsActivity.this, R.color.purple),
ContextCompat.getColor(StepCountsActivity.this, R.color.light_purple),
ContextCompat.getColor(StepCountsActivity.this, R.color.blue));
}
【讨论】: