【问题标题】:MPandroidchart some dot's color doesn't changeMPandroidchart 一些点的颜色没有改变
【发布时间】:2019-08-06 04:53:52
【问题描述】:

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

点的颜色改变了。

我写了这样的代码。

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

结果如下。

如你所见,有一条线。

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

你可以看到图表下方的正方形。

例如,

正方形的个数和​​圆的个数一样,都是9

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

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

但显然有问题。

请让我解决这个问题。

谢谢。

【问题讨论】:

    标签: android mpandroidchart


    【解决方案1】:

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

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

    【讨论】:

    • 哇!多么绝妙的主意!感谢您分享您的好主意!这是工作!再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 2016-12-16
    相关资源
    最近更新 更多