Search code examples
swiftchartsios-charts

Left axis for HorizontalBarChart of Charts are clipped from the top


When setting up a horizontal bar chart, I'm actually getting a weird issue where the labels of the leftAxis are clipped.

See screenshot: Label clipping

Here's the code I've used to set up my horizontal bar

func prepareHorizontalBarChart() {

    horizontalBarChartView.zoomOut()
    horizontalBarChartView.fitBars = true

    horizontalBarChartView.xAxis.drawGridLinesEnabled = false // disable horizontal grid lines
    horizontalBarChartView.chartDescription?.enabled = false

    horizontalBarChartView.xAxis.labelPosition = .bottom
    horizontalBarChartView.leftAxis.spaceTop = 0.0
    horizontalBarChartView.rightAxis.enabled = false
    horizontalBarChartView.leftAxis.axisMinimum = 0
    horizontalBarChartView.leftAxis.labelPosition = .insideChart
    horizontalBarChartView.leftAxis.granularity = 1.0
    horizontalBarChartView.leftAxis.granularityEnabled = true
    horizontalBarChartView.extraRightOffset = 10.0
    horizontalBarChartView.legend.enabled = false
    horizontalBarChartView.xAxis.avoidFirstLastClippingEnabled = true
    horizontalBarChartView.xAxis.granularity = 1.0
    horizontalBarChartView.xAxis.granularityEnabled = true
    horizontalBarChartView.xAxis.drawLabelsEnabled = true

    horizontalBarChartView.rightAxis.labelFont = UIFont.systemFont(ofSize: 20)
    reloadData()
}

Solution

  • I think in Horizontal Bar chart label is cliping on top edges so for that we need to add extra space in Chart view port with below property.

    horiBarChartView.extraTopOffset = 10
    

    will add 10px extra space from top.

    Before extra space (default behaviour):

    enter image description here

    After 10px extra space:

    enter image description here

    Hope this will help you to solve your problem.