我需要创建一个组件,使用户能够通过水平滚动SwiftUI图表来 Select 特定日期的值.组件的图像如下所示.
我的代码块如下:
struct SampleModel: Identifiable {
var id = UUID()
var date: String
var value: Double
var animate: Bool = false
}
struct ContentView: View {
@State private var data = [
SampleModel(date: "26\nFr", value: 9.2),
SampleModel(date: "27\nSa", value: 12.5),
SampleModel(date: "28\nSu", value: 15.0),
SampleModel(date: "29\nMo", value: 20.0),
SampleModel(date: "30\nTu", value: 5.0),
SampleModel(date: "31\nWe", value: 7.0),
SampleModel(date: "01\nTh", value: 3.0),
SampleModel(date: "02\nFr", value: 20.0),
SampleModel(date: "03\nSa", value: 5.0),
SampleModel(date: "04\nSu", value: 7.0),
SampleModel(date: "05\nMo", value: 3.0),
SampleModel(date: "06\nTu", value: 10.0),
SampleModel(date: "07\nWe", value: 21.0),
SampleModel(date: "08\nTh", value: 14.0),
SampleModel(date: "09\nFr", value: 10.0),
SampleModel(date: "10\nSt", value: 7.0),
SampleModel(date: "11\nSu", value: 15.0),
SampleModel(date: "12\nMo", value: 17.0),
SampleModel(date: "13\nTu", value: 29.0)
]
@State private var scrollPosition: String = "26\nFr"
@State var priceText: String = ""
var body: some View {
GeometryReader { geometry in
VStack {
Text(priceText)
.padding()
Chart(data) { flight in
BarMark(x: .value("Date", flight.date),
y: .value("Price", flight.value),
width: 10.0)
.foregroundStyle(
Gradient(
colors: [
.blue,
.green
]
)
)
.clipShape(RoundedRectangle(cornerRadius: 16))
}
.frame(width: .infinity, height: 180)
.background(content: {
VStack {
Color.gray.frame(width: 1)
Color.clear.frame(height: 40)
}
})
.chartXAxis(content: {
AxisMarks(preset: .extended, position: .bottom) { value in
let label = value.as(String.self)!
AxisValueLabel(label)
.foregroundStyle(.gray)
}
})
.chartScrollableAxes(.horizontal)
.chartXVisibleDomain(length: 11)
.chartYAxis(.hidden)
.chartScrollTargetBehavior(
.valueAligned(unit: 1)
)
.chartOverlay { proxy in
}
}
.frame(width: .infinity)
}
}
}
我需要观察水平滚动图表的内容偏移,以便使用proxy
of chartOverlay
根据位置检索中心值.有没有办法观察滚动图表的内容偏移量?或者是否有另一个Angular 来检索滚动图表中的中心值?