Estou curioso, há uma maneira de dizer ao chartXSelection(value:)
modificador para incluir a área do eixo x ao responder aos gestos do usuário? Sei que iniciar um toque na área do gráfico e arrastar para baixo funciona, mas gostaria que o usuário pudesse apenas tocar em um valor que ele gosta no eixo x e tê-lo registrado por chartXSelection
.
Veja o código de exemplo abaixo (editado para incluir tall axisMarks para demonstrar um problema com uma solução proposta):
private struct PlotPoint {
let x: Double
let y: Double
}
private struct PlotSeries: Identifiable {
let id: Int
let name: String
let points: [PlotPoint]
}
private let data: [PlotSeries] = [
PlotSeries(id: 0, name: "Series A", points: stride(from: 0.0, through: 1.0, by: 0.05).map { y in PlotPoint(x: y*y, y: y) }),
PlotSeries(id: 1, name: "Series B", points: stride(from: 0.0, through: 1.0, by: 0.05).map { y in PlotPoint(x: y*y, y: y/2) })
]
struct AxisTap: View {
@State private var selectedX: Double?
var body: some View {
VStack {
Chart {
if let selectedX {
BarMark(xStart: .value("x", selectedX), xEnd: .value("x", selectedX + 0.05))
.foregroundStyle(.yellow.opacity(0.5))
}
ForEach(data) { series in
let plot = LinePlot( series.points, x: .value("x", \.x), y: .value("y", \.y))
plot
.symbol(by: .value("Series", series.name))
.foregroundStyle(by: .value("Series", series.name))
}
}
.chartXAxis {
AxisMarks() { value in
AxisGridLine()
AxisTick()
AxisValueLabel() {
if let axisValue = value.as(Double.self) {
VStack {
Text("|")
Text("v")
Text("\(axisValue.formatted())")
}
}
}
}
}
.chartXSelection(value: $selectedX)
.padding()
Text("selectedX is: \(selectedX ?? 0.0)")
}
}
}
Gostaria que o usuário pudesse tocar em "0,5" no eixo x e obter um resultado como o mostrado abaixo (com "selectedX is: ~0,5" abaixo).