Implementing custom barchart in android

Issue

I’m trying to implement a custom barchart

enter image description here

which should look exactly like this. I tried mpChart but could only get this far

enter image description here

    barDataSet = new BarDataSet(barEntries, "");
    barData = new BarData(barDataSet);
    barData.setBarWidth(.5f);
    barChart.setData(barData);
    
  barDataSet.setColors(getResources().getColor(R.color.colorPrimary));
    barDataSet.setValueTextColor(Color.BLACK);
    barDataSet.setDrawValues(false);
    barChart.getXAxis().setDrawAxisLine(false);
    barChart.setDrawGridBackground(false);
    barChart.getXAxis().setDrawGridLines(false); 
    barChart.getAxisLeft().setDrawGridLines(false); 
    barChart.getAxisRight().setDrawGridLines(false);
    barChart.getAxisLeft().setEnabled(false);
    barChart.getAxisRight().setEnabled(false);
    barChart.getLegend().setEnabled(false);
    barChart.setPinchZoom(false);
    barChart.setDescription(null);
    barChart.setTouchEnabled(false);
    barChart.setDoubleTapToZoomEnabled(false);
    barChart.setExtraOffsets(20, 0, 0, 30);

    XAxis xAxis = barChart.getXAxis();
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setTextSize(14f);
    xAxis.setTextColor(Color.BLACK);
    xAxis.setYOffset(5);
    xAxis.setDrawLabels(true);
    xAxis.setGranularityEnabled(true);
    xAxis.setGranularity(1f);
    xAxis.setXOffset(30);
    xAxis.setValueFormatter(new IndexAxisValueFormatter(xAxisLabel));
    xAxis.setCenterAxisLabels(true);
    
    barChart.invalidate();

I tried almost all possible examples and questions here, couldn’t get the label to centre of the bars. Is it possible to put the labels exactly below the bars in non grouped bars? all the solutions I saw is only for grouped bars or combined charts.

Solution

I was plotting the x axis values from 1. when changed it to 0 and removed

xAxis.setCenterAxisLabels(true);

and i was able to centre the axis labels

Answered By – Narayan C.R

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published