blog icon indicating copy to clipboard operation
blog copied to clipboard

How to handle tap gesture in SwiftUI Charts

Open onmyway133 opened this issue 6 months ago • 0 comments

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)
                }
            }
    }

onmyway133 avatar Aug 07 '24 13:08 onmyway133