Post

Replies

Boosts

Views

Activity

Index Out of Range when Graphing Heart Data using Date as xAxis
Hello! I am getting error Fatal error: Index out of range when trying to plot the heart rate for the past month with dates as its xAxis. Below is the code im using. Im using the Dateformatter to convert date to string and then NSString to get an Int. This is what im guessing is causing the error The goal is to have the xaxis be the date / time of the heart rate interval and the yaxis be the Beats per minute.  let startDate = Calendar.current.date(byAdding: .month, value: -1, to: Date())  let query = HKSampleQuery(sampleType: sampleType, predicate: predicate, limit: Int(HKObjectQueryNoLimit), sortDescriptors: [sortDescriptor]) { query, result, error in               guard let samples = result as? [HKQuantitySample] else {         return       }               for sample in samples {         let unit = HKUnit(from: "count/min")         let HR = sample.quantity.doubleValue(for: unit)                     let dateFormatter = DateFormatter()       let startDate = dateFormatter.string(from: sample.startDate)       let dateInt = (startDate as NSString).integerValue                 var yValues = [ChartDataEntry]()                   yValues.append(ChartDataEntry(x: Double(dateInt), y: HR))                 let set1 = LineChartDataSet(entries: yValues, label: "Test")                 let data = LineChartData(dataSet: set1)                 self.lineChartView.data = data                 }     }           store.execute(query)
2
0
495
May ’22
Index Out of Range when Graphing Heart Data using Date as xAxis
Hello! I am getting error Fatal error: Index out of range when trying to plot the heart rate for the past month with dates as its xAxis. Below is the code im using. Im using the Dateformatter to convert date to string and then NSString to get an Int. This is what im guessing is causing the error The goal is to have the xaxis be the date / time of the heart rate interval and the yaxis be the Beats per minute.  let startDate = Calendar.current.date(byAdding: .month, value: -1, to: Date())  let query = HKSampleQuery(sampleType: sampleType, predicate: predicate, limit: Int(HKObjectQueryNoLimit), sortDescriptors: [sortDescriptor]) { query, result, error in               guard let samples = result as? [HKQuantitySample] else {         return       }               for sample in samples {         let unit = HKUnit(from: "count/min")         let HR = sample.quantity.doubleValue(for: unit)                     let dateFormatter = DateFormatter()       let startDate = dateFormatter.string(from: sample.startDate)       let dateInt = (startDate as NSString).integerValue                 var yValues = [ChartDataEntry]()                   yValues.append(ChartDataEntry(x: Double(dateInt), y: HR))                 let set1 = LineChartDataSet(entries: yValues, label: "Test")                 let data = LineChartData(dataSet: set1)                 self.lineChartView.data = data                 }     }           store.execute(query)
Replies
2
Boosts
0
Views
495
Activity
May ’22