Search code examples
iosxcodeios-charts

Xcode 6.4 iOS Charts API Error


I'm have a problem with a unresolved identifier 'dataPoints' it is in the for loop and I've tried all I can but as I am fairly new to swift I can't seem to find the solution.

    var months: [String]!
func setChart(dataPoints: [String], values: [Double]) {
    barChartView.noDataText = "You need to provide data for the chart."

}

override func viewDidLoad() {
    super.viewDidLoad()

    months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
    let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]

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

    let chartDataSet = BarChartDataSet(yVals: dataEntries, label: "Units Sold")
    let chartData = BarChartData(xVals: months, dataSet: chartDataSet)
    barChartView.data = chartData

Solution

  • You are using dataPoints in your for loop, but there is no such variable in the scope of viewDidLoad().

    In your comment, you mention declaring a dataPoints in the function setChart(), but that is local to that function, it does not apply to other functions.

    From your code, it looks like you should be using months for the for loop.

    On that note, you probably intend to be using unitsSold instead of values when creating your BarChartDataEntry object in that for loop.