Search code examples
iosswiftxcodeuiviewios-charts

iOS Charts, Remove extra space created by xAxis Labels at bottom


I have a vertical normal looking bar chart created with iOS Charts. My issue is that for some reason whenever I run the program, the chart does not fill up the view it is in from top to bottom. I am specifically trying to fix the fact that there is a small space between the bottom of a bar and the bottom of the UIView. How can I remove this space so the bottom of the bar aligns perfectly with the bottom of the UIView? (Need No gap)

Here is my code:

    barChartView.xAxis.labelPosition = .top 
    barChartView.xAxis.drawGridLinesEnabled = false
    barChartView.xAxis.drawAxisLineEnabled = false
    barChartView.leftAxis.drawGridLinesEnabled = false
    barChartView.leftAxis.drawAxisLineEnabled = false
    barChartView.rightAxis.drawAxisLineEnabled = false
    barChartView.rightAxis.drawGridLinesEnabled = false
    barChartView.rightAxis.enabled = false
    barChartView.leftAxis.enabled = false
    barChartView.xAxis.labelTextColor = UIColor.white
    barChartView.data?.setDrawValues(false)
    barChartView.xAxis.labelFont = UIFont(name: "HelveticaNeue-Bold", size: 12.0)!
    barChartView.legend.enabled = false
    barChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values:xValues)
    barChartView.xAxis.granularity = 1
    barChartView.animate(xAxisDuration: 1.0, yAxisDuration: 1.0, easingOption: .easeInQuint)
    barChartView.isUserInteractionEnabled = false
    barChartView.barData?.barWidth = 0.65

Here is a pic of the issue...I need to get rid of that gap between the bottom of the bar and the red space. The UIView that the graph is in (Not the same as the red view) reaches down to the border of the red view.

enter image description here


Solution

  • Was able to solve my problem by adjusting the bottom constraint programmatically based on the size of the phone's display.

    I added this code into viewWillAppear and I will continue to do this for the rest of the iPhone sizes and not just the Xs:

    if UIScreen.main.bounds.height == 896{
            print("Xs Max")
            bottomConstraintGraph.constant = -31.5 // bottomConstraintGraph is the constraint connected from the bottom of the graph's view to the superview and changed here through an outlet connection.
    }