fix(charts): 修复浮窗层级并平滑切频与曲线移出

This commit is contained in:
2026-11-03 12:07:01 +08:00
parent 36b8e32af3
commit 7ef92d9c21
17 changed files with 271 additions and 59 deletions
+43 -32
View File
@@ -16,6 +16,7 @@ struct PanelTrendChart: View {
var thirdLabel: LocalizedStringKey = "cpu.system"
var expectedInterval: TimeInterval = 2
var boundaryDates: Set<Date> = []
var intervals: [TimeInterval] = []
@Environment(\.accessibilityReduceMotion) private var reduceMotion
@State private var retainedScale: Double = 1
@State private var smallSamples = 0
@@ -24,7 +25,14 @@ struct PanelTrendChart: View {
@State private var previousBegin: Date?
@State private var previousEnd: Date?
@State private var progress: CGFloat = 1
@State private var lastFrame: PanelCurveFrame?
@State private var displayedFrame: PanelCurveFrame?
private var inputFrame: PanelCurveFrame {
PanelCurveFrame(dates: dates, series: [first, second, third],
intervals: intervals.count == dates.count ? intervals : Array(repeating: expectedInterval, count: dates.count),
boundaryDates: boundaryDates)
}
private var maximum: Double { max(1, (first + second + third).compactMap { $0 }.filter(\.isFinite).max() ?? 1) }
private var scale: Double { max(0.0001, fixedScale ?? retainedScale) }
private var index: Int? {
@@ -62,7 +70,7 @@ struct PanelTrendChart: View {
if !third.isEmpty { tooltipRow(thirdLabel, values: third, tint: thirdColor, index: index) }
}
.font(.system(size: 10)).padding(8)
.background(Color("PanelTooltip"), in: .rect(cornerRadius: 7))
.background { TooltipMaterialBackground() }
.overlay(RoundedRectangle(cornerRadius: 7).strokeBorder(.primary.opacity(0.2)))
.shadow(color: .black.opacity(0.2), radius: 5, y: 2)
}
@@ -86,8 +94,10 @@ struct PanelTrendChart: View {
.lineLimit(1).minimumScaleFactor(0.6).frame(width: fixedScale == 1 ? 28 : 47, height: 58)
GeometryReader { proxy in
ZStack(alignment: .topLeading) {
PanelCurveCanvas(dates: dates, series: [first, second, third], colors: [color, secondColor, thirdColor],
scale: scale, maximumGap: max(5, expectedInterval * 2), boundaryDates: boundaryDates, previousBegin: previousBegin, previousEnd: previousEnd, progress: progress)
PanelCurveCanvas(frame: displayedFrame ?? inputFrame, colors: [color, secondColor, thirdColor],
scale: scale, maximumGap: max(5, expectedInterval * 2),
targetBegin: (lastFrame ?? inputFrame).dates.first, targetEnd: (lastFrame ?? inputFrame).dates.last,
previousBegin: previousBegin, previousEnd: previousEnd, progress: progress)
if hoveredDate != nil, let index, let begin = dates.first, let end = dates.last, end > begin {
let x = proxy.size.width * dates[index].timeIntervalSince(begin) / end.timeIntervalSince(begin)
Path { path in
@@ -114,17 +124,11 @@ struct PanelTrendChart: View {
}
}
.clipped()
.overlay(alignment: .topLeading) {
if let hoverPoint, let index {
tooltip(index: index)
.frame(width: min(180, proxy.size.width))
.offset(x: min(max(0, hoverPoint.x + 12), max(0, proxy.size.width - 180)),
y: min(hoverPoint.y + 10, proxy.size.height - 12))
.allowsHitTesting(false)
.accessibilityHidden(true)
}
}
}.frame(height: 58).zIndex(10)
}.frame(height: 58)
.anchorPreference(key: ChartTooltipPreference.self, value: .bounds) { plot in
guard let hoverPoint, let index else { return nil }
return ChartTooltipPresentation(plot: plot, point: hoverPoint, content: AnyView(tooltip(index: index)))
}
}
HStack {
if let date = dates.first { Text(date, style: .time) }
@@ -133,40 +137,47 @@ struct PanelTrendChart: View {
}.font(.system(size: 9)).foregroundStyle(.secondary).frame(height: 12)
}
.frame(maxWidth: .infinity)
.zIndex(hoveredDate == nil ? 0 : 20)
.onAppear { retainedScale = maximum }
.onChange(of: dates) { old, new in
.onAppear { retainedScale = maximum; lastFrame = inputFrame; displayedFrame = inputFrame }
.onChange(of: dates) { _, _ 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 }
let append = PanelCurveGeometry.isAppend(from: old, to: new, maximumGap: max(5, expectedInterval * 2)) && !(new.last.map { boundaryDates.contains($0) } ?? false)
let next = inputFrame
let old = lastFrame
let gap = PanelCurveGeometry.allowedGap(at: next.dates.count - 1, intervals: next.intervals, fallback: max(5, expectedInterval * 2))
let append = old.map { PanelCurveGeometry.isAppend(from: $0.dates, to: next.dates, maximumGap: gap) } == true
&& !(next.dates.last.map { boundaryDates.contains($0) } ?? false)
let animated = append && !reduceMotion
var transaction = Transaction(); transaction.disablesAnimations = true
withTransaction(transaction) {
previousBegin = append && !reduceMotion ? old.first : nil
previousEnd = append && !reduceMotion ? old.last : nil
progress = previousBegin == nil ? 1 : 0
lastFrame = next
displayedFrame = next
if animated, let old { displayedFrame = PanelCurveFrame.transition(from: old, to: next) }
previousBegin = animated ? old?.dates.first : nil
previousEnd = animated ? old?.dates.last : nil
progress = animated ? 0 : 1
}
}
.task(id: dates.last) {
.task(id: lastFrame?.dates.last) {
guard progress == 0, !reduceMotion else { return }
do { try await Task.sleep(for: .milliseconds(16)) } catch { return }
withAnimation(.easeInOut(duration: 0.35)) { progress = 1 }
}
.onChange(of: reduceMotion) { _, reduced in if reduced { progress = 1; previousBegin = nil; previousEnd = nil } }
.onChange(of: reduceMotion) { _, reduced in if reduced { progress = 1; previousBegin = nil; previousEnd = nil; lastFrame = inputFrame; displayedFrame = inputFrame } }
.accessibilityElement(children: .combine)
}
}
private struct PanelCurveCanvas: View, Animatable {
let dates: [Date]
let series: [[Double?]]
let frame: PanelCurveFrame
let colors: [Color]
let scale: Double
let maximumGap: TimeInterval
let boundaryDates: Set<Date>
let targetBegin: Date?
let targetEnd: Date?
let previousBegin: Date?
let previousEnd: Date?
var progress: CGFloat
@@ -187,14 +198,14 @@ private struct PanelCurveCanvas: View, Animatable {
grid.move(to: CGPoint(x: 0, y: y)); grid.addLine(to: CGPoint(x: size.width, y: y))
}
context.stroke(grid, with: .color(.primary.opacity(0.10)), lineWidth: 0.5)
guard let first = dates.first, let last = dates.last else { return }
let begin = (previousBegin ?? first).timeIntervalSinceReferenceDate * (1 - progress) + first.timeIntervalSinceReferenceDate * progress
let end = (previousEnd ?? last).timeIntervalSinceReferenceDate * (1 - progress) + last.timeIntervalSinceReferenceDate * progress
for (seriesIndex, entry) in zip(series, colors).enumerated() {
guard let first = targetBegin, let last = targetEnd else { return }
let domain = PanelCurveFrame.domain(previousBegin: previousBegin, previousEnd: previousEnd,
begin: first, end: last, progress: Double(progress))
for (seriesIndex, entry) in zip(frame.series, colors).enumerated() {
let (values, tint) = entry
for run in PanelCurveGeometry.runs(dates: dates, values: values, maximumGap: maximumGap, boundaryDates: boundaryDates) {
for run in PanelCurveGeometry.runs(dates: frame.dates, values: values, maximumGap: maximumGap, boundaryDates: frame.boundaryDates, intervals: frame.intervals) {
let points = run.map { item in
CGPoint(x: end > begin ? size.width * (item.date.timeIntervalSinceReferenceDate - begin) / (end - begin) : size.width,
CGPoint(x: size.width * domain.position(item.date),
y: size.height * (1 - min(1, max(0, item.value / scale))))
}
guard let start = points.first, let finish = points.last else { continue }