BiRjU
BiRjU

Reputation: 753

MPAndroidChart Line Chart has space in bottom, Not start with 0

in my Application, I am using MPAndroidChart Library for display Data in Line Chart

when its data set in chart that time some extra space display in Bottom,

in my data minimum value is "0" but in chart that display between 0-10 not start with 0

you can show in image when I click chart that displays 0 value in that place ...but 0 is in the bottom so why it can't start with that place?

enter image description here

here is my code

private void DisplayLineChart(TotalSalesLineData data, LineChart chart, boolean b) {

    // background color
    chart.setBackgroundColor(Color.WHITE);


    // disable description text
    chart.getDescription().setEnabled(false);

    // enable touch gestures
    chart.setTouchEnabled(true);

    // set listeners
    chart.setOnChartValueSelectedListener(this);
    chart.setDrawGridBackground(false);

    // create marker to display box when values are selected
    MyMarkerView mv = new MyMarkerView(getActivity(), R.layout.custom_marker_view);

    // Set the marker to the chart
    mv.setChartView(chart);
    chart.setMarker(mv);


    YAxis yAxisRight = chart.getAxisRight();
    yAxisRight.setEnabled(false);

    YAxis leftAxis = chart.getAxisLeft();
    leftAxis.setTextColor(Color.BLACK);
    leftAxis.setAxisMaximum(50f);
    leftAxis.setAxisMinimum(0f);

    leftAxis.setDrawGridLines(true);
    leftAxis.setGranularityEnabled(true);

    //chart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
    XAxis xAxis = chart.getXAxis();
    xAxis.setDrawGridLinesBehindData(false);
    xAxis.setDrawGridLines(false);
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setAxisMinimum(0f);
    xAxis.setSpaceMin(0f);
    xAxis.setSpaceMax(0f);


    setData(chart, data);

    // draw points over time
    chart.animateX(0);
}

here is set data method

 private void setData(LineChart chart, TotalSalesLineData range) {

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

    for (int i = 0; i < range.getCurrentYear().size(); i++) {
        //  float val = (float) (Math.random() * (range / 2f)) + 50;
        values1.add(new Entry(i, range.getCurrentYear().get(i)));
    }

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

    for (int i = 0; i < range.getPreviousYear().size(); i++) {
        //  float val = (float) (Math.random() * range) + 450;
        values2.add(new Entry(i, range.getPreviousYear().get(i)));
    }


    LineDataSet set1, set2;

    if (chart.getData() != null && chart.getData().getDataSetCount() > 0) {
        set1 = (LineDataSet) chart.getData().getDataSetByIndex(0);
        set2 = (LineDataSet) chart.getData().getDataSetByIndex(1);

        set1.setValues(values1);
        set2.setValues(values2);

        chart.getData().notifyDataChanged();
        chart.notifyDataSetChanged();
    } else {
        // create a dataset and give it a type

        set1 = new LineDataSet(values1, String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));

        set1.setAxisDependency(YAxis.AxisDependency.RIGHT);
        set1.setColor(ColorTemplate.getHoloBlue());
        set1.setCircleColor(R.color.colorAccent);
        set1.setLineWidth(2f);
        set1.setCircleRadius(3f);
        // set1.setFillAlpha(65);
        set1.setFillColor(ColorTemplate.getHoloBlue());
        //  set1.setHighLightColor(Color.rgb(244, 117, 117));
        set1.setHighLightColor(R.color.colorAccent);
        set1.setDrawCircleHole(false);
        //set1.setFillFormatter(new MyFillFormatter(0f));
        //set1.setDrawHorizontalHighlightIndicator(false);
        //set1.setVisible(false);
        //set1.setCircleHoleColor(Color.WHITE);

        // create a dataset and give it a type
        set2 = new LineDataSet(values2, String.valueOf(CommonMethods.getPreviousYear()));
        set2.setAxisDependency(YAxis.AxisDependency.RIGHT);
        set2.setColor(Color.GRAY);
        set2.setCircleColor(Color.BLACK);
        set2.setLineWidth(2f);
        set2.setCircleRadius(3f);
        //  set2.setFillAlpha(65);
        set2.setFillColor(Color.RED);
        set2.setDrawCircleHole(false);
        set2.setHighLightColor(Color.rgb(244, 117, 117));
        //set2.setFillFormatter(new MyFillFormatter(900f));

        // create a data object with the data sets
        LineData data = new LineData(set1, set2);
        data.setValueTextColor(Color.WHITE);
        data.setValueTextSize(0f);
        // set data
        chart.setData(data);
    }
}

here is my responce

{
"error": false,
"data": {
    "currentYear": [
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0
    ],
    "previousYear": [
        0,
        0,
        0,
        0,
        48970,
        0,
        21854.81,
        242827.07,
        29070.48,
        1196,
        3588,
        441.76
    ]
},
"message": "Successfully"
}

Upvotes: 2

Views: 2932

Answers (1)

Wilder Pereira
Wilder Pereira

Reputation: 2267

I solved by setting the minimum for both left and right axis.

Kotlin code:

barChart.axisLeft.axisMinimum = 0f
barChart.axisRight.axisMinimum = 0f

Upvotes: 9

Related Questions