Swift Charts y-axis scale changes when RuleMark annotation is displayed

In a Swift Chart that listens for drag gestures in order to display a RuleMark with an annotation, the y-axis scale changes while dragging which causes the entire chart to jump. I have a small code example that demonstrates this at https://github.com/mvolkmann/ChartJump. See the two screenshots there.

Replies

I have the very same problem with the annotation using chartOverlay. Did you come up with a solution? I have been thinking about it for 2 days and I concluded nothing…

I came up with a simple solution consisting in just setting the Y (or X) axes scale manually in this way (added chartXScale also, as sometimes I have experienced the same issue also with X axis domain):


Chart {
 // Chart implementation
}
.chartYScale(domain: 0...50)
.chartXScale(domain: 0...25)

In this way you can prevent the jump to happen.

Thank you SO MUCH @MTBff! That solves it for me!

Overriding the default overflow resolution fixes this:

.annotation(overflowResolution: .init(x: .fit(to: .chart)) { ... }
  • This solution sorted it for my use case

Add a Comment