import React, { useMemo } from 'react'; import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer } from 'recharts'; import { Balance, Operation } from '../types'; import { format, eachDayOfInterval, isBefore, isEqual } from 'date-fns'; interface OperationsChartProps { balances: Balance[]; allOperations: Operation[]; dateRange: { start: Date, end: Date }; } const OperationsChart: React.FC = ({ balances, allOperations, dateRange }) => { const chartData = useMemo(() => { const days = eachDayOfInterval(dateRange); const balanceColors = ['#1f2937', '#6b7280', '#ef4444', '#10b981', '#3b82f6', '#a855f7']; const data = days.map(day => { const entry: { date: string, [key: string]: number | string } = { date: format(day, 'MMM dd'), }; balances.forEach(balance => { const balanceAtStartOfRange = balance.initialAmount + allOperations .filter(op => op.balanceId === balance.id && isBefore(new Date(op.date), dateRange.start)) .reduce((acc, op) => acc + (op.type === 'income' ? op.amount : -op.amount), 0); const dailyTotal = allOperations .filter(op => op.balanceId === balance.id && isBefore(new Date(op.date), day) && isEqual(new Date(op.date), day)) .reduce((acc, op) => acc + (op.type === 'income' ? op.amount : -op.amount), balanceAtStartOfRange); const operationsInPeriodUntilDay = allOperations .filter(op => { const opDate = new Date(op.date); return op.balanceId === balance.id && opDate >= dateRange.start && opDate <= day; }) .reduce((acc, op) => acc + (op.type === 'income' ? op.amount : -op.amount), 0); entry[balance.name] = balanceAtStartOfRange + operationsInPeriodUntilDay; }); return entry; }); return { data, colors: balanceColors }; }, [balances, allOperations, dateRange]); if (!balances.length) { return

No balances to display.

} return (
new Intl.NumberFormat('fr-FR', { notation: 'compact', compactDisplay: 'short' }).format(value as number)}/> new Intl.NumberFormat('fr-FR', { style: 'currency', currency: 'EUR' }).format(value as number)} /> {balances.map((balance, index) => ( ))}
); }; export default OperationsChart;