37 lines
1.7 KiB
Swift
37 lines
1.7 KiB
Swift
import Foundation
|
|
|
|
/// A render frame may keep one outgoing point beyond the bounded 60-sample history.
|
|
nonisolated struct PanelCurveFrame: Equatable {
|
|
let dates: [Date]
|
|
let series: [[Double?]]
|
|
let intervals: [TimeInterval]
|
|
let boundaryDates: Set<Date>
|
|
|
|
struct Domain {
|
|
let begin: TimeInterval
|
|
let end: TimeInterval
|
|
func position(_ date: Date) -> Double {
|
|
end > begin ? (date.timeIntervalSinceReferenceDate - begin) / (end - begin) : 1
|
|
}
|
|
}
|
|
|
|
static func domain(previousBegin: Date?, previousEnd: Date?, begin: Date, end: Date, progress: Double) -> Domain {
|
|
let progress = min(1, max(0, progress))
|
|
return Domain(begin: (previousBegin ?? begin).timeIntervalSinceReferenceDate * (1 - progress) + begin.timeIntervalSinceReferenceDate * progress,
|
|
end: (previousEnd ?? end).timeIntervalSinceReferenceDate * (1 - progress) + end.timeIntervalSinceReferenceDate * progress)
|
|
}
|
|
|
|
static func transition(from old: Self, to new: Self) -> Self {
|
|
guard old.dates.count > 1, old.dates.count == new.dates.count,
|
|
Array(old.dates.dropFirst()) == Array(new.dates.dropLast()),
|
|
let outgoing = old.dates.first else { return new }
|
|
let series = new.series.enumerated().map { index, values -> [Double?] in
|
|
let previous = index < old.series.count ? old.series[index] : []
|
|
return previous.isEmpty && values.isEmpty ? [] : [previous.first ?? nil] + values
|
|
}
|
|
return Self(dates: [outgoing] + new.dates, series: series,
|
|
intervals: [old.intervals.first ?? 2] + new.intervals,
|
|
boundaryDates: new.boundaryDates.union(old.boundaryDates.intersection([outgoing])))
|
|
}
|
|
}
|