feat(ui): 实现全卡堆叠趋势与单页资源进程布局
This commit is contained in:
@@ -10,6 +10,7 @@ struct NativePanelCurve: NSViewRepresentable {
|
||||
var hoveredIndex: Int? = nil
|
||||
var visibleSeriesCount: Int? = nil
|
||||
var filledSeriesCount = 1
|
||||
var stacked = false
|
||||
|
||||
func sizeThatFits(_ proposal: ProposedViewSize, nsView: PlotView, context: Context) -> CGSize? {
|
||||
CGSize(width: proposal.width.flatMap { $0.isFinite ? $0 : nil } ?? 10,
|
||||
@@ -18,7 +19,7 @@ struct NativePanelCurve: NSViewRepresentable {
|
||||
func makeNSView(context: Context) -> PlotView { PlotView() }
|
||||
func updateNSView(_ view: PlotView, context: Context) {
|
||||
view.configure(Input(frame: frame, colors: colors, scales: scales, maximumGap: maximumGap,
|
||||
hoveredIndex: hoveredIndex, visibleSeriesCount: visibleSeriesCount, filledSeriesCount: filledSeriesCount))
|
||||
hoveredIndex: hoveredIndex, visibleSeriesCount: visibleSeriesCount, filledSeriesCount: filledSeriesCount, stacked: stacked))
|
||||
}
|
||||
struct Input: Equatable {
|
||||
let frame: PanelCurveFrame
|
||||
@@ -28,6 +29,7 @@ struct NativePanelCurve: NSViewRepresentable {
|
||||
var hoveredIndex: Int? = nil
|
||||
var visibleSeriesCount: Int? = nil
|
||||
var filledSeriesCount = 1
|
||||
var stacked = false
|
||||
}
|
||||
final class PlotView: NSView {
|
||||
private(set) var input: Input?
|
||||
@@ -38,6 +40,7 @@ struct NativePanelCurve: NSViewRepresentable {
|
||||
private var hoverDots: [CAShapeLayer] = []
|
||||
private var seriesLayers: [SeriesLayer] = []
|
||||
private var palette: [NSColor] = []
|
||||
private var plottedBands: [StackedSeries.Band] = []
|
||||
override var isFlipped: Bool { true }
|
||||
override func hitTest(_ point: NSPoint) -> NSView? { nil }
|
||||
|
||||
@@ -67,7 +70,7 @@ struct NativePanelCurve: NSViewRepresentable {
|
||||
for index in seriesLayers.indices {
|
||||
seriesLayers[index].root.isHidden = index >= (value.visibleSeriesCount ?? value.frame.series.count)
|
||||
}
|
||||
if frameChanged || old?.scales != value.scales || old?.maximumGap != value.maximumGap {
|
||||
if frameChanged || old?.scales != value.scales || old?.maximumGap != value.maximumGap || old?.stacked != value.stacked {
|
||||
rebuildPaths()
|
||||
}
|
||||
updateHover()
|
||||
@@ -122,13 +125,16 @@ struct NativePanelCurve: NSViewRepresentable {
|
||||
}
|
||||
let domain = PanelCurveFrame.domain(begin: first, end: last)
|
||||
let frame = input.frame
|
||||
plottedBands = input.stacked ? StackedSeries.bands(frame.series, count: frame.dates.count, limit: input.scales.first) : []
|
||||
for index in frame.series.indices {
|
||||
let series = seriesLayers[index]
|
||||
series.resize(bounds)
|
||||
let value = input.scales[min(index, input.scales.count - 1)]
|
||||
let value = input.scales[input.stacked ? 0 : min(index, input.scales.count - 1)]
|
||||
let scale = value.isFinite && value > 0 ? value : 1
|
||||
let runs = PanelCurveGeometry.runs(dates: frame.dates, values: frame.series[index], maximumGap: input.maximumGap,
|
||||
boundaryDates: frame.boundaryDates, intervals: frame.intervals)
|
||||
let upper = input.stacked ? plottedBands[index].upper : frame.series[index]
|
||||
let lower = input.stacked ? plottedBands[index].lower : nil
|
||||
let runs = PanelCurveGeometry.runs(dates: frame.dates, values: upper, maximumGap: input.maximumGap,
|
||||
boundaryDates: frame.boundaryDates, intervals: frame.intervals, lowerValues: lower)
|
||||
let line = CGMutablePath(), dots = CGMutablePath(), area = CGMutablePath()
|
||||
for run in runs {
|
||||
let points = run.map { CGPoint(x: bounds.width * domain.position($0.date),
|
||||
@@ -143,11 +149,16 @@ struct NativePanelCurve: NSViewRepresentable {
|
||||
segment.addCurve(to: b, control1: CGPoint(x: mid, y: a.y), control2: CGPoint(x: mid, y: b.y))
|
||||
}
|
||||
line.addPath(segment)
|
||||
// Each continuous run closes against its own baseline, preserving real gaps.
|
||||
// Close against the matching lower boundary, never against another run or missing component.
|
||||
let lowerPoints = run.reversed().map { CGPoint(x: bounds.width * domain.position($0.date),
|
||||
y: bounds.height * (1 - min(1, max(0, $0.lower / scale)))) }
|
||||
let fill = CGMutablePath(); fill.addPath(segment)
|
||||
fill.addLine(to: CGPoint(x: points.last!.x, y: bounds.height))
|
||||
fill.addLine(to: CGPoint(x: start.x, y: bounds.height)); fill.closeSubpath()
|
||||
area.addPath(fill)
|
||||
if let lowerStart = lowerPoints.first { fill.addLine(to: lowerStart) }
|
||||
for (a, b) in zip(lowerPoints, lowerPoints.dropFirst()) {
|
||||
let mid = (a.x + b.x) / 2
|
||||
fill.addCurve(to: b, control1: CGPoint(x: mid, y: a.y), control2: CGPoint(x: mid, y: b.y))
|
||||
}
|
||||
fill.closeSubpath(); area.addPath(fill)
|
||||
}
|
||||
series.line.path = line
|
||||
series.points.path = dots
|
||||
@@ -164,9 +175,9 @@ struct NativePanelCurve: NSViewRepresentable {
|
||||
let path = CGMutablePath(); path.move(to: CGPoint(x: x, y: 0)); path.addLine(to: CGPoint(x: x, y: bounds.height)); hoverLine.path = path
|
||||
for series in input.frame.series.indices {
|
||||
guard series < (input.visibleSeriesCount ?? input.frame.series.count) else { continue }
|
||||
let values = input.frame.series[series]
|
||||
let values = input.stacked && plottedBands.indices.contains(series) ? plottedBands[series].upper : input.frame.series[series]
|
||||
guard values.indices.contains(index), let value = values[index], value.isFinite else { continue }
|
||||
let scale = input.scales[min(series, input.scales.count - 1)]
|
||||
let scale = input.scales[input.stacked ? 0 : min(series, input.scales.count - 1)]
|
||||
let y = bounds.height * (1 - min(1, max(0, value / (scale.isFinite && scale > 0 ? scale : 1))))
|
||||
hoverDots[series].path = CGPath(ellipseIn: CGRect(x: x - 2.5, y: y - 2.5, width: 5, height: 5), transform: nil)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user