91 lines
4.7 KiB
Swift
91 lines
4.7 KiB
Swift
import SwiftUI
|
|
|
|
/// Real timestamps determine x positions; missing data and pauses break the path.
|
|
struct PanelTrendChart: View {
|
|
let dates: [Date]
|
|
let first: [Double?]
|
|
var second: [Double?] = []
|
|
let color: Color
|
|
var secondColor: Color = .orange
|
|
var fixedScale: Double? = 1
|
|
var scaleLabel: String?
|
|
@State private var retainedScale: Double = 1
|
|
@State private var smallSamples = 0
|
|
|
|
private var maximum: Double { max(1, (first + second).compactMap { $0 }.filter(\.isFinite).max() ?? 1) }
|
|
private var middleLabel: String {
|
|
if let fixedScale { return fixedScale == 1 ? "50%" : MetricFormat.bytes(UInt64(max(0, fixedScale / 2))) }
|
|
return MetricFormat.rate(retainedScale / 2)
|
|
}
|
|
var body: some View {
|
|
VStack(spacing: 2) {
|
|
HStack(spacing: 5) {
|
|
VStack(alignment: .trailing) {
|
|
Text(verbatim: scaleLabel ?? (fixedScale == nil ? MetricFormat.rate(retainedScale) : "100%"))
|
|
.lineLimit(1).minimumScaleFactor(0.75)
|
|
Spacer(minLength: 0)
|
|
Text(verbatim: middleLabel).lineLimit(1).minimumScaleFactor(0.75)
|
|
Spacer(minLength: 0)
|
|
Text(verbatim: "0")
|
|
}.font(.system(size: 8)).foregroundStyle(.secondary).frame(width: 52, height: 48)
|
|
Canvas { context, size in
|
|
var grid = Path()
|
|
for step in 0...10 {
|
|
let x = size.width * Double(step) / 10
|
|
grid.move(to: CGPoint(x: x, y: 0)); grid.addLine(to: CGPoint(x: x, y: size.height))
|
|
}
|
|
for step in 0...2 {
|
|
let y = size.height * Double(step) / 2
|
|
grid.move(to: CGPoint(x: 0, y: y)); grid.addLine(to: CGPoint(x: size.width, y: y))
|
|
}
|
|
context.stroke(grid, with: .color(color.opacity(0.14)), lineWidth: 0.5)
|
|
guard let begin = dates.first, let end = dates.last, end > begin else { return }
|
|
let scale = fixedScale ?? retainedScale
|
|
for (values, tint) in [(first, color), (second, secondColor)] {
|
|
var path = Path()
|
|
var connected = false
|
|
var fill = Path()
|
|
var runStart: CGPoint?
|
|
var runEnd: CGPoint?
|
|
func closeRun() {
|
|
guard let start = runStart, let end = runEnd else { return }
|
|
fill.addLine(to: CGPoint(x: end.x, y: size.height))
|
|
fill.addLine(to: CGPoint(x: start.x, y: size.height)); fill.closeSubpath()
|
|
runStart = nil; runEnd = nil
|
|
}
|
|
for index in dates.indices {
|
|
guard index < values.count, let value = values[index], value.isFinite else { closeRun(); connected = false; continue }
|
|
if index > 0 && dates[index].timeIntervalSince(dates[index - 1]) > 5 { closeRun(); connected = false }
|
|
let point = CGPoint(x: size.width * dates[index].timeIntervalSince(begin) / end.timeIntervalSince(begin),
|
|
y: size.height * (1 - min(1, max(0, value / scale))))
|
|
if connected { path.addLine(to: point); fill.addLine(to: point) }
|
|
else { path.move(to: point); fill.move(to: point); runStart = point }
|
|
runEnd = point
|
|
connected = true
|
|
}
|
|
closeRun()
|
|
context.fill(fill, with: .linearGradient(Gradient(colors: [tint.opacity(0.45), tint.opacity(0.03)]), startPoint: .zero, endPoint: CGPoint(x: 0, y: size.height)))
|
|
context.stroke(path, with: .color(tint), style: StrokeStyle(lineWidth: 1.5, lineCap: .round))
|
|
}
|
|
}.frame(height: 48)
|
|
}
|
|
HStack {
|
|
if let date = dates.first { Text(date, style: .time) }
|
|
Spacer()
|
|
if let date = dates.last { Text(date, style: .time) }
|
|
}.font(.system(size: 9)).foregroundStyle(.secondary).frame(maxWidth: .infinity)
|
|
}
|
|
.frame(maxWidth: .infinity)
|
|
.onAppear { retainedScale = maximum }
|
|
.onChange(of: dates.last) { _, _ in
|
|
let target = maximum
|
|
if target > retainedScale { retainedScale = target; smallSamples = 0 }
|
|
else if target < retainedScale * 0.5 {
|
|
smallSamples += 1
|
|
if smallSamples >= 10 { retainedScale = target; smallSamples = 0 }
|
|
} else { smallSamples = 0 }
|
|
}
|
|
.accessibilityHidden(true)
|
|
}
|
|
}
|