A horizontally scrollable Swift Chart writes an incorrect date to its chartScrollPosition(x:) binding when I tap the chart to stop its deceleration while the height of a view above the chart changes.
The binding receives a date near the beginning of the chart’s X-axis domain, even though the chart’s visible viewport remains near its previous position. Scrolling again causes the binding to update to a date matching the visible viewport.
Steps to reproduce:
Run the attached sample and open “Chart Header Height Reproduction”.
Leave “Keep header height fixed” turned off.
Swipe the chart horizontally across a boundary between a period with readings and a period without readings. The header above the chart changes height at this boundary.
While the chart is still decelerating, tap it to stop the scroll.
Observe the displayed scroll position and the [CHART_SCROLL_TRACE] console output.
Expected result:
The scroll-position binding continues to represent the chart’s visible position. Stopping deceleration and changing the height of a view above the chart should not cause the binding to receive a date near the beginning of the domain.
Actual result:
The binding receives a date near 1 January 2012, the beginning of the X-axis domain, while the chart’s viewport remains near its previous position in 2026. Scrolling again updates the binding to a date matching the viewport.
Additional observations:
The issue does not reproduce in my testing if I let the chart finish decelerating without tapping it.
The issue also stops reproducing when “Keep header height fixed” is enabled.
The call stack for the unexpected binding write includes Charts and SwiftUI geometry-update frames.
Changing the chart’s leading inset changes the unexpected date. When the viewport’s global leading X coordinate was 56 points, the incoming date corresponded to 56 points from the full plot’s domain start. When the leading X coordinate was 86 points, the incoming date corresponded to 86 points from the domain start. This suggests a possible coordinate-space issue during the geometry update, though I cannot verify the internal calculation.
Environment:
Device: iPhone 15, iPhone 17 pro (simulator)
iOS version and build: 27.0.1, 27.0
Xcode version and build: 27.0
The attached sample uses generated data.
struct ChartScrollHeightReproduction: View {
@StateObject private var model = ScrollHeightModel()
@State private var fixedHeaderHeight = false
var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 16) {
Text("Swipe the chart, then tap while it is decelerating. Repeat across periods with and without readings.")
.font(.caption)
Toggle("Keep header height fixed", isOn: $fixedHeaderHeight)
if fixedHeaderHeight {
ZStack(alignment: .leading) {
dataHeader.opacity(model.hasVisibleData ? 1 : 0)
emptyHeader.opacity(model.hasVisibleData ? 0 : 1)
}
} else if model.hasVisibleData {
dataHeader
} else {
emptyHeader
}
VStack(spacing: 12) {
Text("Scroll position: \(model.position.formatted(date: .abbreviated, time: .shortened))")
.font(.caption.monospaced())
.frame(maxWidth: .infinity, alignment: .leading)
Chart(model.visiblePoints) { point in
BarMark(
x: .value("Date", point.date),
yStart: .value("Minimum", point.value - 8),
yEnd: .value("Maximum", point.value + 8),
width: .fixed(8)
)
.foregroundStyle(model.selectedID == point.id ? .red : .pink)
.cornerRadius(4)
}
.chartXAxis {
AxisMarks(values: model.monthStarts) { value in
if let date = value.as(Date.self) {
AxisValueLabel {
Text(date.formatted(date: .abbreviated, time: .omitted))
.font(.caption2)
}
AxisGridLine()
}
}
}
.chartXScale(domain: model.domain)
.chartXVisibleDomain(length: 86_400)
.chartYScale(domain: 0...100)
.chartScrollableAxes(.horizontal)
.chartScrollPosition(x: Binding(
get: { model.position },
set: { model.receivePosition($0) }
))
.onTapGesture {
model.selectedID = model.visiblePoints.min {
abs($0.date.timeIntervalSince(model.position)) < abs($1.date.timeIntervalSince(model.position))
}?.id
}
.padding()
}
.frame(height: 250)
.background(Color.white)
}
.padding()
}
.navigationTitle("Chart Scroll Reproduction")
}
private var dataHeader: some View {
VStack(alignment: .leading, spacing: 8) {
Text("Heart rate").font(.subheadline)
Text("Readings available").font(.title2.bold())
Text("Swipe into a period without readings").font(.caption)
}
.padding(.vertical, 8)
}
private var emptyHeader: some View {
Text("-- bpm").font(.title).padding(.vertical, 8)
}
}
private struct ScrollHeightPoint: Identifiable {
let id = UUID()
let date: Date
let value: Double
}
Thanks for the focused sample. I reproduced the problem with your project on an iPhone 18 Pro Max running iOS 27.2.1. I also reproduced it with a minimal version that binds chartScrollPosition(x:) directly to a @State date. It has no model and no debounce, and it gives the chart the full data set. In both, the binding received the same date near the start of the domain while the viewport stayed in 2026.
In my tests, the wrong dates started right after the header changed height. In your sample, that came just after the tap stopped the chart, and in my other versions it came mid-scroll. Once they started, the binding alternated between the real position and the wrong date for as long as the chart kept moving. Your sample's 100 ms debounce commits whichever value arrives last, so a wrong date can end up as position.
A view whose height follows the chart's scroll position moves the chart every time the position crosses a boundary. Apple's Visualizing your app's data sample has the same kind of screen: a summary of the visible range above a scrollable chart. In its SalesDetails view, the same three Text views stay above the chart at every position, and only their strings change. It binds a @State date directly with $scrollPositionStart. Your "Keep header height fixed" layout avoided the wrong dates in every variant I tried it with. It keeps both headers in a ZStack and changes only their opacity.
The documentation for chartScrollPosition(x:) says it "Associates a binding to be updated when the chart scrolls along the x-axis." The wrong dates don't match where the chart scrolled, so please file a bug report in Feedback Assistant with your sample attached. Your report can also note that the problem happens with a plain @State binding. If you post the Feedback number here I'll check the status next time I do a sweep of forums posts where I've suggested bug reports.