我正在尝试建立一个线图视图使用SwiftUI中的Swift图表。我有x值从0到23,和随机y值30和40之间,但图是覆盖整个屏幕,我只需要从30到40的y轴上的部分。我该怎么办?代码:
struct PatientData: Decodable, Encodable, Identifiable {
var id: Int?
let temperature: Double
let timestamp: Int
init(id: Int, temperature: Double, timestamp: Int) {
self.id = id
self.temperature = temperature
self.timestamp = timestamp
}
}
func generateData() -> [PatientData]{
var data: [PatientData] = []
for i in 0...23 {
data.append(
PatientData(id: i, temperature: Double.random(in: 30.0...40.0), timestamp: i)
)
}
return data
}
struct TemperatureChartView: View {
var data = generateData()
var body: some View {
Chart (data){
LineMark(x: .value("Time", $0.timestamp), y: .value("Temperature", $0.temperature))
}
}
}
struct TemperatureChartView_Previews: PreviewProvider {
static var previews: some View {
TemperatureChartView()
}
}
1条答案
按热度按时间zbdgwd5y1#
您需要在图表视图的末尾添加以下修饰符:
.chartYScale(domain: 30.0…40.0)
下面是完整的代码沿着一些更改。