correct comparison

This commit is contained in:
2026-05-07 15:12:27 +05:30
parent b97e350b1b
commit 04b72d5843

View File

@@ -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( function buildChartData(
reportData: HistoryChartProps["reportData"], reportData: HistoryChartProps["reportData"],
key: "weekly" | "monthly" | "yearly" | "fyly", key: "weekly" | "monthly" | "yearly" | "fyly",
@@ -86,17 +115,7 @@ function buildChartData(
})); }));
if (comparison) { if (comparison) {
points = points.map((p, i) => ({ points = attachComparison(points, key);
...p,
compare:
i > 0
? {
id: points[i - 1].id,
label: points[i - 1].label,
amount: points[i - 1].amount
}
: undefined
}));
} }
return points; return points;