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

8.1k Views Asked by At

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

4

There are 4 best solutions below

4
On BEST ANSWER

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

0
On

I hope this would help you with controlling the xAxis label positions.

  • First method

    chartView.setExtraOffsets(left: 20, top: 0, right: 0, bottom: 0)
    
  • Second method

    chartView.xAxis.avoidFirstLastClippingEnabled = true
    
0
On
    // xAxis.labelFont = UIFont.systemFont(ofSize: 13.0)
      // xAxis.labelTextColor = .lightGray
       xAxis.labelPosition = .bottom
      xAxis.setLabelCount(dataPoints.count, force: false)
    if xAxis.labelPosition == .bottom {
     xAxis.valueFormatter = IndexAxisValueFormatter(values: dataPoints)
    }
    xAxis.granularity = 1.0
0
On

I had the same issue and I fixed it with these three lines :

self.xAxis.granularity = 1
self.xAxis.granularityEnabled = true
self.xAxis.labelCount = // number of points on X axis