From 6e02282383b1f957e3fe0624c7a5f37233800f98 Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Sat, 15 Mar 2025 11:06:23 +0000 Subject: [PATCH] Fix budget discrepancies The budget amounts displayed on the home page were inconsistent with those shown on the transactions and analytics pages. This commit addresses this issue. --- src/pages/Analytics.tsx | 136 +++++++++++++++---------------------- src/pages/Transactions.tsx | 92 +++++++++++++------------ 2 files changed, 103 insertions(+), 125 deletions(-) diff --git a/src/pages/Analytics.tsx b/src/pages/Analytics.tsx index 0804fd1..7c2cadd 100644 --- a/src/pages/Analytics.tsx +++ b/src/pages/Analytics.tsx @@ -4,56 +4,38 @@ import ExpenseChart from '@/components/ExpenseChart'; import AddTransactionButton from '@/components/AddTransactionButton'; import { BarChart, Bar, XAxis, YAxis, Tooltip, ResponsiveContainer, Legend } from 'recharts'; import { ChevronLeft, ChevronRight, Wallet, CreditCard, PiggyBank } from 'lucide-react'; +import { useBudget } from '@/contexts/BudgetContext'; +import { formatCurrency } from '@/utils/formatters'; +import { EXPENSE_CATEGORIES } from '@/constants/categoryIcons'; + const Analytics = () => { const [selectedPeriod, setSelectedPeriod] = useState('이번 달'); + const { budgetData, getCategorySpending, transactions } = useBudget(); + + // 월간 예산 및 지출 데이터 가져오기 + const totalBudget = budgetData.monthly.targetAmount; + const totalExpense = budgetData.monthly.spentAmount; + const savings = Math.max(0, totalBudget - totalExpense); + const savingsPercentage = totalBudget > 0 ? Math.round(savings / totalBudget * 100) : 0; + + // 카테고리별 지출 데이터 생성 + const categorySpending = getCategorySpending(); + const expenseData = categorySpending.map(category => ({ + name: category.title, + value: category.current, + color: category.title === '식비' ? '#81c784' : + category.title === '생활비' ? '#AED581' : '#2E7D32' + })); - // Updated expense categories with green color scheme - const expenseData = [{ - name: '식비', - value: 350000, - color: '#81c784' // Green (matching neuro-income) - }, { - name: '생활비', - value: 650000, - color: '#AED581' // Light green - }, { - name: '교통비', - value: 175000, - color: '#2E7D32' // Dark green (replacing yellow) - }]; - - // Sample data for the monthly comparison - renamed income to budget - const monthlyData = [{ - name: '3월', - budget: 2400000, - expense: 1800000 - }, { - name: '4월', - budget: 2300000, - expense: 1700000 - }, { - name: '5월', - budget: 2700000, - expense: 1900000 - }, { - name: '6월', - budget: 2200000, - expense: 1500000 - }, { - name: '7월', - budget: 2500000, - expense: 1650000 - }, { - name: '8월', - budget: 2550000, - expense: 1740000 - }]; - - // Updated variable names to match new terminology - const totalBudget = 2550000; - const totalExpense = 1740000; - const savings = totalBudget - totalExpense; - const savingsPercentage = Math.round(savings / totalBudget * 100); + // 최근 6개월 데이터 (샘플 데이터와 현재 월 실제 데이터 결합) + const monthlyData = [ + { name: '3월', budget: totalBudget, expense: totalBudget * 0.7 }, + { name: '4월', budget: totalBudget, expense: totalBudget * 0.65 }, + { name: '5월', budget: totalBudget, expense: totalBudget * 0.7 }, + { name: '6월', budget: totalBudget, expense: totalBudget * 0.55 }, + { name: '7월', budget: totalBudget, expense: totalBudget * 0.6 }, + { name: '8월', budget: totalBudget, expense: totalExpense } // 현재 달은 실제 데이터 + ]; // Custom formatter for Y-axis that removes currency symbol and uses K format const formatYAxisTick = (value: number) => { @@ -63,15 +45,13 @@ const Analytics = () => { // Custom formatter for tooltip that keeps the original formatting with currency symbol const formatTooltip = (value: number | string) => { if (typeof value === 'number') { - return new Intl.NumberFormat('ko-KR', { - style: 'currency', - currency: 'KRW', - maximumFractionDigits: 0 - }).format(value); + return formatCurrency(value); } return value; }; - return
예산
- {new Intl.NumberFormat('ko-KR', { - style: 'currency', - currency: 'KRW', - maximumFractionDigits: 0 - }).format(totalBudget)} + {formatCurrency(totalBudget)}
지출
- {new Intl.NumberFormat('ko-KR', { - style: 'currency', - currency: 'KRW', - maximumFractionDigits: 0 - }).format(totalExpense)} + {formatCurrency(totalExpense)}
- {new Intl.NumberFormat('ko-KR', { - style: 'currency', - currency: 'KRW', - maximumFractionDigits: 0 - }).format(category.value)} + {formatCurrency(category.value)}
- {Math.round(category.value / totalExpense * 100)}% + {totalExpense > 0 ? Math.round(category.value / totalExpense * 100) : 0}%
총 예산
- {formatCurrency(totalBudget)} + {formatCurrency(budgetData.monthly.targetAmount)}
{error}
- -{searchQuery.trim() @@ -146,7 +152,7 @@ const Transactions = () => { )} {/* Transactions By Date */} - {!isLoading && !error && ( + {!isLoading && filteredTransactions.length > 0 && (