diff --git a/apps/www/src/app/examples/timeline-stress/page.tsx b/apps/www/src/app/examples/timeline-stress/page.tsx index 770c01d52..99d237bf0 100644 --- a/apps/www/src/app/examples/timeline-stress/page.tsx +++ b/apps/www/src/app/examples/timeline-stress/page.tsx @@ -100,9 +100,10 @@ const STATUS_COLOR: Record = { }; /** - * Card height is fixed at 64 to sit inside the 66px lane pitch. Under - * `virtualized` the lane height is exactly `estimatedRowHeight`, so a taller - * card would overflow into the lane below instead of growing its own. + * Card height is fixed at 64 × scaling to sit inside the default lane height + * of 66 × scaling. Under `virtualized` the lane height is exactly + * `estimatedRowHeight`, so a taller card would overflow toward the lane below + * instead of growing its own. */ function TaskCard({ task, @@ -114,7 +115,7 @@ function TaskCard({ return (
{ // jsdom doesn't implement ResizeObserver, and the timeline observes its scroll @@ -198,6 +204,41 @@ describe('buildAxis', () => { expect(roomy.every(t => t.showLabel)).toBe(true); }); + it('labels at exact multiples of the label spacing despite float error', () => { + // The call site divides by scaling: 29.4 / 1.05 = 27.999999999999996. + expect( + buildAxis(januaryScale, 'day', 29.4 / 1.05).ticks.every(t => t.showLabel) + ).toBe(true); + const half = buildAxis(januaryScale, 'day', 14.7 / 1.05).ticks; + expect(half.map(t => t.showLabel).slice(0, 4)).toEqual([ + true, + false, + true, + false + ]); + }); + + it('keeps the default label spacing at every scaling', () => { + for (const s of SCALINGS) { + const scaling = Number(s); + const every = (['day', 'week', 'month', 'quarter'] as const).map( + scale => { + const unitWidth = TIMELINE_DEFAULT_UNIT_WIDTH[scale] * scaling; + const scaleForUnit = createTimeScale({ + minTime: new Date(2025, 0, 1).getTime(), + maxTime: new Date(2027, 0, 1).getTime(), + scale, + unitWidth, + padUnits: 0 + }); + const { ticks } = buildAxis(scaleForUnit, scale, unitWidth / scaling); + return ticks.findIndex((t, i) => i > 0 && t.showLabel); + } + ); + expect(every).toEqual([2, 1, 1, 1]); + } + }); + it('labels every Nth unit when labelEvery is passed', () => { const roomyScale = createTimeScale({ minTime: new Date(2025, 0, 1).getTime(), @@ -2225,7 +2266,7 @@ describe('DataView.Timeline theme scaling', () => { ]; function renderScaled( - scaling: '1' | '1.1', + scaling: '1' | '1.05' | '1.1', props: Partial> = {}, data: Order[] = scaledOrders ) { @@ -2319,6 +2360,11 @@ describe('DataView.Timeline theme scaling', () => { expect(lefts(scaled.container, 'data-view-timeline-axis-tick')[1]).toBe(42); }); + it('labels every tick when unitWidth equals the scaled label floor', () => { + const { container } = renderScaled('1.05', { unitWidth: 29.4 }); + expect(labels(container)).toHaveLength(32); + }); + it('scales the gap between cards sharing a lane', () => { // At scaling 1, b starts 7.5px after a ends, under the 8px card gap. const nearlyTouching: Order[] = [ diff --git a/packages/react/components/data-view/utils/time-scale.tsx b/packages/react/components/data-view/utils/time-scale.tsx index 29759c3c2..dd8a4458c 100644 --- a/packages/react/components/data-view/utils/time-scale.tsx +++ b/packages/react/components/data-view/utils/time-scale.tsx @@ -37,6 +37,13 @@ export const TIMELINE_DEFAULT_UNIT_WIDTH: Record = { /** Minimum px between rendered tick labels. Denser ticks skip labels. */ const TICK_LABEL_MIN_SPACE = 28; +/** + * Absorbs float error in the spacing ratio. The call site divides the unit + * width by scaling, so a width on an exact multiple of the floor can come out + * a hair under it (29.4 / 1.05 = 27.999999999999996). + */ +const LABEL_SPACING_EPSILON = 1e-9; + /** Coerce a consumer-provided date (Date | epoch ms | parseable string | object with a numeric `valueOf`) to ms. */ export function toTimestamp(value: unknown): number | null { return toInstant(value)?.getTime() ?? null; @@ -167,8 +174,9 @@ function tickLabel(date: Date, scale: TimelineScale): string { * year in its label ("Jan 2025", then "Feb"). * * `labelEvery` labels every Nth unit, counted from the domain start. The - * collision floor (labels never closer than `TICK_LABEL_MIN_SPACE`) still - * applies, so a too-dense request degrades instead of overlapping. + * collision floor (labels never closer than `TICK_LABEL_MIN_SPACE`, within + * float error) still applies, so a too-dense request degrades instead of + * overlapping. * * Cost note: this materializes one tick per `scale` unit across the whole * domain on every rebuild, since `virtualized` culls what renders, not what gets @@ -184,7 +192,9 @@ export function buildAxis( ): { ticks: TimelineTick[]; bands: TimelineBand[] } { const autoLabelEvery = Math.max( 1, - Math.ceil(TICK_LABEL_MIN_SPACE / Math.max(1, unitWidth)) + Math.ceil( + TICK_LABEL_MIN_SPACE / Math.max(1, unitWidth) - LABEL_SPACING_EPSILON + ) ); const effectiveLabelEvery = Math.max( autoLabelEvery,