How to change dot colors if value is higher than constant in MPAndroidChart

女生的网名这么多〃 提交于 2019-11-28 12:07:11

问题


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.



来源:https://stackoverflow.com/questions/37268519/how-to-change-dot-colors-if-value-is-higher-than-constant-in-mpandroidchart

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