Charts: puntos + labels de valor en todas las gráficas de líneas

- Nuevo ChartValueLabels.swift: ChartValueBubble (cápsula) y ChartSelectionCard
  (tarjeta multi-serie), con umbral compartido (<=12 puntos → labels SIEMPRE
  visibles; las gráficas cortas aportan mucho más con los valores a la vista)
- PointMark añadido donde faltaba (Compare, Year vs Year, Drawdown, Volatility);
  agrandado donde ya existía (Evolution, Prediction, Period vs Period, Rolling)
- Selección por tap/arrastre (chartXSelection) en las 8 gráficas: RuleMark + burbuja
  prominente (una serie) o tarjeta con el valor de cada serie (multi-serie)
- Evolution reutiliza su scrubbing existente y ahora muestra burbuja en el punto
- Period vs Period: labels A arriba / B abajo para evitar solapes
- Drawdown/Volatility migrados de Chart(data:) a Chart{ForEach} para poder añadir
  marcas de selección

Verificado en iPad Pro 13" (labels visibles en Evolution y Period vs Period).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015qUZrBusG82T37R7PeokqJ
This commit is contained in:
alexandrev-tibco
2026-07-03 23:09:35 +02:00
parent 7c92ea36c0
commit f7d4fdbe2d
7 changed files with 409 additions and 42 deletions
@@ -4,6 +4,22 @@ import Charts
struct PredictionChartView: View {
let predictions: [Prediction]
let historicalData: [(date: Date, value: Decimal)]
@State private var selectedDate: Date?
private var showAllLabels: Bool {
historicalData.count + predictions.count <= ChartLabels.alwaysShowThreshold
}
/// Nearest point (historical or predicted) to the selected date.
private var selectedPoint: (date: Date, value: Decimal, isPrediction: Bool)? {
guard let selectedDate else { return nil }
let all: [(date: Date, value: Decimal, isPrediction: Bool)] =
historicalData.map { ($0.date, $0.value, false) } +
predictions.map { ($0.date, $0.predictedValue, true) }
return all.min(by: {
abs($0.date.timeIntervalSince(selectedDate)) < abs($1.date.timeIntervalSince(selectedDate))
})
}
var body: some View {
VStack(alignment: .leading, spacing: 16) {
@@ -47,7 +63,12 @@ struct PredictionChartView: View {
y: .value("Value", NSDecimalNumber(decimal: item.value).doubleValue)
)
.foregroundStyle(Color.appPrimary)
.symbolSize(24)
.symbolSize(showAllLabels ? 36 : 24)
.annotation(position: .top, spacing: 3) {
if showAllLabels {
ChartValueBubble(text: item.value.compactCurrencyString)
}
}
}
// Confidence interval area
@@ -74,7 +95,12 @@ struct PredictionChartView: View {
y: .value("Predicted", NSDecimalNumber(decimal: prediction.predictedValue).doubleValue)
)
.foregroundStyle(Color.appSecondary)
.symbolSize(24)
.symbolSize(showAllLabels ? 36 : 24)
.annotation(position: .top, spacing: 3) {
if showAllLabels {
ChartValueBubble(text: prediction.predictedValue.compactCurrencyString, color: .appSecondary)
}
}
}
// Bull scenario line (upper confidence interval)
@@ -118,7 +144,27 @@ struct PredictionChartView: View {
.foregroundStyle(Color.appSecondary)
.lineStyle(StrokeStyle(lineWidth: 2, dash: [5, 5]))
}
if let sel = selectedPoint {
RuleMark(x: .value("Selected", sel.date))
.foregroundStyle(Color.secondary.opacity(0.35))
.lineStyle(StrokeStyle(lineWidth: 1, dash: [3, 3]))
PointMark(
x: .value("Selected", sel.date),
y: .value("Value", NSDecimalNumber(decimal: sel.value).doubleValue)
)
.foregroundStyle(sel.isPrediction ? Color.appSecondary : Color.appPrimary)
.symbolSize(80)
.annotation(position: .top, spacing: 4) {
ChartValueBubble(
text: sel.value.compactCurrencyString,
color: sel.isPrediction ? .appSecondary : .appPrimary,
prominent: true
)
}
}
}
.chartXSelection(value: $selectedDate)
.chartXAxis {
AxisMarks(values: .stride(by: .month, count: 3)) { value in
AxisValueLabel(format: .dateTime.month(.abbreviated).year(.twoDigits))