forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpendingChart.tsx
More file actions
159 lines (145 loc) · 4.78 KB
/
Copy pathSpendingChart.tsx
File metadata and controls
159 lines (145 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { useMemo } from "react";
import {
AreaChart,
Area,
XAxis,
YAxis,
CartesianGrid,
Tooltip,
ResponsiveContainer,
} from "recharts";
import type { SpendingTrend } from "../../types/analytics";
import { useTheme } from "../ThemeContext";
interface SpendingChartProps {
data: SpendingTrend[];
onPeriodSelect?: (period: string) => void;
}
function formatMonth(dateStr: string): string {
const d = new Date(dateStr);
return d.toLocaleDateString("en-US", { month: "short", year: "2-digit" });
}
function formatCurrency(value: number): string {
return `$${value.toLocaleString("en-US", { minimumFractionDigits: 0, maximumFractionDigits: 0 })}`;
}
function cssVar(name: string): string {
return getComputedStyle(document.documentElement)
.getPropertyValue(name)
.trim();
}
/** Re-reads CSS custom properties from :root whenever the resolved theme changes. */
function useThemeColors() {
const { resolvedTheme } = useTheme();
return useMemo(
() => ({
accentColor: cssVar("--color-accent"),
borderColor: cssVar("--color-border"),
mutedColor: cssVar("--color-text-muted"),
surfaceColor: cssVar("--color-surface"),
textColor: cssVar("--color-text"),
}),
// eslint-disable-next-line react-hooks/exhaustive-deps
[resolvedTheme],
);
}
export function SpendingChart({ data, onPeriodSelect }: SpendingChartProps) {
const { resolvedTheme } = useTheme();
const { accentColor, borderColor, mutedColor, surfaceColor, textColor } =
useThemeColors();
const chartData = data.map((d) => ({
...d,
label: formatMonth(d.period),
}));
return (
<div
className="bg-card-theme rounded-lg shadow border border-theme p-6"
id="spending-chart"
>
<h2 className="text-xl font-bold text-theme mb-4">Spending Trends</h2>
<ResponsiveContainer width="100%" height={300}>
<AreaChart
data={chartData}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
onClick={(e: any) => {
if (e?.activePayload?.[0] && onPeriodSelect) {
onPeriodSelect(e.activePayload[0].payload.period);
}
}}
>
<defs>
<linearGradient id="spendGradient" x1="0" y1="0" x2="0" y2="1">
<stop
offset="5%"
stopColor={accentColor}
stopOpacity={resolvedTheme === "dark" ? 0.2 : 0.3}
/>
<stop offset="95%" stopColor={accentColor} stopOpacity={0} />
</linearGradient>
</defs>
<CartesianGrid strokeDasharray="3 3" stroke={borderColor} />
<XAxis
dataKey="label"
tick={{ fill: mutedColor, fontSize: 12 }}
axisLine={{ stroke: borderColor }}
/>
<YAxis
tickFormatter={formatCurrency}
tick={{ fill: mutedColor, fontSize: 12 }}
axisLine={{ stroke: borderColor }}
/>
<Tooltip
// eslint-disable-next-line @typescript-eslint/no-explicit-any
formatter={(value: any) => [
formatCurrency(Number(value ?? 0)),
"Total Spent",
]}
contentStyle={{
backgroundColor: surfaceColor,
border: `1px solid ${borderColor}`,
borderRadius: "0.5rem",
boxShadow:
resolvedTheme === "dark"
? "0 1px 3px rgba(0,0,0,0.4)"
: "0 1px 3px rgba(0,0,0,0.1)",
color: textColor,
}}
labelStyle={{ color: textColor }}
/>
<Area
type="monotone"
dataKey="totalSpent"
stroke={accentColor}
strokeWidth={2}
fill="url(#spendGradient)"
animationDuration={1000}
/>
</AreaChart>
</ResponsiveContainer>
{/* ── Summary Stats ── */}
<div className="mt-4 grid grid-cols-3 gap-4 text-center">
<div>
<p className="text-sm text-muted-theme">Total</p>
<p className="text-lg font-semibold text-theme">
{formatCurrency(data.reduce((s, d) => s + d.totalSpent, 0))}
</p>
</div>
<div>
<p className="text-sm text-muted-theme">Transactions</p>
<p className="text-lg font-semibold text-theme">
{data.reduce((s, d) => s + d.transactionCount, 0)}
</p>
</div>
<div>
<p className="text-sm text-muted-theme">Avg / Tx</p>
<p className="text-lg font-semibold text-theme">
{formatCurrency(
data.length
? data.reduce((s, d) => s + d.avgTransactionAmount, 0) /
data.length
: 0,
)}
</p>
</div>
</div>
</div>
);
}