Skip to content
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

Closed
USBA opened this issue Feb 3, 2021 · 2 comments
Closed

Chart does not refresh on State change #5

USBA opened this issue Feb 3, 2021 · 2 comments

Comments

@USBA
Copy link

USBA commented Feb 3, 2021

My enter view refreshes on @State change except the chart.

@State var component: Calendar.Component = .month
@State var periodCount: Int = 0

The chart does not refresh until I drag on it.

Screen.Recording.2021-02-03.at.7.59.39.PM.mov
LineChart()
           .touchOverlay()
           .xAxisGrid()
           .yAxisGrid()
           .xAxisLabels()
           .yAxisLabels()
           .environmentObject(
                      ChartData(dataPoints : chartDataPoints(periodChange: periodCount, periodComponent: component) )
            )
            .frame(height: horizontalSizeClass == .compact ? 200 : 250)
            .padding()

Please fix this.
Thanks so much for this amazing SwiftUI library.

@willdale
Copy link
Owner

willdale commented Feb 3, 2021

Hi,

I believe this is because the data being passed into the environmentObject is not @State. This means that a change of the datas state won't cause the views state to change.

While I don't know what the function chartDataPoints() does, I've make one that returns [ChartDataPoint] .

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

@USBA
Copy link
Author

USBA commented Feb 3, 2021

This works. Thanks so much for the quick reply @willdale

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants