MPandroidchart一些点的颜色不变 [英] MPandroidchart some dot's color doesn't change

查看:144
本文介绍了MPandroidchart一些点的颜色不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作散点图,如果值超过特定值,

I'm making scatter chart that if value over specific value,

点的颜色已更改.

我写了这样的代码.

for (int i = 0; i < 30; i++) 
{  
       float y = (float) (Math.random()*0.2+0.1);
       value1.add(new Entry(i, y));

        if(y>=0.2f)
        {
            colors.add(getBaseContext().getResources().getColor(R.color.color_red));
        }
        else
            colors.add(getBaseContext().getResources().getColor(R.color.color_skyblue));
       }
}

结果在下面.

如您所见,有一行.

上面的颜色应该是红色,下面的颜色应该是蓝色.

Upper side's color should be red, lower side's color should be blue.

您会看到图表下方的正方形.

And you can see the square that below the graph.

例如,

正方形的数量与圆形的数量相同,9

the number of the squares is same as the number of circles, 9

但是只有一个圆圈是蓝色的.

but only one circle is blue.

我认为我的代码没有问题.

I think there is no problem in my codes.

但显然存在问题.

请让我解决这个问题.

谢谢.

推荐答案

您可以为参考上方和下方的点创建两组,并为两组分配颜色.

You can create two sets for the points above and below the reference and assign colors to the two sets.

ArrayList<Entry> aboveLevel = new ArrayList<>();
ArrayList<Entry> belowLevel = new ArrayList<>();

for (int i = 0; i < 30; i++){
    float y = (float) (Math.random()*0.2+0.1);
    if (y>=0.2f) {
       aboveLevel.add(new Entry(i, y));
    } else {
       belowLevel.add(new Entry(i, y));
    }
}

ScatterDataSet set1 = new ScatterDataSet(aboveLevel, "Above");
set1.setColor(ColorTemplate.COLORFUL_COLORS[0]);

ScatterDataSet set2 = new ScatterDataSet(belowLevel, "Below");
set2.setColor(ColorTemplate.COLORFUL_COLORS[1]);

ArrayList<IScatterDataSet> dataSets = new ArrayList<>();
dataSets.add(set1); // add the data sets
dataSets.add(set2);

// create a data object with the data sets
ScatterData data = new ScatterData(dataSets);

chart.setData(data);

这篇关于MPandroidchart一些点的颜色不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆