I am currently using the Charts framework to use a line graph within my app. I have used the code below but an error is occurring. Here is the code-
我目前正在使用Charts框架在我的应用程序中使用折线图。我使用了下面的代码但是发生了错误。这是代码 -
`func setChart(dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(x: values[i], y: Double(i))
dataEntries.append(dataEntry)
var colors: [UIColor] = []
for i in 0..<dataPoints.count {
let red = Double(arc4random_uniform(256))
let green = Double(arc4random_uniform(256))
let blue = Double(arc4random_uniform(256))
let color = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1)
colors.append(color)
}
let lineChartDataSet = LineChartDataSet(values: dataEntries, label: "Units Sold")
let lineChartDataa = LineChartData(xVals: dataPoints, dataSet: lineChartDataSet)
Graph.data = lineChartDataa
}
`
The error that is occurring is below-
发生的错误如下 -
Cannot invoke initializer for type 'LineChartData' with an argument list of type '(xVals: [String], dataSet: LineChartDataSet)'
无法使用类型为'(xVals:[String],dataSet:LineChartDataSet)的参数列表调用类型'LineChartData'的初始值设定项
Any ideas how I could fix this.
我有什么想法可以解决这个问题。
2 个解决方案
#1
0
LineChartData has declared this as init.
LineChartData已将此声明为init。
public override init(dataSets: [IChartDataSet]?)
Its superclass, ChartData has declared these inits:
它的超类ChartData声明了这些内容:
@objc public init(dataSets: [IChartDataSet]?)
@objc public convenience init(dataSet: IChartDataSet?)
It looks like the init with params that you are trying, is not available, hence the compiler error.
看起来你正在尝试使用params的init是不可用的,因此编译错误。
#2
0
You have to use like this :
你必须这样使用:
This is to set LineChartData
:
这是设置LineChartData:
let lineChartDataSet = LineChartDataSet(values: dataEntries, label: "Units Sold")
let lineChartDataa = LineChartData(dataSet: lineChartDataSet)
For setting xAxis labels :
要设置xAxis标签:
Graph.xAxis.labelPosition = XAxis.LabelPosition.bottom
Graph.xAxis.valueFormatter = IndexAxisValueFormatter(values: dataPoints)
#1
0
LineChartData has declared this as init.
LineChartData已将此声明为init。
public override init(dataSets: [IChartDataSet]?)
Its superclass, ChartData has declared these inits:
它的超类ChartData声明了这些内容:
@objc public init(dataSets: [IChartDataSet]?)
@objc public convenience init(dataSet: IChartDataSet?)
It looks like the init with params that you are trying, is not available, hence the compiler error.
看起来你正在尝试使用params的init是不可用的,因此编译错误。
#2
0
You have to use like this :
你必须这样使用:
This is to set LineChartData
:
这是设置LineChartData:
let lineChartDataSet = LineChartDataSet(values: dataEntries, label: "Units Sold")
let lineChartDataa = LineChartData(dataSet: lineChartDataSet)
For setting xAxis labels :
要设置xAxis标签:
Graph.xAxis.labelPosition = XAxis.LabelPosition.bottom
Graph.xAxis.valueFormatter = IndexAxisValueFormatter(values: dataPoints)