Label above bar not showing for horizontal bars ios-charts

1.1k views Asked by At

I am implementing ios-charts in my project for a horizontal bar chart. I need the values of the individual bars to appear above the bars (since the chart is horizontal, the label should appear on the right of the bar). However, I am unable to make it work. The code is as follows:

func setupHorizontalChartView() {

        self.chartView.chartDescription?.enabled = false
        chartView.dragEnabled = false
        chartView.setScaleEnabled(true)
        chartView.pinchZoomEnabled = false

        let chartXAxis = chartView.xAxis
        chartXAxis.labelPosition = .bottom

        let chartLeftAxis = chartView.leftAxis
        chartLeftAxis.labelFont = NSUIFont(name: "Helvetica", size: 12)!
        chartLeftAxis.labelTextColor = UIColor.darkGray

        chartView.rightAxis.enabled = false

        chartView.drawValueAboveBarEnabled = true

        let formatter = BarChartFormatter()
        formatter.setValues(values: self.months)

        chartView.xAxis.valueFormatter = formatter
        chartView.xAxis.setLabelCount(self.months.count - 1, force: false)

        chartView.xAxis.drawGridLinesEnabled = false
        chartView.leftAxis.drawGridLinesEnabled = false

        chartView.xAxis.drawAxisLineEnabled = false
        chartView.leftAxis.drawAxisLineEnabled = false

        chartView.leftAxis.drawLabelsEnabled = false
    }

    func setChart(dataPoints: [String], values: [Double]) {

        var dataEntries: [BarChartDataEntry] = []

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

        let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")

        chartDataSet.drawValuesEnabled = true
        let chartData = BarChartData(dataSet: chartDataSet)
        chartData.setDrawValues(true)

        chartView.data = chartData
    }

drawValueAboveBarEnabled has been set to true for the chartView. When I use the same code for BarChartView instead of HorizontalBarChartView, the values of the bars get shown above the bars. However, it doesn't work for horizontal bar chart view.

1

There are 1 answers

0
ashumilov On

Setting chartView.leftAxis.axisMinimum = 0 helped me. I've open a discussion for it here GitHub