span
集团的。
.position(by: .value("Workout(type)", element.workoutType), span: 40)
Workout
struct Workout: Identifiable, Hashable {
let id = UUID()
let day: String
let minutes: Int
let kind: Kind
enum Kind {
case walk, run
}
}
在
findElement
,看看是否
location.x
位于组中心X的左侧或右侧。
func findElement(location: CGPoint, proxy: ChartProxy, geometry: GeometryProxy) -> Workout? {
guard let frame = proxy.plotFrame,
let x = proxy.value(atX: location.x - geometry[frame].origin.x, as: String.self),
let xRange = proxy.positionRange(forX: x) else { return nil }
let midPoint = (xRange.lowerBound + xRange.upperBound) / 2
return if location.x - geometry[frame].origin.x < midPoint { // walk
workoutData[0].data.first { $0.day == x }
} else { // run
workoutData[1].data.first { $0.day == x }
}
}
在
chartBackground
kind
然后将其向左/向右移动10点。
.chartBackground { proxy in
ZStack (alignment: .topLeading) {
GeometryReader { geo in
if let selectedElement, let frame = proxy.plotFrame, let x = proxy.position(forX: selectedElement.day) {
let offset: CGFloat = selectedElement.kind == .run ? 10 : -10
let height = geo[frame].height
Rectangle()
.fill(.quaternary)
.frame(width: 2, height: height)
.position(x: x + offset + geo[frame].minX, y: height / 2)
// the rectangle containing the info goes here...
}
}
}
}
这是一个最小的可重复示例。处理从右到左的布局是一个练习。
struct Workout: Identifiable, Hashable {
let id = UUID()
let day: String
let minutes: Int
let kind: Kind
enum Kind {
case walk, run
}
}
extension Workout {
static let walkWorkout: [Workout] = [
.init(day: NSLocalizedString("mon", comment: ""), minutes: 23, kind: .walk),
.init(day: "Tue", minutes: 35, kind: .walk),
.init(day: "Wed", minutes: 55, kind: .walk),
.init(day: "Thu", minutes: 30, kind: .walk),
.init(day: "Fri", minutes: 15, kind: .walk),
.init(day: "Sat", minutes: 65, kind: .walk),
.init(day: "Sun", minutes: 81, kind: .walk),
]
static let runWorkout: [Workout] = [
.init(day: NSLocalizedString("mon", comment: ""), minutes: 16, kind: .run),
.init(day: "Tue", minutes: 12, kind: .run),
.init(day: "Wed", minutes: 55, kind: .run),
.init(day: "Thu", minutes: 34, kind: .run),
.init(day: "Fri", minutes: 22, kind: .run),
.init(day: "Sat", minutes: 43, kind: .run),
.init(day: "Sun", minutes: 90, kind: .run),
]
}
let workoutData = [
(workoutType: "Walk", data: Workout.walkWorkout),
(workoutType: "Run", data: Workout.runWorkout)
]
struct ContentView: View {
@State var selectedElement: Workout?
func findElement(location: CGPoint, proxy: ChartProxy, geometry: GeometryProxy) -> Workout? {
guard let frame = proxy.plotFrame,
let x = proxy.value(atX: location.x - geometry[frame].origin.x, as: String.self),
let xRange = proxy.positionRange(forX: x) else { return nil }
let midPoint = (xRange.lowerBound + xRange.upperBound) / 2
return if location.x - geometry[frame].origin.x < midPoint { // walk
workoutData[0].data.first { $0.day == x }
} else { // run
workoutData[1].data.first { $0.day == x }
}
}
var body: some View {
Chart {
ForEach(workoutData, id: \.workoutType) { element in
ForEach(element.data) { workout in
BarMark(
x: .value("Day", workout.day),
y: .value("Workout(in minutes)", workout.minutes)
)
}
.foregroundStyle(by: .value("Workout(type)", element.workoutType))
.position(by: .value("Workout(type)", element.workoutType), span: 40)
}
}
.chartYAxis {
AxisMarks(position: .leading, values: Array(stride(from: 0, through: 100, by: 10))) {
axis in
AxisTick()
AxisGridLine()
AxisValueLabel("\((axis.index * 10))", centered: false)
}
}
.chartBackground { proxy in
ZStack (alignment: .topLeading) {
GeometryReader { geo in
if let selectedElement, let frame = proxy.plotFrame, let x = proxy.position(forX: selectedElement.day) {
let offset: CGFloat = selectedElement.kind == .run ? 10 : -10
let height = geo[frame].height
Rectangle()
.fill(.quaternary)
.frame(width: 2, height: height)
.position(x: x + offset + geo[frame].minX, y: height / 2)
VStack(alignment: .leading) {
Text("\(selectedElement.id)")
.font(.callout)
.foregroundStyle(.secondary)
Text("\(selectedElement.day)\n\(selectedElement.minutes)")
.font(.body.bold())
.foregroundColor(.primary)
}
.frame(width: 150, alignment: .leading)
.background {
ZStack {
RoundedRectangle(cornerRadius: 8)
.fill(.background)
RoundedRectangle(cornerRadius: 8)
.fill(.quaternary.opacity(0.7))
}
.padding([.leading, .trailing], -8)
.padding([.top, .bottom], -4)
}
.offset(x: min(x + offset + geo[frame].minX, geo[frame].maxX - 150))
}
}
}
}
.chartOverlay { proxy in
GeometryReader { nthGeometryItem in
Rectangle().fill(.clear).contentShape(Rectangle())
.gesture(
SpatialTapGesture()
.onEnded { value in
let element = findElement(location: value.location, proxy: proxy, geometry: nthGeometryItem)
if selectedElement?.id == element?.id {
selectedElement = nil
} else {
selectedElement = element
}
}
.exclusively(
before: DragGesture()
.onChanged { value in
selectedElement = findElement(location: value.location, proxy: proxy, geometry: nthGeometryItem)
}
)
)
}
}
.padding()
}
}