-
-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Chart does not refresh on State change #5
Comments
Hi, I believe this is because the data being passed into the environmentObject is not While I don't know what the function There maybe a more elegant solution but without more info I don't know. @Environment(\.horizontalSizeClass) var horizontalSizeClass
@State var component: Calendar.Component = .month
@State var periodCount: Int = 0
// make data a `@State` variable
@State var data = ChartData(dataPoints: [])
var body: some View {
VStack {
LineChart()
.touchOverlay()
.xAxisGrid()
.yAxisGrid()
.xAxisLabels()
.yAxisLabels()
.environmentObject(data)
.padding()
Button(action: {
periodCount += 1
}, label: {
Text("Increment")
})
}
// Re-Initialise data at `onAppear`
.onAppear {
data.dataPoints = chartDataPoints(periodChange: periodCount, periodComponent: component)
}
// detect changes
.onChange(of: periodCount, perform: { value in
data.dataPoints = chartDataPoints(periodChange: value, periodComponent: component)
})
}
func chartDataPoints(periodChange: Int, periodComponent: Calendar.Component) -> [ChartDataPoint] {
var test : [ChartDataPoint] = []
let one : Double = Double(periodChange + 1)
let two : Double = Double(periodChange + 4)
for _ in 1...6 {
let value = Double.random(in: one...two)
test.append(ChartDataPoint(value: value))
}
return test
} Screen.Recording.2021-02-03.at.17.44.30.mov |
This works. Thanks so much for the quick reply @willdale |
My enter view refreshes on
@State
change except the chart.The chart does not refresh until I drag on it.
Screen.Recording.2021-02-03.at.7.59.39.PM.mov
Please fix this.
Thanks so much for this amazing SwiftUI library.
The text was updated successfully, but these errors were encountered: