MPandroidchart some dot's color doesn't change

寵の児 提交于 2019-12-24 18:23:21

问题


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

the dot's color is changed.

I wrote codes like these.

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));
       }
}

And the result is below.

As you can see, there is a line.

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

And you can see the square that below the graph.

for example,

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.

But clearly there is a problem.

Please let me solve this problem.

Thanks.


回答1:


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);


来源:https://stackoverflow.com/questions/57369154/mpandroidchart-some-dots-color-doesnt-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!