Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Income vs Expenses in one chart #62

Merged
merged 1 commit into from
Sep 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 39 additions & 34 deletions MMEX/Views/InsightsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,42 +35,10 @@ struct InsightsView: View {
.shadow(radius: 2)
}

// Transactions Income Over Time Chart Section
Section {
Chart(viewModel.stats) {
BarMark(
x: .value("Day", $0.day),
y: .value("Amount", $0.income)
)
.foregroundStyle(by: .value("Status", $0.status.fullName))
}
.frame(height: 200)
.padding()
.background(Color(.systemGray6))
.cornerRadius(10)
.shadow(radius: 2)
} header: {
Text("Income Over Time")
.font(.headline)
.padding(.horizontal)
}

// Transactions expenses Over Time Chart Section
Section {
Chart(viewModel.stats) {
BarMark(
x: .value("Day", $0.day),
y: .value("Amount", $0.expenses)
)
.foregroundStyle(by: .value("Status", $0.status.fullName))
}
.frame(height: 200)
.padding()
.background(Color(.systemGray6))
.cornerRadius(10)
.shadow(radius: 2)
incomeVSexpense
} header: {
Text("Expenses Over Time")
Text("Income vs Expense Over Time")
.font(.headline)
.padding(.horizontal)
}
Expand Down Expand Up @@ -100,6 +68,43 @@ struct InsightsView: View {
.navigationBarTitleDisplayMode(.inline) // Ensure title is inline to reduce top space
}
}

private var incomeVSexpense: some View {
Chart() {
ForEach(viewModel.stats) { stat in
Plot {
BarMark(
x: .value("Day", stat.day),
y: .value("Amount", stat.income)
)
// .foregroundStyle(by: .value("Status", $0.status.fullName))
.foregroundStyle(.green)
}
.accessibilityLabel("income")
.accessibilityValue("\(stat.income)")
}

ForEach(viewModel.stats) { stat in
Plot {
BarMark(
x: .value("Day", stat.day),
y: .value("Amount", 0 - stat.expenses)
)
// .foregroundStyle(by: .value("Status", $0.status.fullName))
.foregroundStyle(.purple)
}
.accessibilityLabel("expenses")
.accessibilityValue("\(stat.expenses)")
}
}
.chartYAxis {
AxisMarks(preset: .automatic, position: .leading)
}
.frame(height: 300)
.chartYAxis(.automatic)
.chartXAxis(.automatic)
.padding(.horizontal)
}
}

#Preview {
Expand Down