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

时间:2017-04-09 10:40:59

标签: swift xcode bar-chart ios-charts

我想用Swift 3设置条形图,然后按照this教程。

我在barChartView中创建了viewController,在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)'

2 个答案:

答案 0 :(得分:5)

iOS-Chart有一些变化:

  1. BarChartDataEntry(value:xIndex:)已更改为BarChartDataEntry(x: y:)
  2. BarChartData(xVals: dataSet:)已更改为BarChartData(dataSet:)
  3. 所以你需要这样的东西

    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设置标签,则需要使用方法IAxisValueFormatter实施stringForValue( _: axis:)协议 您可以在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 ...
    }
    

答案 1 :(得分:2)

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

nvidia-docker start <container-name>