Search code examples
iosswiftchartsios-charts

How do I display labels on bottom axis (xVals) correctly on iOS Chart?


I have been following very simple tutorial of iOS Charts.

The values in my chart are now showing correctly, however the label on the bottom is not showing.

    override func viewDidLoad() {

    results =  ["Won", "Drawn", "Lost"]
    let games = [totalWins, totalDraws, totalLosses]
    setChart(dataPoints: results, values: games)

    }

    // CHART FUNCTION ************

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

    barChartView.noDataText = "you need to provide some data for the chart."

    var dataEntries: [BarChartDataEntry] = Array()

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


    let chartDataSet = BarChartDataSet(values: dataEntries, label: "Games Played")

    //let chartData = BarChartData(xVals: self.results, dataSet: dataEntries)
    let chartData = BarChartData()
    self.barChartView.xAxis.labelPosition = XAxis.LabelPosition.bottom
    barChartView.leftAxis.granularityEnabled = true
    barChartView.rightAxis.enabled = false
    barChartView.leftAxis.granularity = 1.0
    chartData.addDataSet(chartDataSet)
    barChartView.data = chartData

}

// END OF CHART FUNCTION ***********

As you can see it is displaying numbers, rather than "Won, Drawn, Lost"

enter image description here

I believe this is because I need to assign the labels in a command like this:

let chartData = BarChartData(xVals: self.results, dataSet: dataSet)
chartView.data = chartData

But I get errors and I don't know what needs to go in dataSet as I took that solution from another thread and can't seem to amend it to work.

Temp image:

enter image description here


Solution

  • You have interchanges x and y values.

    let dataEntry = BarChartDataEntry(x: Double(i), y: values[i])
    

    And to get the X-Axis at the bottom, you have to add the following.

    self.barChartView.xAxis.labelPosition = XAxis.LabelPosition.bottom
    

    Please find my working code for a sample dataset.

    class BarChartViewController: UIViewController {
      @IBOutlet weak var barChartView: BarChartView!
          override func viewDidLoad() {
          super.viewDidLoad()
          let values = [11.00, 90.95, 250.00, 40.90, 60.88, 99.99, 25.00]
          setChart(values: values)
        }
    
        func setChart(values: [Double]) {
            var daysEntries: [BarChartDataEntry] = []
    
            for i in 0..<values.count {
                let dataEntry = BarChartDataEntry(x : Double(i), y : values[i])
                daysEntries.append(dataEntry)
            }
    
            let data = BarChartData()
    
            let ds1 = BarChartDataSet(values: daysEntries, label: "Days")
            ds1.colors = [NSUIColor.red]
    
            data.addDataSet(ds1)
    
            self.barChartView.data = data
            self.barChartView.gridBackgroundColor = NSUIColor.white
            self.barChartView.chartDescription?.text = "Barchart Days and Gross"
            self.barChartView.rightAxis.enabled = true
            self.barChartView.xAxis.labelPosition = XAxis.LabelPosition.bottom
            self.barChartView.xAxis.axisRange = 5.0
        }
    
        override func viewWillAppear(_ animated: Bool) {
            self.barChartView.animate(xAxisDuration: 1.0, yAxisDuration: 1.0)
        }
    }
    

    Thanks Sriram