ChartsOrg / Charts

Beautiful charts for iOS/tvOS/OSX! The Apple side of the crossplatform MPAndroidChart.
Apache License 2.0
27.54k stars 5.99k forks source link

SwiftUI: Graph is not updated when @ObservedObject changes #4517

Open kdthomas2121 opened 3 years ago

kdthomas2121 commented 3 years ago

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) -> LineChartView { let chart = LineChartView() chart.data = addData() updateChart() } } `

Within my view:

case .loaded(let data): VStack { LineChartSwiftUI(viewModel: viewModel) }

ramunasjurgilas commented 3 years ago

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

RajeshKanti commented 1 year ago

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

malai520 commented 4 months ago

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