I am trying to add a SwiftUI chart inside a SwiftUI sheet. The chart has around 288 data points and around 10 different AreaMarks. When I try to add any of .chartForegroundStyleScale, .chartXAxis, or .chartYAxis view modifiers I start to notice an animation hitch when dismissing the sheet. I set a breakpoint and noticed that when I add any of these modifiers, ForEach<>._layoutChartContent gets called when I dismiss the sheet. So I am guessing the animation hitch happens because the chart's layout is happening at the same time as the sheet dismiss animation. My question is why does SwiftUI ForEach<>._layoutChartContent get called here ONLY when I add these view modifiers? Here is code:
import SwiftUI
import Charts
struct ContentView: View {
@State var p = true
var body: some View {
ZStack {
Button("press") {
p = true
}
}.sheet(isPresented: $p) {
Chart {
ForEach(0...288, id: \.self) { i in
AreaMark(
x: .value("Time", i),
y: .value("Cost", i)
)
.foregroundStyle(by: .value("", "Cost"))
}
}
.chartForegroundStyleScale(["Test": Color.red])
}
}
}
You will notice the animation hitch when you add around 10 of those AreaMarks.