feat(ui): 统一实现卡片背景趋势与大数值布局
This commit is contained in:
@@ -20,7 +20,6 @@ struct PanelTrendChart: View {
|
||||
var thirdColor: Color = .purple
|
||||
var fourthColor: Color = .yellow
|
||||
var fixedScale: Double? = 1
|
||||
var scaleLabel: String?
|
||||
var secondSeriesUsesAutonomousScale = false
|
||||
var firstValueFormat: TrendValueFormat?
|
||||
var secondValueFormat: TrendValueFormat?
|
||||
@@ -31,8 +30,7 @@ struct PanelTrendChart: View {
|
||||
var expectedInterval: TimeInterval = 2
|
||||
var boundaryDates: Set<Date> = []
|
||||
var intervals: [TimeInterval] = []
|
||||
var plotHeight: CGFloat = PanelStyle.plotHeight
|
||||
var showsLegendValues = true
|
||||
var visibleSeriesCount = 1
|
||||
@State private var retainedScale: Double = 1
|
||||
@State private var smallSamples = 0
|
||||
@State private var retainedSecondScale: Double = 1
|
||||
@@ -73,16 +71,6 @@ struct PanelTrendChart: View {
|
||||
case .watts: return MetricFormat.watts(value)
|
||||
}
|
||||
}
|
||||
private func legend(_ label: LocalizedStringKey, values: [Double?], tint: Color, format: TrendValueFormat? = nil) -> some View {
|
||||
HStack(spacing: 3) {
|
||||
RoundedRectangle(cornerRadius: 1).fill(tint).frame(width: 7, height: 7)
|
||||
Text(label)
|
||||
if showsLegendValues {
|
||||
Text(verbatim: formatted(values.last ?? nil, format: format)).monospacedDigit()
|
||||
}
|
||||
}.font(PanelStyle.legend).lineLimit(1).minimumScaleFactor(0.8)
|
||||
}
|
||||
|
||||
private func tooltipRow(_ label: LocalizedStringKey, values: [Double?], tint: Color, format: TrendValueFormat? = nil, index: Int) -> some View {
|
||||
HStack(spacing: 6) {
|
||||
RoundedRectangle(cornerRadius: 2).fill(tint).frame(width: 8, height: 8)
|
||||
@@ -105,64 +93,28 @@ struct PanelTrendChart: View {
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 3) {
|
||||
HStack(spacing: 10) {
|
||||
legend(firstLabel, values: first, tint: color)
|
||||
if !second.isEmpty { legend(secondLabel, values: second, tint: secondColor, format: secondValueFormat) }
|
||||
if !third.isEmpty { legend(thirdLabel, values: third, tint: thirdColor) }
|
||||
if !fourth.isEmpty { legend(fourthLabel, values: fourth, tint: fourthColor) }
|
||||
Spacer(minLength: 0)
|
||||
}.frame(height: 14)
|
||||
HStack(spacing: 5) {
|
||||
VStack(alignment: .trailing) {
|
||||
Text(verbatim: scaleLabel ?? formatted(scale))
|
||||
Spacer(minLength: 0)
|
||||
Text(verbatim: formatted(scale / 2))
|
||||
Spacer(minLength: 0)
|
||||
Text(verbatim: "0")
|
||||
}.font(.system(size: 8)).foregroundStyle(.secondary)
|
||||
.lineLimit(1).minimumScaleFactor(0.6).frame(width: fixedScale == 1 ? 28 : 47, height: plotHeight)
|
||||
GeometryReader { proxy in
|
||||
ZStack(alignment: .topLeading) {
|
||||
NativePanelCurve(frame: inputFrame, colors: [color, secondColor, thirdColor, fourthColor],
|
||||
scales: [scale, hasSecondaryScale ? secondaryScale : scale, hasSecondaryScale ? secondaryScale : scale, scale],
|
||||
maximumGap: max(5, expectedInterval * 2), hoveredIndex: hoveredDate == nil ? nil : index)
|
||||
}
|
||||
.contentShape(Rectangle())
|
||||
.onContinuousHover { phase in
|
||||
switch phase {
|
||||
case .active(let point):
|
||||
if let begin = dates.first, let end = dates.last {
|
||||
hoverPoint = point
|
||||
hoveredDate = begin.addingTimeInterval(end.timeIntervalSince(begin) * min(1, max(0, point.x / max(1, proxy.size.width))))
|
||||
}
|
||||
case .ended: hoveredDate = nil; hoverPoint = nil
|
||||
GeometryReader { proxy in
|
||||
NativePanelCurve(frame: inputFrame, colors: [color, secondColor, thirdColor, fourthColor],
|
||||
scales: [scale, hasSecondaryScale ? secondaryScale : scale, scale, scale],
|
||||
maximumGap: max(5, expectedInterval * 2), hoveredIndex: hoveredDate == nil ? nil : index,
|
||||
visibleSeriesCount: visibleSeriesCount, filledSeriesCount: visibleSeriesCount)
|
||||
.contentShape(Rectangle())
|
||||
.onContinuousHover { phase in
|
||||
switch phase {
|
||||
case .active(let point):
|
||||
if let begin = dates.first, let end = dates.last {
|
||||
hoverPoint = point
|
||||
hoveredDate = begin.addingTimeInterval(end.timeIntervalSince(begin) * min(1, max(0, point.x / max(1, proxy.size.width))))
|
||||
}
|
||||
case .ended: hoveredDate = nil; hoverPoint = nil
|
||||
}
|
||||
.clipped()
|
||||
}.frame(height: plotHeight)
|
||||
}
|
||||
.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)))
|
||||
}
|
||||
if hasSecondaryScale {
|
||||
VStack(alignment: .leading) {
|
||||
Text(verbatim: formatted(secondaryScale, format: .watts))
|
||||
Spacer(minLength: 0)
|
||||
Text(verbatim: formatted(secondaryScale / 2, format: .watts))
|
||||
Spacer(minLength: 0)
|
||||
Text(verbatim: "0 W")
|
||||
}.font(.system(size: 8)).foregroundStyle(.secondary)
|
||||
.lineLimit(1).minimumScaleFactor(0.6).frame(width: 45, height: plotHeight)
|
||||
}
|
||||
}
|
||||
HStack {
|
||||
if let date = dates.first { Text(date, style: .time) }
|
||||
Spacer(minLength: 0)
|
||||
if let date = dates.last { Text(date, style: .time) }
|
||||
}.font(PanelStyle.legend).foregroundStyle(.secondary).frame(height: 12)
|
||||
}
|
||||
.frame(maxWidth: .infinity)
|
||||
.frame(height: ResourceCardMetrics.plotHeight)
|
||||
.onAppear {
|
||||
retainedScale = maximum
|
||||
retainedSecondScale = secondMaximum
|
||||
@@ -181,6 +133,8 @@ struct PanelTrendChart: View {
|
||||
if smallSecondSamples >= 10 { retainedSecondScale = secondTarget; smallSecondSamples = 0 }
|
||||
} else { smallSecondSamples = 0 }
|
||||
}
|
||||
.accessibilityElement(children: .combine)
|
||||
.accessibilityElement(children: .ignore)
|
||||
.accessibilityLabel(Text(firstLabel))
|
||||
.accessibilityValue(Text(verbatim: formatted(first.last ?? nil)))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user