From 8a8f07c3c303fe4651affca6379293c6d13f7d94 Mon Sep 17 00:00:00 2001 From: junlu592 Date: Mon, 1 Dec 2025 13:16:47 +0100 Subject: [PATCH] bin labels and totals correct for year, review --- ts/lib/tslib/time.ts | 41 ++++++++++++++++++++++++++----------- ts/routes/graphs/reviews.ts | 4 +++- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/ts/lib/tslib/time.ts b/ts/lib/tslib/time.ts index 25d70eef3..a6285ff3f 100644 --- a/ts/lib/tslib/time.ts +++ b/ts/lib/tslib/time.ts @@ -170,20 +170,37 @@ export function dayLabel(daysStart: number, daysEnd: number): string { } else { return tr.statisticsDaysAgoSingle({ days: -daysStart }); } - } else { - // range - if (daysStart >= 0) { - return tr.statisticsInDaysRange({ - daysStart, - daysEnd: daysEnd - 1, - }); } else { - return tr.statisticsDaysAgoRange({ - daysStart: Math.abs(daysEnd - 1), - daysEnd: -daysStart, - }); + // range + if (daysStart >= 0) { + return tr.statisticsInDaysRange({ + daysStart, + daysEnd: daysEnd - 1, + }); + } else { + // For bins that cross or end at day 0, we need special handling + if (daysEnd > 0) { + // Bin crosses day 0: show from 0 to the oldest day (exclusive of endDay) + // If bin is [-5, 1), we want "0-4 days ago" + return tr.statisticsDaysAgoRange({ + daysStart: 0, + daysEnd: -daysStart - 1, + }); + } else { + // Bin is entirely in the past: show from newest to oldest + // For a bin [startDay, endDay), to show consecutive ranges like "0-4", "5-9", "10-14", + // we use endDay as the start of the range + // The bin width is daysEnd - daysStart, so daysEnd = daysStart + (width - 1) + // Example: bin [-10, -5) has width 5, should show "5-9 days ago" + const binWidth = Math.abs(daysEnd - daysStart); + const daysStartAbs = Math.abs(daysEnd); + return tr.statisticsDaysAgoRange({ + daysStart: daysStartAbs, + daysEnd: daysStartAbs + binWidth - 1, + }); + } + } } - } } /** Helper for converting Unix timestamps to date strings. */ diff --git a/ts/routes/graphs/reviews.ts b/ts/routes/graphs/reviews.ts index 5235939fc..ed9c799d2 100644 --- a/ts/routes/graphs/reviews.ts +++ b/ts/routes/graphs/reviews.ts @@ -237,7 +237,9 @@ export function renderReviews( function tooltipText(d: BinType, cumulative: number): string { const startDay = Math.floor(d.x0!); - const endDay = Math.ceil(d.x1!); + // If bin ends at 0, treat it as including day 0 (so use 1 as endDay for dayLabel) + // For negative bins, use the bin end directly (dayLabel will handle the conversion) + const endDay = d.x1! === 0 ? 1 : d.x1! < 0 ? d.x1! : Math.ceil(d.x1!); const day = dayLabel(startDay, endDay); const totals = totalsForBin(d); const dayTotal = valueLabel(sum(totals));