Charts
Charts copied to clipboard
SwiftUI: Graph is not updated when @ObservedObject changes
- [x] I've read, understood, and done my best to follow the *CONTRIBUTING guidelines.
What did you do?
Create a graph inside a UIViewRepresentable with an @ObservedObject viewModel
Attempted the below update code, which does not work
func updateUIView(_ uiView: LineChartView, context: UIViewRepresentableContext<LineChartSwiftUI>) { chart.data = addData() chart.data?.notifyDataChanged() chart.notifyDataSetChanged() chart.animate(xAxisDuration: 0.01) }
What did you expect to happen?
When the @Published data within the view model changes, the graph should reload
What happened instead?
Nothing
Charts Environment
Latest Xcode Version 12.2 SwiftUI Platform(s) running Charts: Big sur `struct LineChartSwiftUI: UIViewRepresentable { @ObservedObject var viewModel: viewModel
let chart = LineChartView()
func makeUIView(context: UIViewRepresentableContext<LineChartSwiftUI>) -> LineChartView { let chart = LineChartView() chart.data = addData() updateChart() } } `
Within my view:
case .loaded(let data): VStack { LineChartSwiftUI(viewModel: viewModel) }
@kdthomas2121 I have same problem. Did you found solution how to force update chart?
@kdthomas2121 I am having the same problem. Did you find any solution to this problem yet? thanks in advance!
@kdthomas2121 I am having the same problem. Did you find any solution to this problem yet? thanks in advance!