Home » Android » android – How to modify the bar width in MPAndroidChart BarChart?

android – How to modify the bar width in MPAndroidChart BarChart?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am using MPAndroidChart library.

I would like modify the size of bar present in bar chart. Could you let us know where to check for modify size of bar in bar chart in mp android chart

How to&Answers:

You can reduce the spacing between the bars that will automatically increase the width of bars.

BarDataSet set = new BarDataSet(vals, "Set A");
set.setBarSpacePercent(50f);

UPDATE v3.0.0:

As of this library version, you can control the bar width with the following method:

BarData barData = new BarData(...);
barData.setBarWidth(1f);

The number you provide for this method represents a certain interval on the x-axis. If your total axis has a range of e.g. 10k, and you set the bar width to 1000, the bar will cover 10% of the total x-axis range.

Answer:

As for the latest version of the library BarDataSet does not have this function any longer, its in BarData class.

BarData data = new BarData(dataset);
data.setBarWidth(0.5f);

Answer:

Kotlin:

barChartView.data = BarData(dataSetList).apply { 
    barWidth = 0.5f
}    

To fit data in graph (No scroll behavior)

barChartView.xAxis.axisMaximum = dataList.size
barChartView.setVisibleXRange(1f,xAxisLabels.size.toFloat())

Answer:

This applies to MPAndroidChart API. To reduce the Bar width you have to increase space between bars (as mentioned Rakesh). The value 50F should be good for mobile display but you can increase/decrease it to suit your device screen.

Sample code as requested by one user :

    BarDataSet barDataSet = new BarDataSet(group1, "X");
    barDataSet.setColors(ColorTemplate.COLORFUL_COLORS);
    barDataSet.setBarSpacePercent(50f);

The API imports are-

import com.github.mikephil.charting.data.BarDataSet;