我试图更新barChart中的数据,当我传递另一个包含更多数据点的数据集时,一切顺利,但当我传递另一个数据点较低的数据集时,应用程序崩溃。
我也尝试将barchartView.notifyDataSetChanged()放在line chart.DataSet.color之后但仍然崩溃,可能是错误的地方,但我尝试将notifyDataSetChanged放在很多地方。
我告诉你我的代码,我在更改dataSet值时所做的只是调用setChart方法并将新值数组作为参数传递。
func setChart(values: [Double]) {
let formato:BarChartFormatter = BarChartFormatter()
formato.setArray(self.listadoLabels)
let xaxis:XAxis = XAxis()
barChartView.noDataText = "You need to provide data for the chart."
max = values.maxElement()
var dataEntries: [BarChartDataEntry] = []
for i in 0..<values.count {
let dataEntry = BarChartDataEntry(x: Double(i), y:values[i])
dataEntries.append(dataEntry)
formato.stringForValue(Double(i), axis: xaxis)
}
xaxis.valueFormatter = formato
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units")
chartDataSet.colors = ChartColorTemplates.joyful()
barChartView.notifyDataSetChanged()
barChartView.fitBars = true
barChartView.xAxis.valueFormatter = xaxis.valueFormatter
barChartView.xAxis.labelPosition = .Bottom
if(values.count > 5){barChartView.zoom(scaleX: 17.0, scaleY: 1.0, x: 0, y: 0)}
barChartView.xAxis.granularityEnabled = true
barChartView.xAxis.granularity = 1.0
barChartView.leftAxis.axisMinimum = 0
barChartView.leftAxis.axisMaximum = max != nil ? max + (2 * max/10) : 5
barChartView.rightAxis.enabled = false
barChartView.drawValueAboveBarEnabled = true
barChartView.descriptionText = ""
barChartView.xAxis.drawGridLinesEnabled = false
barChartView.animate(xAxisDuration: 1.0, yAxisDuration: 1.0)
let chartData = BarChartData()
chartData.addDataSet(chartDataSet)
barChartView.data?.notifyDataChanged()
barChartView.data = chartData
}
控制台显示&#34;致命错误:索引超出范围&#34;。
希望有人能提供帮助,谢谢。