简体   繁体   中英

How to change the color of BarView in SwiftUI charts when we tap on it

I have created a chart view like below. I wanted to know how to change the bar mark when user tap on that bar.

  Chart {
            ForEach(Data.lastOneHour, id: \.day) {
                BarMark(
                    x: .value("Month", $0.day, unit: .hour),
                    y: .value("Duration", $0.duration)
                )
            }
        }

I see.onTap modifier is not available on BarMark. And I don't see any way to access this barmark and apply color using gesture location by using Geometryreader.

You could try this approach, using a chartOverlay and a select variable to change the bar color when the user tap on that bar.

struct ContentView: View {
    let measurement: [Measurement] = [
        Measurement(id: "1", val: 11.2),
        Measurement(id: "2", val: 22.2),
        Measurement(id: "3", val: 38.2)
    ]
    
    @State var select = "0"
    
    var body: some View {
        Chart(measurement) { data in
            BarMark(x: .value("Time", data.id), y: .value("val", data.val))
                .foregroundStyle(select == data.id ? .red : .blue)
        }
        .chartOverlay { proxy in
            GeometryReader { geometry in
                ZStack(alignment: .top) {
                    Rectangle().fill(.clear).contentShape(Rectangle())
                        .onTapGesture { location in
                            doSelection(at: location, proxy: proxy, geometry: geometry)
                        }
                }
            }
        }
    }
    
    func doSelection(at location: CGPoint, proxy: ChartProxy, geometry: GeometryProxy) {
        let xPos = location.x - geometry[proxy.plotAreaFrame].origin.x
        guard let xbar: String = proxy.value(atX: xPos) else { return }
        select = xbar
    }
    
}

struct Measurement: Identifiable {
    var id: String
    var val: Double
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM