I have a slider that controls the day of the year. In the chart the calculations are time consuming, so if you move the slider a lot it can take an age to finish the update, as it's regenerating each intermediary view.
So, I introduced an onEditingChanged to manage this.
HStack {
Slider(value: $daySlider, in: 0...364, onEditingChanged: { (isBegin) in
if isBegin {
sliderUpdate = false // Begins sliding
} else {
sliderUpdate = true // Finishes sliding
}
})
}
There are two related phenomena going on here:
i) When you release the slider it goes back to its original value.
ii) Due to the way I have written the interim code the chart evaporates whilst the slider is being moved, so you just see.
The code for the chart is, where sliderUpdate is a State variable:
func AnimatedChart()->some View{
if sliderUpdate == true {
Chart{
let filteredArray = solarElevation.filter { $0.day == Int(daySlider) }
ForEach(filteredArray) {item in
LineMark(
x: .value("Hour", ((Calendar.current.dateComponents(components, from: item.date).hour!))+1),
y: .value("Elevation", item.elevation),
series: .value("Hour", "Elevation")
)
.interpolationMethod(.catmullRom)
.lineStyle(StrokeStyle(lineWidth: 2))
.foregroundStyle(.blue)
}
let filteredArrayHour = solarElevation.filter { $0.day == Int(daySlider) && $0.hour == Int(hourSlider) }
ForEach(filteredArrayHour) {itemRule in
RuleMark(x: .value("Hour", itemRule.hour + 1))
.foregroundStyle(Color.orange)
.lineStyle(StrokeStyle(lineWidth: 1))
PointMark(
x: .value("Hour", itemRule.hour + 1),
y: .value("Elevation", itemRule.elevation)
)
.foregroundStyle(.orange)
}
}
.frame(height: 300)
.chartXAxisLabel(position: .bottom, alignment: .center) { Text("Hours") }
// .chartXScale(domain: 0...24)
.chartXAxis{
AxisMarks(values: [0, 6, 12, 18, 24])
}
.chartYScale(domain: -90...90)
.onAppear {
daySlider = Double(Celestial().calculateDayOfYear(Date()))
animateGraph()
}
}
else {
}
}
What do I need to do?