下面的代码在我想高亮显示的区域的顶部和底部绘制了一个RuleMark
,但是,我想把这个区域涂成灰色来显示正常范围,而不是两条线。我该怎么做呢?
import SwiftUI
import Charts
struct ContentView: View {
struct StepCount: Identifiable {
let id = UUID()
let weekday: Date
let steps: Int
init(day: String, steps: Int) {
let formatter = DateFormatter()
formatter.dateFormat = "yyyyMMdd"
self.weekday = formatter.date(from: day) ?? Date.distantPast
self.steps = steps
}
}
let currentWeek: [StepCount] = [
StepCount(day: "20220717", steps: 4200),
StepCount(day: "20220718", steps: 15000),
StepCount(day: "20220719", steps: 2800),
StepCount(day: "20220720", steps: 10800),
StepCount(day: "20220721", steps: 12370),
StepCount(day: "20220722", steps: 9500),
StepCount(day: "20220723", steps: 3700)
]
var body: some View {
VStack {
Chart {
ForEach(currentWeek) {
LineMark(
x: .value("Week Day", $0.weekday, unit: .day),
y: .value("Step Count", $0.steps)
)
}
RuleMark(y: .value("Max of Range", 10000))
.foregroundStyle(.red)
RuleMark(y: .value("Min of Range", 6000))
.foregroundStyle(.red)
}
}
.frame(height: 200)
.padding()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}[![enter image description here][1]][1]
2条答案
按热度按时间tquggr8v1#
如果区域应跨越整个图表(尤其是日期值),则可以按如下方式使用
RectangeMark
:oug3syen2#
您可以使用
AreaMark
或RectangleMark
。以下是使用AreaMark的示例: