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
+36
View File
@@ -0,0 +1,36 @@
import SwiftUI
/// Charts export their plot anchor; the owning card draws the tooltip above all content.
struct ChartTooltipPresentation {
let plot: Anchor<CGRect>
let point: CGPoint
let content: AnyView
}
struct ChartTooltipPreference: PreferenceKey {
static var defaultValue: ChartTooltipPresentation? { nil }
static func reduce(value: inout ChartTooltipPresentation?, nextValue: () -> ChartTooltipPresentation?) {
if let next = nextValue() { value = next }
}
}
/// Measures the actual localized tooltip, then keeps it inside the card.
struct ChartTooltipLayer: Layout {
let point: CGPoint
func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {
proposal.replacingUnspecifiedDimensions()
}
func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
guard let tooltip = subviews.first else { return }
let width = min(180, max(1, bounds.width - 20))
let tooltipProposal = ProposedViewSize(width: width, height: nil)
let size = tooltip.sizeThatFits(tooltipProposal)
let x = min(max(10, point.x + 12), max(10, bounds.width - size.width - 10))
let below = point.y + 10
let preferredY = below + size.height <= bounds.height - 10 ? below : point.y - size.height - 10
let y = min(max(10, preferredY), max(10, bounds.height - size.height - 10))
tooltip.place(at: CGPoint(x: bounds.minX + x, y: bounds.minY + y), anchor: .topLeading, proposal: tooltipProposal)
}
}