86 lines
2.9 KiB
Swift
86 lines
2.9 KiB
Swift
|
import SwiftUI
|
||
|
import Charts
|
||
|
|
||
|
struct TemperatureDayOverview: View {
|
||
|
|
||
|
let storage: TemperatureStorage
|
||
|
|
||
|
@State
|
||
|
var points: [TemperatureMeasurement] = []
|
||
|
|
||
|
init(storage: TemperatureStorage, dateIndex: Int) {
|
||
|
self.storage = storage
|
||
|
let points = storage.loadMeasurements(for: dateIndex)
|
||
|
print("Loaded \(points.count) points for date \(dateIndex)")
|
||
|
self.points = points
|
||
|
update()
|
||
|
}
|
||
|
|
||
|
mutating func update() {
|
||
|
self.upperTempLimit = max(40, points.maximumValue() ?? 40)
|
||
|
self.lowerTempLimit = min(-20, points.minimumValue() ?? -20)
|
||
|
let startDay = points.first?.date.dateIndex ?? Date().dateIndex
|
||
|
self.pastDateLimit = Date(dateIndex: startDay)
|
||
|
let endDay = (points.last?.date.dateIndex ?? Date().dateIndex) + 1
|
||
|
self.futureDateLimit = Date(dateIndex: endDay)
|
||
|
}
|
||
|
|
||
|
var upperTempLimit: Double = 40.0
|
||
|
var lowerTempLimit: Double = -20
|
||
|
|
||
|
var pastDateLimit: Date = Date().startOfDay
|
||
|
var futureDateLimit: Date = Date().startOfNextDay
|
||
|
|
||
|
var body: some View {
|
||
|
Chart {
|
||
|
ForEach(points) { point in
|
||
|
if let s = point.sensor0.optionalValue {
|
||
|
LineMark(
|
||
|
x: .value("Date", point.date),
|
||
|
y: .value("Temperature", s))
|
||
|
.foregroundStyle(by: .value("Type", "Sensor 0"))
|
||
|
}
|
||
|
if let s = point.sensor1.optionalValue {
|
||
|
LineMark(
|
||
|
x: .value("Date", point.date),
|
||
|
y: .value("Temperature", s))
|
||
|
.foregroundStyle(by: .value("Type", "Sensor 1"))
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
.aspectRatio(2.6, contentMode: .fit)
|
||
|
//.chartXScale(domain: pastDateLimit...futureDateLimit)
|
||
|
.chartYScale(domain: lowerTempLimit...upperTempLimit)
|
||
|
.chartXAxis {
|
||
|
AxisMarks(preset: .automatic)
|
||
|
AxisMarks.init(values: AxisMarkValues.stride(by: .hour)) {
|
||
|
AxisGridLine()
|
||
|
}
|
||
|
}
|
||
|
.chartYAxis {
|
||
|
AxisMarks(position: .trailing, values: .automatic) { value in
|
||
|
AxisValueLabel(multiLabelAlignment: .trailing) {
|
||
|
if let intValue = value.as(Int.self) {
|
||
|
Text("\(intValue)°")
|
||
|
.font(.system(size: 10))
|
||
|
//.foregroundColor(.white)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
AxisMarks.init(values: AxisMarkValues.stride(by: 5)) {
|
||
|
AxisGridLine()
|
||
|
}
|
||
|
}
|
||
|
.padding()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
struct TemperatureDayOverview_Previews: PreviewProvider {
|
||
|
static var previews: some View {
|
||
|
TemperatureDayOverview(storage: TemperatureStorage.mock, dateIndex: Date().dateIndex)
|
||
|
.previewLayout(.fixed(width: 350, height: 150))
|
||
|
//.background(.gray)
|
||
|
}
|
||
|
}
|