I am creating a workout tracking app and trying to plot the largest weight value on a day over time for a given exercise. The entity "ExerciseSet" contains attributes dateCompleted and weight. There are multiple ExerciseSets saved on the same date, so I want to sort the ExerciseSets descending by weight for each dateCompleted and return the first element (being the max) for each date so that there is one entry (the max) for each date. How can I modify the fetch request to do this? Any suggestions would be greatly appreciated. As of now, there are multiple points for each date when I want just the max weight. My code is here:
import SwiftUI
import Charts
struct ExerciseDetailView: View {
@FetchRequest var exercisesets: FetchedResults<ExerciseSet>
var exercise: String
init(exercise: String) {
self.exercise = exercise
self._exercisesets = FetchRequest(
entity: ExerciseSet.entity(),
sortDescriptors: [NSSortDescriptor(key: "weight", ascending: false)],
predicate: NSPredicate(format: "exercisename == %@", exercise as any CVarArg)
)}
var body: some View {
NavigationView {
ScrollView {
if #available(iOS 16.0, *) {
Chart(exercisesets) { e in
LineMark(x: .value("Date", e.dateCompleted.startOfDay),
y: .value("Reps", e.reps)
)}
}
else {
Text("Oops")
// Fallback on earlier versions
}
}
}
}
}