androidchartsmpandroidchartcubic-bezier

MPAndroid Chart - LimitLine and Cubic Bezier drawing - curved line exceeds min-max limit lines


The screenshot shows the chart with and without cubic-bezier mode for the same data. As you can see when cubic bezier mode is enabled the line ecxeeds the min and max values which I am required to show in the chart as LimitLine and it looks very bad.

Is there any way to fix this??

enter image description here

private void setData(int count, float range) {

    ArrayList<Entry> yVals = new ArrayList<Entry>();

    float max = - Float.MAX_VALUE, min = Float.MAX_VALUE;
    for (int i = 0; i < count; i++) {
        float mult = (range + 1);
        float val = (float) (Math.random() * mult) + 20;// + (float)
                                                       // ((mult *
                                                       // 0.1) / 10);
        yVals.add(new Entry(i, val));

        if(val<min){
            min = val;
        }
        if(val>max){
            max = val;
        }

    }

    LineDataSet set1;

    if (mChart.getData() != null &&
            mChart.getData().getDataSetCount() > 0) {
        set1 = (LineDataSet)mChart.getData().getDataSetByIndex(0);
        set1.setValues(yVals);
        mChart.getData().notifyDataChanged();
        mChart.notifyDataSetChanged();
    } else {
        // create a dataset and give it a type
        set1 = new LineDataSet(yVals, "DataSet 1");

        set1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
        set1.setCubicIntensity(0.2f);
        //set1.setDrawFilled(true);
        set1.setDrawCircles(false);
        set1.setLineWidth(1.8f);
        set1.setCircleRadius(4f);
        set1.setCircleColor(Color.BLACK);
        set1.setHighLightColor(Color.rgb(244, 117, 117));
        set1.setColor(Color.parseColor("#16315d"));
        set1.setFillColor(Color.RED);
        GradientDrawable d = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, new int[]{Color.BLACK, Color.RED});
        set1.setFillAlpha(220);
        set1.setFillDrawable(d);


        set1.setFillAlpha(100);
        set1.setDrawHorizontalHighlightIndicator(false);
        set1.setFillFormatter(new IFillFormatter() {
            @Override
            public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) {
                return -10;
            }
        });

        // create a data object with the datasets
        LineData data = new LineData(set1);
        data.setValueTypeface(mTfLight);
        data.setValueTextSize(9f);
        data.setDrawValues(false);

        // set data
        mChart.setData(data);
    }

    YAxis yAxis = mChart.getAxisLeft();
    yAxis.removeAllLimitLines();

    LimitLine lMin = new LimitLine(min, Float.toString(min));
    lMin.setLabelPosition(LimitLine.LimitLabelPosition.LEFT_BOTTOM);

    LimitLine lMax = new LimitLine(max, Float.toString(max));
    lMax.setLabelPosition(LimitLine.LimitLabelPosition.LEFT_TOP);

    yAxis.addLimitLine(lMin);
    yAxis.addLimitLine(lMax);

}

Solution

  • Just for future visitors the answer is to NOT call:

    lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
    

    Instead use:

    lineDataSet.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);