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

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

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.

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);

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())

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;

data.barWidth = 0.5f
it means barWidth/(barWidth + blankWith) = 0.5f

Related

How to make extra padding in x-Axis values in MP Chart Library?

I am using MPChart Library for Multi data set. I am using three grouped graph for representation. For Spacing between grouped graphs I have used the following code snippet
groupSpace=0.8f
chart.getXAxis().setAxisMaximum(startYear + chart.getBarData().getGroupWidth(groupSpace,barSpace) * groupCount);
chart.groupBars(startYear, groupSpace, barSpace);
By Using groupspace I am able to set margin between group bars. But I want to also set the same margin with x-Axis values,those are not set in proper margin.
Screenshot
Try using
XAxis xl = chart.getXAxis();
xl.setCenterAxisLabels(true);
xl.setLabelCount(groupCount + 1, true);
xl.setValueFormatter((value, axis) -> String.valueOf((int) value));
...
chart.getXAxis().setAxisMinimum(startYear);
chart.getXAxis().setAxisMaximum(startYear + chart.getBarData().getGroupWidth(groupSpace,barSpace) * groupCount);

MPAndroidchart two Axes overlapping

I have a chart with two datasets displayed on both yAxes. However these datasets overlap each other. Is there a way to display them like a grouped chart side by side over one label
If i use chart.groupBars(0f, 0.8f, 0.1f) the chart gets cut off because the bar width does not scale correctly.
OK After a bit more digging around and reading the docs more carefully i found the answer.
The sum of your barWidth, barSpace and groupSpace have to be zero, eg in my case with two bars per group:
val barWidth = 0.3
val groupSpace = 0.3
val barSpace = 0.05
val offsetForFirstX = -0.5 // for three bars 0.33 etc
chartView.groupBars(-0.5, groupSpace, barSpace)
For any problem on this charts lib
Download & run
https://github.com/PhilJay/MPAndroidChart/tree/master/MPChartExample
Make changes to the sample chart you wish to implement with view as required.
Sounds long cut, but we get to understand the working of library with help of documentation and don't get stuck in issues

MPAndroidChart hide bubble size value

I'm drawing a bubble chart using MPAndroidChart, and want to hide the bubble size value. How do I do that?
How do I hide the "5' and "66.8"?
It can be done by using bubbleDataSet.setDrawValues(false);
Code:
BubbleDataSet set1 = new BubbleDataSet(yVals1, "DS 1");
set1.setColor(ColorTemplate.COLORFUL_COLORS[0], 130);
set1.setDrawValues(false);

How to set the width of the bar in mpchart exactly(Zooming disabled)

Based on the dataset count the width of the bar is varying in mpchart.
Is there any possibilities to set the width of the bar as 50dp.
As per the requirement zoom has to be disable.
Even I can able to achieve partially by using chart.zoom(2.5f, 0f, 0f, 0f); but zoom size needs to vary based on the dataset count.
Is there any other chart engine to set exact width of the bar.
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.
warning: look for mpandroidchart library version that includes BarData class.
(for example: import com.github.mikephil.charting.data.BarDataSet;)

How to draw border around a bar in MPAndroidChart BarCart?

I am using MPAndroidChart for drawing a bar chart.
How can I draw border around each bar in the graph that is displayed?
This is not possible by default.
You will have to modify the library, specifically the BarChartRenderer class.
Simply take the RectF object that represents the bar that is to be drawn and draw it once again over the original bar whilst changing the Paint mode to STROKE instead of FILL.
Of course you will also have to change the Paint color to distinguish the border from the bar.
So, after a rigorous searching and checking the Demo App the Library Developer used to demonstrate the Library functionalities, I got this.
BarChart barChart = new BarChart();
BarData barData = new BarData(barDataSet);
//set the appropriate properties of object **barData**
//then set the data property of the chart
barChart.setData(barData);
.
.
.
.
//add border to chart bars
for (IBarDataSet set : barChart.getData().getDataSets())
((BarDataSet) set).setBarBorderWidth(set.getBarBorderWidth() == 1.f ? 0.f : 1.f);
.
.
.
//set up other properties as well
Intent
barData.sliceSpace = 2f
and for mas styling
eneficioChar.holeRadius = 1f
val set1 = BarDataSet(entries,seriesLabel)
set1.color = Color.YELLOW
set1.barBorderWidth = 1f
set1.barBorderColor=Color.RED

Categories

Resources