如何不在折线图上绘制负值-MPAndroidChart?

时间:2020-04-02 22:47:16

标签: android mpandroidchart

当我的值是负数时,我试图通过set.setColor(Color.Transparent)隐藏它。但这是行不通的。

这是我的代码:

public void addEntry() {
    LineData data = mChart.getData();

    if (data != null ) {

        ILineDataSet set = data.getDataSetByIndex(0);

        if(getYValue() > 0) {
            Log.d(TAG,"blue color");
            set = createSet();
            data.addDataSet(set);
            data.addEntry(new Entry(xValue, getYValue()), 0); //Y value is random from -5 to 10
            data.notifyDataChanged();
        } else  {
            Log.d(TAG,"transparent");
            set = createSetTransparent();
            data.addDataSet(set);
            data.addEntry(new Entry(xValue, getYValue()), 0);
        }
        xValue +=1;
        data.notifyDataChanged();

        mChart.notifyDataSetChanged();

        mChart.setVisibleXRangeMaximum(1028);

        mChart.moveViewToX(data.getEntryCount());

        mChart.invalidate();
    }
}

public LineDataSet createSetTransparent() {
    LineDataSet set = new LineDataSet(null, null);
    set.setLineWidth(3f);
    set.setColor(Color.TRANSPARENT);
    set.setHighlightEnabled(true);
    set.setDrawValues(true);
    set.setDrawCircles(false);
    set.setValueTextColor(Color.WHITE);
    return set;
}

public LineDataSet createSet() {
    LineDataSet set = new LineDataSet(null, null);
    set.setLineWidth(3f);
    set.setColor(Color.parseColor("#2094A5"));
    set.setHighlightEnabled(true);
    set.setDrawValues(true);
    set.setDrawCircles(false);
    set.setValueTextColor(Color.WHITE);
    return set;
}

我的问题如下 addDataSet()将始终获得我的第一个设置。 例如:即使我的值是negatif,我的行也始终是蓝色的。反之亦然。

有人对替换 addDataSet 中的 set 有任何想法吗?值为负数时,或者不绘制线条

0 个答案:

没有答案