与这个问题密切相关的是,线性梯度代码运行良好,但它总是与其数据相关联,因此例如下面的代码,如果您有一系列从 70 到 110 的心率,则较低的心率始终为灰色,较高的心率始终为灰色紫色,但 90 到 195 的数组也是如此。如何映射停靠点以使颜色与心率区域相对应?换句话说,心率从 70-110 只会显示蓝色到橙色?
Chart {
ForEach(smoothHeartRatesEMA(customHeartRates, decayFactor: 0.3)) { heartRate in
LineMark(
x: .value("Sample Time", heartRate.startDate, unit: .nanosecond), //changed these to .nanosecond to fix Nike Run Club bug (some how Nike Run Club gets more frequent HR samples than other apps?)
y: .value("Heart Rate", heartRate.doubleValue)
)
.lineStyle(StrokeStyle(lineWidth: 3.0))
.foregroundStyle(
.linearGradient(
stops: [
.init(color: Color.gray, location: 0.0),
.init(color: TrackerConstants.AppleFitnessBlue, location: 0.16),
.init(color: TrackerConstants.AppleFitnessYellow, location: 0.33),
.init(color: TrackerConstants.AppleFitnessOrange, location: 0.5),
.init(color: TrackerConstants.AppleFitnessRed, location: 0.66),
.init(color: TrackerConstants.AppleFitnessPurple, location: 1.0) //how do I get these to allign with a range of e.g. 170-210. I.e. if no heart rate is above 170bpm, the line is never purple?
],
startPoint: .bottom,
endPoint: .top)
)
}
}
.chartYScale(domain: 50...210)