MPAndroid bar chat is showing zero values in middle if all the values are zeros

Viewed 292

Im using MPAndroidChart for bar chart with some values.. If I get all the values are zero then its like below. Please anybody help how to show zero in bottom or hide if graph if all the values are zero

implementation 'com.github.Philjay:MPAndroidChart:v3.1.0'

private fun drawWeightBarChart(weightData: ArrayList<BarEntry>?) {
    // barChart = findViewById<View>(R.id.barChart) as BarChart
    val barDataSet = BarDataSet(weightData, "")
    //barDataSet.barBorderWidth = 0.9f
    //barDataSet.valueFormatter = MyValueFormatter()
    barDataSet.setColors(Color.parseColor("#20D2FF"))
    val barData = BarData(barDataSet)

    barData.setValueFormatter(MyValueFormatter())

    val xAxis: XAxis = weight_chart.getXAxis()
    xAxis.position = XAxis.XAxisPosition.BOTTOM

    val yAxisRight: YAxis = weight_chart.getAxisRight()
    val yAxisLeft: YAxis = weight_chart.getAxisLeft()

    yAxisRight.isEnabled = false
    yAxisLeft.isEnabled = false

    //yAxisLeft.setAxisMaxValue(20f);

    weight_chart.getDescription().setEnabled(false);

    val leg: Legend = weight_chart.getLegend()
    leg.isEnabled = false

    weight_chart.getXAxis().setDrawGridLines(false);
    weight_chart.getAxisLeft().setDrawGridLines(false);
    weight_chart.getAxisRight().setDrawGridLines(false);

    val months =
        arrayOf("1", "2", "3", "4", "5", "6", "7", "8", "9", "10")
    val formatter = IndexAxisValueFormatter(months)
    xAxis.granularity = 1f
    xAxis.valueFormatter = formatter
    weight_chart.setData(barData)
    weight_chart.setFitBars(true)
    weight_chart.animateXY(500, 500)
    weight_chart.invalidate()

}

enter image description here

1 Answers

You should set the minimum value of your axis to 0 using setAxisMinValue, like this:

weight_chart.getAxisLeft().setAxisMinValue(0);
weight_chart.getAxisRight().setAxisMinValue(0);

Kotlin:

weight_chart.axisLeft.axisMinimum = (0F)
weight_chart.axisRight.axisMinimum = (0F)
Related