import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, Legend, } from "recharts"; import type { Balance } from "../../types/api"; interface BalanceChartProps { data: Balance[]; className?: string; } interface ChartDataPoint { date: string; balance: number; account_id: string; } interface AggregatedDataPoint { date: string; [key: string]: string | number; } export default function BalanceChart({ data, className }: BalanceChartProps) { // Process balance data for the chart const chartData = data .filter((balance) => balance.balance_type === "closingBooked") .map((balance) => ({ date: new Date(balance.reference_date).toLocaleDateString(), balance: balance.balance_amount, account_id: balance.account_id, })) .sort((a, b) => new Date(a.date).getTime() - new Date(b.date).getTime()); // Group by account and aggregate const accountBalances: { [key: string]: ChartDataPoint[] } = {}; chartData.forEach((item) => { if (!accountBalances[item.account_id]) { accountBalances[item.account_id] = []; } accountBalances[item.account_id].push(item); }); // Create aggregated data points const aggregatedData: { [key: string]: AggregatedDataPoint } = {}; Object.entries(accountBalances).forEach(([accountId, balances]) => { balances.forEach((balance) => { if (!aggregatedData[balance.date]) { aggregatedData[balance.date] = { date: balance.date }; } aggregatedData[balance.date][accountId] = balance.balance; }); }); const finalData = Object.values(aggregatedData).sort( (a, b) => new Date(a.date).getTime() - new Date(b.date).getTime() ); const colors = ["#3B82F6", "#10B981", "#F59E0B", "#EF4444", "#8B5CF6"]; if (finalData.length === 0) { return (

Balance Progress

No balance data available
); } return (

Balance Progress Over Time

{ const date = new Date(value); return date.toLocaleDateString(undefined, { month: "short", day: "numeric", }); }} /> `€${value.toLocaleString()}`} /> [ `€${value.toLocaleString()}`, "Balance", ]} labelFormatter={(label) => `Date: ${label}`} /> {Object.keys(accountBalances).map((accountId, index) => ( ))}
); }