MPAndroidChart,根据y轴值为条形图中的条形设置不同的颜色
我正在用MPAndroid图表绘制条形图。现在我所有的条都有相同的颜色,但我希望基于Y轴值的条有不同的颜色,比如如果值>100。颜色=红色,如下图所示。可能吗?有人请帮帮我MPAndroidChart,根据y轴值为条形图中的条形设置不同的颜色,android,Android,我正在用MPAndroid图表绘制条形图。现在我所有的条都有相同的颜色,但我希望基于Y轴值的条有不同的颜色,比如如果值>100。颜色=红色,如下图所示。可能吗?有人请帮帮我 问候。您可以从以下位置找到有关在MPAndroidChart中设置颜色的文档: 您可以重写BarDataSet类来实现这一点 public class MyBarDataSet extends BarDataSet { public MyBarDataSet(List<BarEntry> yVals
问候。您可以从以下位置找到有关在MPAndroidChart中设置颜色的文档:
您可以重写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扩展了BarDataSet{
公共MyBarDataSet(列表YVAL,字符串标签){
超级(yVals,标签);
}
@凌驾
公共整数getColor(整数索引){
if(getEntryForXIndex(index.getVal()<95)//小于95绿色
返回mColors.get(0);
else if(getEntryForXIndex(index).getVal()<100)//小于100橙色
返回mColors.get(1);
else//大于或等于100红色
返回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);
MyBarDataSet=新的MyBarDataSet(yVals,”);
set.setColors(新int[]{ContextCompat.getColor(context,R.color.green),
ContextCompat.getColor(context,R.color.orange),
getColor(context,R.color.red)});
ArrayList数据集=新的ArrayList();
数据集。添加(集合);
BarData数据=新的BarData(xVAL,数据集);
我用了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);
}
if(floatArray.get(i)>=0.0&&floatArray.get(i)max1&&floatArray.get(i)max2)
{
barColorArray1[i]=Color.rgb(199,0,15);
}
在本文中,我创建了一个带有整数值颜色的数组。
最后,将其作为
barDataSet1.setColors(barColorArray1)传递到BarDataSet
中代码>这是MPAndroidChart的类:v3.0.2
MyBarDataSet类扩展了BarDataSet{
set.setColors(new int[]{ContextCompat.getColor(context, R.color.green),
ContextCompat.getColor(context, R.color.orange),
ContextCompat.getColor(context, R.color.red)});
公共MyBarDataSet(列表YVAL,字符串标签){
超级(yVals,标签);
}
@凌驾
公共整数getColor(整数索引){
if(getEntryForIndex(index).getY()<140)
返回mColors.get(0);
else if(getEntryForIndex(index).getY()>145)
返回mColors.get(1);
其他的
返回mColors.get(2);
}
}
对@m4n3k4s的答案竖起大拇指。我只是想澄清一下这些行,因为我花了一段时间才弄明白:
barDataSet.setColors(
ContextCompat.getColor(barchart.getContext(), R.color.green),
ContextCompat.getColor(barchart.getContext(), R.color.yellow),
ContextCompat.getColor(barchart.getContext(), R.color.red)
);
我不知道什么是context
,也不知道如何使用它,直到我找到了它。我将上面的行替换为:
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)
);
假设条形图
是视图
的子类,getContext()
是视图
类的方法
所以我的完整代码如下所示,其中KpBarDataSet是覆盖BarDataSet的类,DateKp是一个自定义类
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);
}
}
BarChart BarChart=(BarChart)findviewbyd(R.id.BarChart);
列表项=新的ArrayList();
List dateLabels=new ArrayList();
int i=0;
对于(日期kp天:数据){
//将数据转换为输入对象
add(新的BarEntry(i,day.getValueY());
dateLabels.add(day.getLabel());
i++;
}
KpBarDataSet barDataSet=新KpBarDataSet(条目,null);
barDataSet.setColors(
getColor(barchart.getContext(),R.color.green),
getColor(barchart.getContext(),R.color.yellow),
ContextCompat.getColor(barchart.getContext(),R.color.red)
);
最后,R.color.green
,R.color.red
等将不存在,除非您在/res/values/colors.xml中定义它们
希望这有帮助。更新
表格答案
很高兴听到:)@AlexChengalan我们可以根据MPAndroidChart中的条目(y轴)值设置条形图的颜色吗?如果没有,请建议支持相同的库。@Alex Chengalan我们可以根据MPAndroidChart中的条目(y轴)值设置条形图颜色吗?如果没有,请建议支持相同的库。如何根据数据值设置颜色。例如,如果数据小于50,我将给出绿色,如果它大于50,我将给出红色。请确保在数据集上,您也不调用set.setColor(…)。这将使getColor永远不会被调用。@m4n3k4s这是条形图最需要的解决方案,其中条形图的颜色取决于Y值。谢谢+10.BarData数据=新的BarData(xVals,set);?因为数据集没有赋值。我使用的是getApplicationContext().getResources().getColor(android.R.color.holo_orange_dark),但这似乎已被弃用……是否还有其他代码可以替换?我只是想得到不同的颜色,比如黄色,但是使用我附加的代码行不起作用。Hi@neo你应该使用ContextCompat.getColor(context,R.color.color\u name)。同时,我更新了答案这是应用自定义条件的最简单方法。getEntryForIndex从何而来?从这里“扩展BarDataSet”
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)
);
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);
}
}
set1.setColors(ContextCompat.getColor(StepCountsActivity.this, R.color.purple),
ContextCompat.getColor(StepCountsActivity.this, R.color.light_purple),
ContextCompat.getColor(StepCountsActivity.this, R.color.blue));
}