gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 09:33:56 24 4
gpt4 key购买 nike

我想用 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)'

最佳答案

iOS-Chart 有一些变化:

  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 协议(protocol)您可以在 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 ...
}

关于swift - 与 ioscharts 不匹配可用的重载错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43305767/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com