ChartsOrg / ChartsOrg/Charts

SwiftUI: Graph is not updated when @ObservedObject changes

Open
#4,517 3 comments 4 reactions 0 assignees View on GitHub
Dominant language
Swift
Stars
28k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

* [x] I've read, understood, and done my best to follow the [*CONTRIBUTING guidelines](https://github.com/jjatie/Charts/blob/master/CONTRIBUTING.md).

## 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) {
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) }
`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.