From ecb7851a62787a522c142469c8f97cc9dc6b813c Mon Sep 17 00:00:00 2001 From: AJ Date: Sun, 12 Oct 2025 02:13:57 -0400 Subject: [PATCH] Add percentage of total to top expense categories on dashboard MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Show what percentage of total expenses each category represents in the top categories table. This helps quickly identify which categories consume the largest portion of spending. Changes: - Add PercentageOfTotal property to TopCategoryRow - Calculate percentage based on total expenses in the period - Display percentage as a badge in the table (e.g., "23.5%") - Add new "% of Total" column header - Format to 1 decimal place for clarity Example: If "Groceries" is $500 and total expenses are $2000, it shows 25.0% 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- MoneyMap/Pages/Index.cshtml | 4 ++++ MoneyMap/Pages/Index.cshtml.cs | 19 +++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/MoneyMap/Pages/Index.cshtml b/MoneyMap/Pages/Index.cshtml index f25cac3..dd38a2c 100644 --- a/MoneyMap/Pages/Index.cshtml +++ b/MoneyMap/Pages/Index.cshtml @@ -71,6 +71,7 @@ Category Total Spend + % of Total Txns @@ -84,6 +85,9 @@ @c.TotalSpend.ToString("C") + + @c.PercentageOfTotal.ToString("F1")% + @c.Count } diff --git a/MoneyMap/Pages/Index.cshtml.cs b/MoneyMap/Pages/Index.cshtml.cs index ceab630..b124efb 100644 --- a/MoneyMap/Pages/Index.cshtml.cs +++ b/MoneyMap/Pages/Index.cshtml.cs @@ -44,6 +44,7 @@ namespace MoneyMap.Pages public string Category { get; set; } = ""; public decimal TotalSpend { get; set; } public int Count { get; set; } + public decimal PercentageOfTotal { get; set; } } public class RecentTxnRow @@ -176,20 +177,30 @@ namespace MoneyMap.Pages { var since = DateTime.UtcNow.Date.AddDays(-lastDays); - return await _db.Transactions + // Get all expense transactions for the period + var expenseTransactions = await _db.Transactions .Where(t => t.Date >= since && t.Amount < 0) .ExcludeTransfers() // Exclude credit card payments and transfers + .ToListAsync(); + + // Calculate total spend + var totalSpend = expenseTransactions.Sum(t => -t.Amount); + + // Group by category and calculate percentages + var topCategories = expenseTransactions .GroupBy(t => t.Category ?? "") .Select(g => new IndexModel.TopCategoryRow { Category = g.Key, TotalSpend = g.Sum(x => -x.Amount), - Count = g.Count() + Count = g.Count(), + PercentageOfTotal = totalSpend > 0 ? (g.Sum(x => -x.Amount) / totalSpend) * 100 : 0 }) .OrderByDescending(x => x.TotalSpend) .Take(count) - .AsNoTracking() - .ToListAsync(); + .ToList(); + + return topCategories; } }