adjusted last bin labeling with xmin

This commit is contained in:
junlu592 2025-12-12 14:38:23 +01:00
parent ce9b087190
commit d00dc07613

View file

@ -109,6 +109,8 @@ export function renderReviews(
}
const desiredBars = Math.min(70, Math.abs(xMin!));
const shouldCapRange = range !== GraphRange.AllTime;
// Store original xMin before any adjustments (needed for periodDays calculation)
const originalXMinBeforeAdjustment = xMin!;
const originalXMin = shouldCapRange ? xMin! : undefined;
// Create initial scale to determine tick spacing
@ -230,7 +232,11 @@ export function renderReviews(
function tooltipText(d: BinType, cumulative: number): string {
// Convert bin boundaries [x0, x1) for dayLabel
// If bin ends at 0, treat it as crossing zero so day 0 is included
const startDay = Math.floor(d.x0!);
// For the first (oldest) bin, use the original xMin to ensure labels match the intended range
const isFirstBin = bins.length > 0 && d.x0 === bins[0].x0;
const startDay = isFirstBin
? originalXMinBeforeAdjustment
: Math.floor(d.x0!);
const endDay = d.x1! === 0 ? 1 : d.x1!;
const day = dayLabel(startDay, endDay);
const totals = totalsForBin(d);
@ -347,7 +353,11 @@ export function renderReviews(
})
.on("mouseout", hideTooltip);
const periodDays = -xMin + 1;
// Calculate periodDays from the actual data range, not the adjusted xMin
// For AllTime, xMin might be extended for bin alignment, so use the original xMin (actual oldest data)
// For fixed ranges, use the original xMin before adjustment (which matches the displayed range)
const actualXMin = originalXMinBeforeAdjustment;
const periodDays = -actualXMin + 1;
const studiedDays = sum(bins, (bin) => bin.length);
const studiedPercent = (studiedDays / periodDays) * 100;
const total = yCumMax;