2

I have recently upgraded to the latest version of IOS Charts and X-Code (8.3) and I'm having an issue with IOS Charts, and my x-axis. My x-Axis is supported to be displaying a date, but since the upgrade I cannot get it working, I just get 0 to 6. I believe I know why, it's because I don't seem to be able to set the X-axis values anymore.

Here the original code before the upgrade:

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

    var dataEntries: [ChartDataEntry] = []

    for i in 0..<dataPoints.count {
        let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
        dataEntries.append(dataEntry)
    }

    let lineChartDataSet = LineChartDataSet(yVals: dataEntries, label: "Total Items")
    let lineChartData = LineChartData(xVals: dataPoints, dataSet: lineChartDataSet)

    lineChartDataSet.colors = [UIColor(red: 27.0/255.0, green: 114.0/255.0, blue: 216.0/255.0, alpha: 1.0)]
    lineChartView.xAxis.labelPosition = .bottom
    lineChartView.xAxis.labelFont = UIFont(name: "AvenirNext-Medium", size: 8.0)!
    lineChartView.legend.enabled = false
    lineChartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .easeInOutQuad)
    lineChartView.chartDescription?.text = ""
    lineChartView.rightAxis.enabled = false
    lineChartView.leftAxis.labelFont = UIFont(name: "AvenirNext-Medium", size: 8.0)!
    lineChartDataSet.drawValuesEnabled = false
    lineChartDataSet.drawCirclesEnabled = false
    lineChartDataSet.lineWidth = 2.0
    lineChartView.leftAxis.valueFormatter = NumberFormatter()
    lineChartView.leftAxis.valueFormatter.minimumFractionDigits = 0
    lineChartView.isUserInteractionEnabled = false
    lineChartView.data = lineChartData

}

So this always working fine, since the upgrade things have changed, so my code is now:

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

    var dataEntries: [ChartDataEntry] = []

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

    let lineChartDataSet = LineChartDataSet(values: dataEntries, label: "Total Items")
    let lineChartData = LineChartData(dataSet: lineChartDataSet)

    lineChartDataSet.colors = [UIColor(red: 27.0/255.0, green: 114.0/255.0, blue: 216.0/255.0, alpha: 1.0)]
    lineChartView.xAxis.labelPosition = .bottom
    lineChartView.xAxis.labelFont = UIFont(name: "AvenirNext-Medium", size: 8.0)!
    lineChartView.legend.enabled = false
    lineChartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .easeInOutQuad)
    lineChartView.chartDescription?.text = ""
    lineChartView.rightAxis.enabled = false
    lineChartView.leftAxis.labelFont = UIFont(name: "AvenirNext-Medium", size: 8.0)!
    lineChartDataSet.drawValuesEnabled = false
    lineChartDataSet.drawCirclesEnabled = false
    lineChartDataSet.lineWidth = 2.0
    lineChartView.leftAxis.valueFormatter = NumberFormatter() as? IAxisValueFormatter
    //lineChartView.leftAxis.valueFormatter.minimumFractionDigits = 0
    lineChartView.isUserInteractionEnabled = false
    lineChartView.data = lineChartData

}

So before I set the x-axis values in lineChartData, but now I cannot do that, so my x-axis data I presume is not being set. Can anyone help to get my dates displaying on my x-axis again please?

Thanks

Dave

Dave
  • 351
  • 4
  • 18

1 Answers1

4

You need to implement IAxisValueFormatter protocol with stringForValue method and set it as delegate for xAxis.valueFormatter

I see two ways.

  1. You can just add IAxisValueFormatter protocol to ViewController and realize stringForValue in your ViewController.

    class ViewController: UIViewController, IAxisValueFormatter {
    
        // your code ...
    
        // implement delegate method
        func stringForValue(_ dataPointIndex: Double, axis: AxisBase?) -> String {
    
            return someDataPoints[Int(dataPointIndex)]
        }
    
        func setChart(dataPoints: [String], values: [Double]){
            // your code ...
    
            // Set delegate 
            lineChartView.xAxis.valueFormatter = self
        }
    
        // your code ...
    }
    
  2. Or you can create specilaized delegate class for IAxisValueFormatter protocol.

    private class DataPointFormatter: NSObject, IAxisValueFormatter {
    
        var dataPoints: [String] = []
    
        func stringForValue(_ value: Double, axis: AxisBase?) -> String {
            return dataPoints[Int(value)]
        }
    
        init(dataPoints: [String]) {
            super.init()
            self.dataPoints = dataPoints
        }
    }
    

    And then set delegate in your setChart function

    func setChart(dataPoints: [String], values: [Double]){
        // your code ...
    
        lineChartView.xAxis.valueFormatter = DataPointFormatter(dataPoints: dataPoints)
    }
    
AlexSmet
  • 2,141
  • 1
  • 13
  • 18
  • 2
    If anyone is stuck on error where IAxisValueFormatter could not be resolved, here is a little note: IAxisValueFormatter has been renamed to AxisValueFormatter and IValueFormatter has been renamed to ValueFormatter – Patrik Jurgelj Aug 02 '22 at 11:54