From 04b72d58439665ef475bf25e53c02ed7db9e8eed Mon Sep 17 00:00:00 2001 From: Vishesh 'ironeagle' Bangotra Date: Thu, 7 May 2026 15:12:27 +0530 Subject: [PATCH] correct comparison --- src/components/HistoryChart/HistoryChart.tsx | 41 ++++++++++++++------ 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/src/components/HistoryChart/HistoryChart.tsx b/src/components/HistoryChart/HistoryChart.tsx index ba7ee4a..bef5e23 100644 --- a/src/components/HistoryChart/HistoryChart.tsx +++ b/src/components/HistoryChart/HistoryChart.tsx @@ -70,6 +70,35 @@ function mergeBuckets( ); } +function attachComparison( + points: ChartDataPoint[], + key: "weekly" | "monthly" | "yearly" | "fyly" +): ChartDataPoint[] { + const getCompareIndex = (i: number) => { + if (key === "weekly") return i - 4; + if (key === "monthly") return i - 12; + if (key === "yearly") return i - 1; + if (key === "fyly") return i - 1; + return -1; + }; + + return points.map((p, i) => { + const ci = getCompareIndex(i); + + return { + ...p, + compare: + ci >= 0 && points[ci] + ? { + id: points[ci].id, + label: points[ci].label, + amount: points[ci].amount + } + : undefined + }; + }); +} + function buildChartData( reportData: HistoryChartProps["reportData"], key: "weekly" | "monthly" | "yearly" | "fyly", @@ -86,17 +115,7 @@ function buildChartData( })); if (comparison) { - points = points.map((p, i) => ({ - ...p, - compare: - i > 0 - ? { - id: points[i - 1].id, - label: points[i - 1].label, - amount: points[i - 1].amount - } - : undefined - })); + points = attachComparison(points, key); } return points;