How to change dot colors if value is higher than c

2019-03-05 11:16发布

问题:

I need to draw red circles if value higher than 3. How to realize that? I've read that I should override method drawCircles but I dont understand where I should do this.

    LineDataSet set1;

    set1 = new LineDataSet(entries, "");
    chart.getLegend().setEnabled(false);

    set1.setColor(Color.WHITE);
    set1.setCircleColor(Color.WHITE);
    set1.setLineWidth(2f);
    set1.setCircleRadius(4f);
    set1.setValueTextSize(9f);
    set1.setValueTextColor(Color.WHITE);
    ArrayList<ILineDataSet> dataSets = new ArrayList<>();
    dataSets.add(set1); // add the datasets

    // create a data object with the datasets
    LineData data = new LineData(xVals, dataSets);
    chart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
    chart.getAxisRight().setEnabled(false);
    chart.setScaleMinima(6f, 1f);
    if (measure.equals("co")) {
        LimitLine ll = new LimitLine(CO_CRITICAL, getResources().getString(R.string.critical_co));
        ll.setLineColor(Color.RED);
        ll.setLineWidth(1f);
        ll.setTextColor(Color.WHITE);
        ll.setTextSize(10f);
        chart.getAxisLeft().addLimitLine(ll);
    } else if (measure.equals("no2")) {
        LimitLine ll = new LimitLine(NO2_CRITICAL, getResources().getString(R.string.critical_no2));
        ll.setLineColor(Color.RED);
        ll.setLineWidth(1f);
        ll.setTextColor(Color.WHITE);
        ll.setTextSize(10f);
        chart.getAxisLeft().addLimitLine(ll);
    }

    // set data
    chart.setData(data);

回答1:

Try with this:

Define one ArrayList:

ArrayList<Integer> color = new ArrayList<>();

And add your condition as:

if (YOUR_CONDITION) {
    color.add(ColorTemplate.rgb("#f8bf94"));
    yVals1.add(new Entry(VALUE, COUNTER));
} else {
    color.add(ColorTemplate.rgb("#e0e0e0"));
    yVals1.add(new Entry(VALUE, COUNTER));
}

And before adding dataset, add

set1.setColors(color);

For your reference, you can check this link.

Hope this answer will help you.