TapGesture on SwiftUI Scrollable Chart (iOS 17.0)

86 Views Asked by At

I'm trying to detect TapGesture on BarMark on SwiftUI Chart (iOS 17.0), but unsuccessfully. I'm aware that I can use the chartXSelection modifier but it reacts on drag gestures which collides with scrolling and leads to not very best user experience.

Do you have any suggestions? Thanks

var body: some View {
        Chart {
            ActivitiesChartContent(activities: $activities)
            
            HistoryDataChartContent(quantities: $quantities)
        }
        .chartScrollableAxes(.horizontal)
        .chartXSelection(value: $rawSelectedDate)
        .onChange(of: rawSelectedDate) { oldValue, newValue in
            guard let newValue,
                  let activity = activities.first(where: { ($0.startDate...$0.endDate).contains(newValue) }) else { return }
            
            onTap?(activity)
        }
    }

I tried:

  1. Overriding chartGesture, but I couldn't find a way to detect TapGesture location.
  2. Using chartOverlay as suggested in other threads, but it doesn't contain the whole frame so it can't detect the real location
0

There are 0 best solutions below