我正在使用图表库创建几个折线图,当没有可用数据时,我希望图表清除。图表清除正常并显示其通常的“nodatatext”,但当我尝试再次设置图表时,代码在设置数据的行上失败:fasterChartView.data = chartData3
func setChart3(dataEntryX forX:[String],dataEntryY forY: [Double]) {
fastestChartView.noDataText = "You need to provide data for the chart."
var dataEntries:[ChartDataEntry] = []
for i in 0..<forX.count{
let dataEntry = ChartDataEntry(x: Double(i), y: Double(forY[i]) , data: chugIndexArr3 as AnyObject?)
dataEntries.append(dataEntry)
}
let chartDataSet = LineChartDataSet(values: dataEntries, label: "Fastest Chugs")
chartDataSet.setColor(UIColor.green, alpha: 0.6)
chartDataSet.circleColors = self.colors3
chartDataSet.circleHoleColor = UIColor(red: 255/255, green: 223/255, blue: 24/255, alpha: 1.0)
chartData3 = LineChartData(dataSet: chartDataSet)
chartData3.setDrawValues(true)
fastestChartView.data = chartData3
fastestChartView.legend.enabled = false
fastestChartView.xAxis.labelPosition = .bottom
fastestChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: chugIndexArr3)
fastestChartView.animate(xAxisDuration: 1.0, yAxisDuration: 1.0)
fastestChartView.drawBordersEnabled = true
fastestChartView.data?.setValueFormatter(valueFormatter)
if chugIndexArr3.count > 25 {
fastestChartView.setVisibleXRange(minXRange: 10, maxXRange: 25)
fastestChartView.moveViewToAnimated(xValue: Double(self.chugIndexArr3.count - 25), yValue: 0, axis: .left, duration: 2)
} else if chugIndexArr3.count == 0 {
fastestChart.clear()
}
else {
fastestChartView.resetZoom()
}
}
我知道我的程序有效,因为我使用相同的函数来初始显示图形,就像我在尝试重新加载数据时一样。但出于某种原因,当我尝试使用fasterChartView.clear()然后尝试使用与viewdidload()中相同的方法设置数据时,它会崩溃。以下是viewdidload()和导致崩溃的按钮操作函数中的代码:
self.ref.child("users").child(uid!).child("chugStats").child("4avgs").observeSingleEvent(of: .value, with: { (snapshot) in
if let dict = snapshot.value as? NSDictionary {
for index in dict.allKeys {
let currentKey = String(describing: index)
if currentKey != "avg" && currentKey != "fastest" {
let currentValue = dict[currentKey] as? String ?? ""
let messageArr = currentValue.components(separatedBy: ":")
self.chugIndexArr2.append(currentKey)
self.chugTimeArr2.append(Double(messageArr[0])!)
self.intervals2.append(Double(messageArr[1])!)
if let myDouble = NumberFormatter().number(from: messageArr[1])?.doubleValue {
let date = NSDate(timeIntervalSince1970: myDouble)
let dateFormatter = DateFormatter()
dateFormatter.timeStyle = .short
let dateStr = dateFormatter.string(from: date as Date)
self.chugInterval2.append(dateStr)
}
//Color array setup
if messageArr.count == 2 {
self.colors2.append(UIColor(red: 100/255, green: 200/255, blue: 110/255, alpha: 0.9))
} else if messageArr.count == 3 {
if messageArr[2] == "fun" {
self.colors2.append(UIColor(red: 100/255, green: 200/255, blue: 110/255, alpha: 0.9))
} else if messageArr[2] == "ranked" {
self.colors2.append(UIColor.red)
} else if messageArr[2] == "challenge" {
self.colors2.append(UIColor.blue)
}
}
}
}
let sorted1 = zip(self.chugTimeArr2, self.intervals2).sorted { $0.1 < $1.1 }
self.chugTimeArr2 = sorted1.map { $0.0 }
let sorted2 = zip(self.chugInterval2, self.intervals2).sorted { $0.1 < $1.1 }
self.chugInterval2 = sorted2.map { $0.0 }
let sorted3 = zip(self.chugIndexArr2, self.intervals2).sorted { $0.1 < $1.1 }
self.chugIndexArr2 = sorted3.map { $0.0 }
let sorted4 = zip(self.colors2, self.intervals2).sorted { $0.1 < $1.1 }
self.colors2 = sorted4.map { $0.0 }
}
self.setChart2(dataEntryX: self.chugIndexArr2, dataEntryY: self.chugTimeArr2)
})
请帮我解决这个问题。我无法找到解决方法,也无法找到在互联网上遇到同样问题的人。
答案 0 :(得分:2)
我遇到了同样的问题。清除图表似乎有些问题,可能并非所有变量都返回默认值。
我假设您正在使用Charts v3.0.3(目前我自己使用3.0.2)。我使用的修复程序应该足够好。
转到AxisRendererBase.swift
,如果使用Pod,则解锁文件,然后将第106行(if labelCount == 0 || range <= 0 || range.isInfinite
)更改为:if labelCount == 0 || range <= 0 || range.isInfinite || range.isNaN
。
编辑: 创建了PR。