Hey I am utilizing the brand new SwiftUI Charts to show a graph to show a line graph for a consumer’s weight. The problem that I’m operating into particularly is attempting to animate the scroll place of the chart. Under is a snippet of the code that I’ve tried to realize this.
@State var scrollPosition: Date
var physique: some View {
Button {
withAnimation {
// replace scrollPosition property to some date
var present = scrollPosition
present.addTimeInterval(-86400)
scrollPosition = present
}
} label: {
Textual content("scroll to a different date")
}
Chart {
Foreach(knowledge) { dataPoint in
LineMark(x: .worth("Day", dataPoint.knowledge), y: .worth("Worth", dataPoint.worth))
}
}
.chartScrollableAxes(.horizontal)
.chartScrollPosition(x: $scrollPosition)
}
So once I faucet on the button and replace the scrollPosition variable with a brand new date, the chart primarily refreshes and updates to the right location on the x axis, nonetheless, there isn’t any animation triggered. I hoped that wrapping the scrollPosition throughout the animation block would obtain the animation that I hoped for however no such luck.
Is that this the right approach to programmatically scroll to a place within the chart? I’ve checked out different options the place (iOS 16 and beneath) the place they wrapped the Chart view behind a scrollView and use the scrollView’s perform to scroll however then that introduces a number of different issues (akin to dropping the y Axis labels when scrolling) so I would favor to not do this.
So yeah is there any approach to programmatically animate the scroll place to make it look clean? Is also it attainable to disable consumer’s contact on the Chart? I would favor to have buttons to show a set date vary relatively than let customers scroll themselves. Thanks