From b587f8aeb6ce6535e5a9027701b6cc196a8d40fa Mon Sep 17 00:00:00 2001 From: Vishesh 'ironeagle' Bangotra Date: Tue, 7 Apr 2026 11:06:24 +0530 Subject: [PATCH] compare shows +/- instead of full amounts for easier consumption --- src/components/HistoryChart.tsx | 162 ++++++++++++++------------------ 1 file changed, 69 insertions(+), 93 deletions(-) diff --git a/src/components/HistoryChart.tsx b/src/components/HistoryChart.tsx index 403e20c..99553bb 100644 --- a/src/components/HistoryChart.tsx +++ b/src/components/HistoryChart.tsx @@ -5,8 +5,6 @@ export interface ChartDataPoint { id: string; amount: number; compareAmount?: number; - count?: number; - compareLabel?: string; highlighted?: boolean; } @@ -32,12 +30,46 @@ export interface HistoryChartProps { setComparison: (mode: boolean) => void; } -export interface AggregatedDashboardData { - chartData: ChartData; - totalAmount: number; - topPayees: Array<{ payeeName: string; amount: number }>; -} +const formatDisplay = (point: ChartDataPoint, tab: string) => { + const base = point.amount; + const cmp = point.compareAmount || 0; + const diff = base - cmp; + const sign = diff >= 0 ? "+" : "-"; + const absDiff = Math.abs(diff); + + const formatShort = (val: number) => { + if (tab === "monthly") { + if (val >= 100000) return `${(val / 100000).toFixed(2)}L`; + } + if (tab === "weekly") { + if (val >= 1000) return `${(val / 1000).toFixed(1)}K`; + } + return val.toLocaleString("en-IN"); + }; + + if (!cmp) return `₹ ${formatShort(base)}`; + + return `₹ ${formatShort(base)} (${sign}${formatShort(absDiff)})`; +}; + +const formatLabel = (label: string, type: string) => { + if (type === "monthly") return label; + + if (type === "weekly") { + const parts = label.split(" - "); + if (parts.length === 2) { + const [start, end] = parts; + const startDay = start.split(" ")[0]; + const endParts = end.split(" "); + const endDay = endParts[0]; + const month = endParts[1]; + return `${startDay}–${endDay} ${month}`; + } + } + + return label; +}; export default function HistoryChart({ header, @@ -52,9 +84,7 @@ export default function HistoryChart({ const [activeTab, setActiveTab] = React.useState(tabs[0] || ""); const handleTabChange = (_: React.MouseEvent, newTab: string | null) => { - if (newTab !== null) { - setActiveTab(newTab); - } + if (newTab !== null) setActiveTab(newTab); }; const activeDataKey = activeTab.toLowerCase() as keyof ChartData; @@ -80,24 +110,6 @@ export default function HistoryChart({ ) : 1; - const formatAmount = (amount: number) => { - const tab = activeTab.toLowerCase(); - - if (amount === 0) return ""; - - if (tab === "monthly") { - if (amount >= 100000) return `₹ ${(amount / 100000).toFixed(2)} L`; - return `₹ ${amount.toLocaleString("en-IN")}`; - } - - if (tab === "weekly") { - if (amount >= 1000) return `₹ ${(amount / 1000).toFixed(1)} K`; - return `₹ ${amount.toLocaleString("en-IN")}`; - } - - return `₹ ${amount.toLocaleString("en-IN")}`; - }; - return ( )} - {/* Tabs */} - theme.palette.mode === "dark" - ? "rgba(255,255,255,0.05)" - : "rgba(0,0,0,0.02)", - borderRadius: 8, - p: 0.5, - "& .MuiToggleButton-root": { - border: "none", - borderRadius: 8, - textTransform: "capitalize", - fontWeight: 600, - color: "text.secondary", - "&.Mui-selected": { - bgcolor: (theme) => - theme.palette.mode === "dark" - ? "primary.dark" - : "primary.light", - color: (theme) => - theme.palette.mode === "dark" - ? "primary.contrastText" - : "primary.main" - } - } - }} + sx={{ mb: 4 }} > {tabs.map((tab) => ( @@ -159,7 +145,6 @@ export default function HistoryChart({ ))} - {/* Period Toggle */} Compare - {/* Chart */} {currentData.length > 0 ? ( {currentData.map((point) => { @@ -203,29 +187,15 @@ export default function HistoryChart({ height: "100%" }} > - {/* Values */} - - {comparison && point.compareAmount ? ( - - {formatAmount(point.compareAmount)} - - ) : null} - - - {formatAmount(point.amount)} - - - - {/* Bars */} - {/* Compare */} {comparison && ( )} - {/* Current */} - - - {/* Label */} - - {comparison && point.compareLabel && ( + > - {point.compareLabel} + {formatDisplay(point, activeTab.toLowerCase())} - )} - - - {point.id} - + + + + {formatLabel(point.id, activeDataKey)} + ); })}