gpt4 book ai didi

Swift - iOS 图表 - 在一个 ViewController 中使用两个数据集的两个 LineChart View

转载 作者:行者123 更新时间:2023-11-30 12:37:49 25 4
gpt4 key购买 nike

我有一个应用程序,我可以按天从 Firebase 获取一些数据,并且尝试使用 iOS 图表在单独的图表中显示每天的数据,但它崩溃并出现错误“ fatal error :索引超出”范围'。下面是我的代码:

 func setChart(dataPoints: [String], values: [Double]) {

var dataEntries: [ChartDataEntry] = []

for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
}





let lineChartDataSet = LineChartDataSet(yVals: dataEntries, label: "Units Sold")
let lineChartData = LineChartData(xVals: dataPoints, dataSet: lineChartDataSet)
firstDayChart.data = lineChartData

}

func setChart2(dataPoints: [String], values: [Double]) {

var dataEntries: [ChartDataEntry] = []

for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
}







let lineChartDataSet = LineChartDataSet(yVals: dataEntries, label: "Units Sold")
let lineChartData = LineChartData(xVals: dataPoints, dataSet: lineChartDataSet)
secondDayChart.data = lineChartData

}

//按钮

  @IBAction func getResults(sender: UIButton) {
let doubleArray = BGdataOutsideDay1.map { Double($0)!}
// print(doubleArray)
// var newArray:[Double] = []


self.doublesArrayDay1 = doubleArray

if doublesArrayDay1.count != 0 {
let bgAvg = doubleArray.reduce(0, combine: +) / Double(doubleArray.count)
let highest = doubleArray.maxElement()
let lowest = doubleArray.minElement()
self.bgHighestDay1 = String(format: "%.1f", highest!)
self.bgAvgDay1 = String(format: "%.1f", bgAvg)
self.bgLowestDay1 = String(format: "%.1f", lowest!)
self.firstDayHighest.text = self.bgHighestDay1! + " " + "\(self.units!)"
self.firstDayAvg.text = self.bgAvgDay1! + " " + "\(self.units!)"
self.firstDayLowest.text = self.bgLowestDay1! + " " + "\(self.units!)"
} else {
self.firstDayHighest.text = "0" + " " + "\(self.units!)"
self.firstDayAvg.text = "0" + " " + "\(self.units!)"
self.firstDayLowest.text = "0" + " " + "\(self.units!)"

}
let doubleArray2 = BGdataOutsideDay2.map { Double($0)!}
// print(doubleArray)
// var newArray:[Double] = []


self.doublesArrayDay2 = doubleArray

if doublesArrayDay2.count != 0 {
let bgAvg2 = doubleArray2.reduce(0, combine: +) / Double(doubleArray.count)
let highest2 = doubleArray2.maxElement()
let lowest2 = doubleArray2.minElement()
self.bgHighestDay2 = String(format: "%.1f", highest2!)
self.bgAvgDay2 = String(format: "%.1f", bgAvg2)
self.bgLowestDay2 = String(format: "%.1f", lowest2!)
self.secondDayHighest.text = self.bgHighestDay2! + " " + "\(self.units!)"
self.secondDayAvg.text = self.bgAvgDay2! + " " + "\(self.units!)"
self.secondDayLowest.text = self.bgLowestDay2! + " " + "\(self.units!)"
} else {
self.secondDayHighest.text = "0" + " " + "\(self.units!)"
self.secondDayAvg.text = "0" + " " + "\(self.units!)"
self.secondDayLowest.text = "0" + " " + "\(self.units!)"
setChart(DateOutSideDay1, values: doublesArrayDay1)
setChart(DateOutSideDay2, values: doublesArrayDay2)

}

任何帮助将不胜感激!提前致谢! :)

最佳答案

首先,我认为很奇怪,你有一组数据点和另一组值,但它们的大小不一样?在我看来,这些集合的大小应该相同,因为这些值代表数据点。

任何人..

您可以通过替换 let dataEntry = ChartDataEntry(value: values[i], xIndex: i) 来解决此问题与 let dataEntry = ChartDataEntry(value: i < values.count ? values[i] : 0, xIndex: i)

您可以定义一个默认值,当 values 中该索引处没有可用值时使用。数组,我将其设置为 0 .

关于Swift - iOS 图表 - 在一个 ViewController 中使用两个数据集的两个 LineChart View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42644168/

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