与 ioscharts 不匹配可用的重载错误

Not matching available overloads error with ioscharts

我想用 Swift 3 设置条形图并遵循 this 教程。

我在 viewController 中创建了一个 barChartView,在 viewDidLoad 中创建了数据值:

var months: [String]!
var counts: [Double]!
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
counts = [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 函数:

setChart(dataPoints: months, values: counts)

函数本身如下所示:

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

    var dataEntries: [BarChartDataEntry] = []

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

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

我在尝试构建时收到两个错误警告,此处:

let dataEntry = BarChartDataEntry(value: values[i], xIndex: i)
Argument labels '(value:, xindex:)' do not match any available overloads

这里:

let chartData = BarChartData(xVals: months, dataSet: chartDataSet)
Cannot invoke initializer for type 'BarChartData' with an argument list of type '(xvals: [String]!, dataSet: BarChartDataSet)'

Charts 3.0 有一些重大变化。试试这个

class BarChartViewController: UIViewController, ChartViewDelegate {

    @IBOutlet weak var barChartView: BarChartView!

    let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        barChartView.delegate = self
        barChartView.xAxis.valueFormatter = self

        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(values: unitsSold)
    }

    func setChart(values: [Double]) {
        barChartView.noDataText = "You need to provide data for the chart."

        let chartData = BarChartDataSet()
        for (i, val) in values.enumerated(){
            _ = chartData.addEntry(BarChartDataEntry(x: Double(i), y: val))
        }
        barChartView.data = BarChartData(dataSet: chartData)
    }
}

extension BarChartViewController: IAxisValueFormatter{
    func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        return months[ Int(value) % months.count]
    }
}

iOS-图表有一些变化:

  1. BarChartDataEntry(value:xIndex:) 已更改为 BarChartDataEntry(x: y:)
  2. BarChartData(xVals: dataSet:) 已更改为 BarChartData(dataSet:)

所以你需要这样的东西

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")
    let chartData = BarChartData(dataSet: chartDataSet)
    countPerCountryView.data = chartData
} 

此外,如果您想为 xAxis 设置标签,您需要使用方法 stringForValue( _: axis:) 实现 IAxisValueFormatter 协议 您可以在 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 ...
}