Fix compilation errors in ChartsViewModel and AllocationEvolutionChart
- Remove unnecessary optional binding for non-optional source.id - Break up AllocationEvolutionChart body to help Swift type-checker Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -294,8 +294,7 @@ class ChartsViewModel: ObservableObject {
|
||||
let sources: [InvestmentSource]
|
||||
if !selectedSourceIds.isEmpty {
|
||||
sources = sourceRepository.sources.filter { source in
|
||||
guard let id = source.id else { return false }
|
||||
return selectedSourceIds.contains(id) && shouldIncludeSource(source)
|
||||
selectedSourceIds.contains(source.id) && shouldIncludeSource(source)
|
||||
}
|
||||
} else if let selectedSource {
|
||||
sources = sourceRepository.sources.filter { $0.id == selectedSource.id && shouldIncludeSource($0) }
|
||||
|
||||
@@ -929,28 +929,60 @@ struct CashflowStackedChartView: View {
|
||||
|
||||
// MARK: - Allocation Evolution Chart
|
||||
|
||||
struct AllocationEvolutionDataPoint: Identifiable {
|
||||
let id: String
|
||||
let date: Date
|
||||
let category: String
|
||||
let percentage: Double
|
||||
let color: String
|
||||
}
|
||||
|
||||
struct AllocationEvolutionChart: View {
|
||||
let data: [(date: Date, category: String, percentage: Double, color: String)]
|
||||
|
||||
private var identifiableData: [AllocationEvolutionDataPoint] {
|
||||
data.enumerated().map { index, item in
|
||||
AllocationEvolutionDataPoint(
|
||||
id: "\(index)-\(item.category)",
|
||||
date: item.date,
|
||||
category: item.category,
|
||||
percentage: item.percentage,
|
||||
color: item.color
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 12) {
|
||||
Text("Allocation Over Time")
|
||||
.font(.headline)
|
||||
|
||||
if data.isEmpty {
|
||||
emptyView
|
||||
} else {
|
||||
chartView
|
||||
}
|
||||
}
|
||||
.padding()
|
||||
.background(Color(.systemBackground))
|
||||
.cornerRadius(AppConstants.UI.cornerRadius)
|
||||
.shadow(color: .black.opacity(0.05), radius: 8, y: 2)
|
||||
}
|
||||
|
||||
private var emptyView: some View {
|
||||
Text("Not enough data to show allocation evolution.")
|
||||
.foregroundColor(.secondary)
|
||||
.frame(height: 260)
|
||||
} else {
|
||||
Chart {
|
||||
ForEach(data, id: \.date.description + \.category) { item in
|
||||
}
|
||||
|
||||
private var chartView: some View {
|
||||
Chart(identifiableData) { item in
|
||||
AreaMark(
|
||||
x: .value("Date", item.date),
|
||||
y: .value("Percentage", item.percentage)
|
||||
)
|
||||
.foregroundStyle(by: .value("Category", item.category))
|
||||
}
|
||||
}
|
||||
.chartForegroundStyleScale(domain: categoryNames, range: categoryColors)
|
||||
.chartXAxis {
|
||||
AxisMarks(values: .stride(by: .month, count: 2)) { _ in
|
||||
@@ -970,12 +1002,6 @@ struct AllocationEvolutionChart: View {
|
||||
.frame(height: 260)
|
||||
.drawingGroup()
|
||||
}
|
||||
}
|
||||
.padding()
|
||||
.background(Color(.systemBackground))
|
||||
.cornerRadius(AppConstants.UI.cornerRadius)
|
||||
.shadow(color: .black.opacity(0.05), radius: 8, y: 2)
|
||||
}
|
||||
|
||||
private var categoryNames: [String] {
|
||||
Array(Set(data.map { $0.category })).sorted()
|
||||
|
||||
Reference in New Issue
Block a user