BlackWolf
BlackWolf

Reputation: 5629

SwiftUI Charts: Animating foregroundStyle of a Mark on iOS17

I'm trying to animate a change of data of a bar chart using Swift Charts. While this works fine on iOS18, the animation of the color does not work on iOS17. Since this also seems to be undocumented and such a simple thing to do, I'm wondering if this is simply not supported on iOS17 or if I'm doing something wrong.

Code:

struct ContentView: View {
    struct Month: Identifiable, Equatable {
        var id: String {
            return name
        }
        
        let value: Double
        let name: String
        let color: Color
    }
    
    @State var months: [Month] = [
        .init(value: 100, name: "Jan", color: .red),
        .init(value: 150, name: "Feb", color: .red),
        .init(value: 200, name: "Mar", color: .red),
    ]
    
    var body: some View {
        VStack {
            Chart {
                ForEach(months) { month in
                    BarMark(
                        x: .value("", month.value),
                        y: .value("", month.name)
                    )
                    .foregroundStyle(month.color)
                }
            }
            .frame(height: 122)
            .animation(.smooth, value: months)
            
            Button("Change") {
                months = [
                    .init(value: 250, name: "Jan", color: .blue),
                    .init(value: 70, name: "Feb", color: .blue),
                    .init(value: 300, name: "Mar", color: .blue),
                ]
            }
        }
    }
}

The code is quite straightforward – we built a barchart from the months array and each bar has a month name, value and color attached. On button click, we change the data. The length of the individual bars as well as the axis animate perfectly fine, but the color does not:

enter image description here

I also tried

Surprisingly, I also couldn't find any related questions, so any pointers (or just confirmation this is not supported on iOS17 with a source) would be appreciated.

Upvotes: 1

Views: 61

Answers (0)

Related Questions