Charts icon indicating copy to clipboard operation
Charts copied to clipboard

SwiftUI: Graph is not updated when @ObservedObject changes

Open kdthomas2121 opened this issue 4 years ago • 3 comments

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 avatar Nov 30 '20 19:11 kdthomas2121

@kdthomas2121 I have same problem. Did you found solution how to force update chart?

ramunasjurgilas avatar Sep 13 '21 06:09 ramunasjurgilas

@kdthomas2121 I am having the same problem. Did you find any solution to this problem yet? thanks in advance!

RajeshKanti avatar Aug 03 '23 00:08 RajeshKanti

@kdthomas2121 I am having the same problem. Did you find any solution to this problem yet? thanks in advance!

malai520 avatar Jun 08 '24 02:06 malai520