Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 8 additions & 1 deletion backend/controllers/transactionController.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ const getChartData = async (req, res) => {
{ $project: { name: '$_id', total: 1, _id: 0 } }
]);

// Data for Income by Category (Pie Chart)
const incomeByCategory = await IncomeExpense.aggregate([
{ $match: { user: userId, isIncome: true, isDeleted: false } },
{ $group: { _id: '$category', total: { $sum: '$cost' } } },
{ $project: { name: '$_id', total: 1, _id: 0 } }
]);

// Data for Expenses Over Time (Bar Chart)
const expensesOverTime = await IncomeExpense.aggregate([
{ $match: { user: userId, isIncome: false, isDeleted: false, addedOn: { $gte: thirtyDaysAgo } } },
Expand All @@ -234,7 +241,7 @@ const getChartData = async (req, res) => {
{ $project: { date: '$_id', total: 1, _id: 0 } }
]);

res.json({ expensesByCategory, expensesOverTime, incomeOverTime });
res.json({ expensesByCategory, incomeByCategory, expensesOverTime, incomeOverTime });
} catch (error) {
// Also log the error to the backend console for easier debugging
console.error('Error in getChartData:', error);
Expand Down
Loading