correct comparison
This commit is contained in:
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user