14

I have a problem with the Charts library. The problem I am facing is that the bar graph is not "fit" within the borders of my view. The following image shows that, the both first and last bar are cut into half and label of the highest bar is also partially cut.

Bars and labels are cut along the graph borders

When I search online, I got the following thread https://github.com/danielgindi/Charts/issues/353 which seems to describe the exact same issue.

I use a simple dataset with some random numbers at 6 indices. The data is added to the chart as follows:

let values: [Int] = [5, 8, 1, 2, 1, 2]
var dataEntries: [BarChartDataEntry]

for i in 0..<values.count {
    let dataEntry = BarChartDataEntry(x: Double(i), y: Double(values[i]))
    dataEntries.append(dataEntry)
}

let chartDataSet = BarChartDataSet(values: dataEntries, label: "Count")
let chartData = BarChartData(dataSet: chartDataSet)
barChartView.data = chartData

Hopefully someone can help me out. Thanks in advance!

Wide Angle Technology
  • 1,184
  • 1
  • 8
  • 28
Woodahack
  • 143
  • 1
  • 5

4 Answers4

27

There is a bug in the library with regards to calculating the xAxis minimum and maximum.

You can manually set the xAxis minimum and maximum to prevent the first and last bars to be shown in half.

Add the below line of code, it should work perfectly fine.

yourBarChartView.xAxis.axisMinimum = -0.5;
yourBarChartView.xAxis.axisMaximum = Double(yourAxisDataList.count) - 0.5;
Vikram Ezhil
  • 995
  • 1
  • 9
  • 15
  • Thanks, work likes a charm! I will mark this answer as right, as I expect more people to have this problem. Although ashmi123 provided an appropriate answer for the label problem. – Woodahack Aug 29 '16 at 21:58
  • Between are you sure you are using the latest library version? Since this bug was fixed in v2.2.5 and there is no need to add this code. I have also checked this in one of my app with the new version and it's working as expected. – Vikram Ezhil Sep 01 '16 at 15:47
  • 1
    For version in branch Chart3.0-Swift2.3 this hack is still valid. Thanks! – Voldy Oct 12 '16 at 22:39
  • @VikramEzhil Im using the latest version of Charts and was having this problem. – Aditya Garg Dec 20 '16 at 08:46
  • @Aditya The most recent version of Charts has this bug, guess the bug tracking and fixing by the creators is very inconsistent for this library. – Vikram Ezhil Dec 21 '16 at 08:49
9

you can add the offset to Top and bottom view as below :

 barChartView.extraTopOffset = 40.0f;
 barChartView.extraBottomOffset = 10.0f;
ashmi123
  • 710
  • 1
  • 6
  • 21
  • Thank you very much, this seems to have fixed the labelling issue. The problem of the half bars unfortunately remains, it is not fixed by setting extraLeftOffset/extraRightOffset. When I run the demo app it seems to be displaying the bar graphs correctly. – Woodahack Aug 25 '16 at 16:13
  • can you show image for that exactly where it cut off? – ashmi123 Aug 26 '16 at 03:42
  • The link in my previous post still holds for the cutoff. It seems to cut the bars exactly in half. Changing the size of the viewcontroller does not seem to help. http://i.stack.imgur.com/iW17f.png – Woodahack Aug 26 '16 at 14:03
1

You can try, editting the value of xAxis of your bargraph with values between -0.99... to -0.5. Check this example:

enter image description here

With: yourBarChart.xAxis.axisMinimum = -0.5

enter image description here

With: yourBarChart.xAxis.axisMinimum = -0.99

0

If it is vertical Bar chart

barChartView.xAxis.spaceMin   = 5.0f;
barChartView.xAxis.spaceMax   = 5.0f;

if it is Horizantal bar chart

barChartView.leftAxis.spaceBottom = 0.0 

or barChartView.rightAxis.

ram880
  • 1,288
  • 12
  • 22