blog
blog copied to clipboard
How to handle tap gesture in SwiftUI Charts
From iOS 17, SwiftUI Charts has chartGesture, together with SpatialTapGesture we can check tap location and convert that to Charts value
Chart {}
.chartGesture { chart in
SpatialTapGesture()
.onEnded { value in
let result = chart.value(at: value.location, as: (Int, Double).self)
if let index = result?.0, let log = logs[safe: index] {
print(log)
}
}
}