From 846433d72d98b7e6a122f62572be13e188b71bc1 Mon Sep 17 00:00:00 2001 From: Alex Fedotyev <61838744+alex-fedotyev@users.noreply.github.com> Date: Tue, 17 Nov 2026 21:25:07 +0000 Subject: [PATCH] fix: keep stacked chart Y-axis labels distinct Stacked bar and stacked line charts skipped the Y-axis tick logic that keeps labels distinct, so Recharts picked the ticks and a 0-decimal number format could give two of them the same label (1500 and 2000 both read "2k"). Size the stacked axis from the per-timestamp stack total and send it through the same zero-pinned tick path line charts use. A legend selection only narrows which series are summed and fit-to-data only applies to line charts, so neither changes a stack's axis. A stack holding a negative value, or a chart with a reference line, keeps Recharts' default domain as before. Co-Authored-By: Claude Opus 5.5 --- .../stacked-chart-distinct-axis-labels.md | 6 + packages/app/src/HDXMultiSeriesTimeChart.tsx | 54 +++++- .../__tests__/HDXMultiSeriesTimeChart.test.ts | 176 ++++++++++++++---- 3 files changed, 194 insertions(+), 42 deletions(-) create mode 100644 .changeset/stacked-chart-distinct-axis-labels.md diff --git a/.changeset/stacked-chart-distinct-axis-labels.md b/.changeset/stacked-chart-distinct-axis-labels.md new file mode 100644 index 000000000..f0a8e0815 --- /dev/null +++ b/.changeset/stacked-chart-distinct-axis-labels.md @@ -0,0 +1,6 @@ +--- +'@hyperdx/app': patch +--- + +fix: stacked bar and stacked line charts no longer show duplicate Y-axis labels +(e.g. two "2k" ticks); the axis is now sized from the stack total diff --git a/packages/app/src/HDXMultiSeriesTimeChart.tsx b/packages/app/src/HDXMultiSeriesTimeChart.tsx index 3de997cf7..1940fa417 100644 --- a/packages/app/src/HDXMultiSeriesTimeChart.tsx +++ b/packages/app/src/HDXMultiSeriesTimeChart.tsx @@ -1134,6 +1134,38 @@ export function scanYAxisValueRange( return { min, max }; } +// The stacked counterpart of scanYAxisValueRange: a stack's drawn height at +// each timestamp is the sum of its series. Positive values sum up from zero +// and negative ones down from it, so min stays 0 unless a value is negative. +export function scanStackedYAxisValueRange( + graphResults: Record[], + lineData: LineData[], +): { min: number; max: number } { + let min = Infinity; + let max = -Infinity; + graphResults.forEach(dataPoint => { + let positiveSum = 0; + let negativeSum = 0; + let hasValue = false; + lineData.forEach(ld => { + const value = dataPoint[ld.dataKey]; + if (typeof value === 'number' && !isNaN(value)) { + hasValue = true; + if (value < 0) { + negativeSum += value; + } else { + positiveSum += value; + } + } + }); + if (hasValue) { + min = Math.min(min, negativeSum); + max = Math.max(max, positiveSum); + } + }); + return { min, max }; +} + export interface YAxisBounds { domain: AxisDomain; ticks: number[] | undefined; @@ -1168,8 +1200,9 @@ export function getLineCurveType( ]; } -// A stacked bar's rendered height sums its series at each timestamp - leave -// that entirely to Recharts, regardless of selection/fit-to-data state. +// A stack's drawn height at each timestamp sums its series, and it stays +// pinned at zero whatever the selection or fit-to-data state: a selection only +// narrows which series are summed, and fitting applies to line charts only. export function computeYAxisBounds( graphResults: any[], visibleLineData: LineData[], @@ -1179,22 +1212,23 @@ export function computeYAxisBounds( referenceLineValues: number[], axisNumberFormat?: NumberFormat, ): YAxisBounds { - if ( + const isStacked = displayType === DisplayType.StackedBar || - displayType === DisplayType.StackedLine - ) { - return DEFAULT_Y_AXIS_BOUNDS; - } + displayType === DisplayType.StackedLine; const shouldFitYAxis = fitYAxisToData; - if (!hasSelection && !shouldFitYAxis) { + if (isStacked || (!hasSelection && !shouldFitYAxis)) { // A fully numeric domain skips Recharts' own nice rounding, and a // reference line can extend it further - defer to Recharts entirely. if (referenceLineValues.length > 0) { return DEFAULT_Y_AXIS_BOUNDS; } - const { min, max } = scanYAxisValueRange(graphResults, visibleLineData); - if (max === -Infinity) { + const { min, max } = isStacked + ? scanStackedYAxisValueRange(graphResults, visibleLineData) + : scanYAxisValueRange(graphResults, visibleLineData); + // Where negative values land in a stack depends on Recharts' stackOffset, + // so a stack holding any keeps Recharts' own stacked domain. + if (max === -Infinity || (isStacked && min < 0)) { return DEFAULT_Y_AXIS_BOUNDS; } // Recharts widens an explicit domain to fit out-of-range data, so diff --git a/packages/app/src/__tests__/HDXMultiSeriesTimeChart.test.ts b/packages/app/src/__tests__/HDXMultiSeriesTimeChart.test.ts index 9a53c8438..21bba2f9f 100644 --- a/packages/app/src/__tests__/HDXMultiSeriesTimeChart.test.ts +++ b/packages/app/src/__tests__/HDXMultiSeriesTimeChart.test.ts @@ -24,6 +24,7 @@ import { getYAxisTicks, HARD_LINES_LIMIT, sameActiveClickSeries, + scanStackedYAxisValueRange, scanYAxisValueRange, } from '@/HDXMultiSeriesTimeChart'; import { COLORS } from '@/utils'; @@ -421,6 +422,54 @@ describe('scanYAxisValueRange', () => { }); }); +describe('scanStackedYAxisValueRange', () => { + const series = (dataKey: string): LineData => ({ + dataKey, + currentPeriodKey: dataKey, + previousPeriodKey: `${dataKey}.prev`, + displayName: dataKey, + valueColumnName: dataKey, + groupValues: [], + color: '#a', + }); + + it('sums the series it is given at each timestamp', () => { + // Only the drawn series stack, so `c` (not passed in) adds nothing. + const lineData = [series('a'), series('b')]; + const graphResults = [ + { a: 10, b: 40, c: 1000 }, + { a: 30, b: 5 }, + ]; + expect(scanStackedYAxisValueRange(graphResults, lineData)).toEqual({ + min: 0, + max: 50, + }); + }); + + it('skips missing and non-numeric values, like scanYAxisValueRange', () => { + const lineData = [series('a'), series('b')]; + const graphResults = [{ a: NaN, b: 7 }, { a: null, b: 'x' }, {}]; + expect(scanStackedYAxisValueRange(graphResults, lineData)).toEqual({ + min: 0, + max: 7, + }); + }); + + it('returns an empty range when there is no numeric data', () => { + expect(scanStackedYAxisValueRange([{ a: null }], [series('a')])).toEqual({ + min: Infinity, + max: -Infinity, + }); + }); + + it('sums negative values apart from positive ones', () => { + const lineData = [series('a'), series('b'), series('c')]; + expect( + scanStackedYAxisValueRange([{ a: 5, b: -3, c: -1 }], lineData), + ).toEqual({ min: -4, max: 5 }); + }); +}); + describe('computeYAxisBounds', () => { const series = (dataKey: string): LineData => ({ dataKey, @@ -536,40 +585,103 @@ describe('computeYAxisBounds', () => { expect(bounds).toEqual({ domain: [0, 'auto'], ticks: undefined }); }); - it('leaves a stacked bar`s max to Recharts, selection or not', () => { - // Regression: 60 and 40 individually reach a stack height of 100, not - // 63 - the guard must apply regardless of hasSelection. - const lineData = [series('a'), series('b')]; - const graphResults = [{ a: 60, b: 40 }]; - for (const hasSelection of [false, true]) { - expect( - computeYAxisBounds( - graphResults, - lineData, - hasSelection, - false, - DisplayType.StackedBar, - [], - ), - ).toEqual({ domain: [0, 'auto'], ticks: undefined }); - } + it.each([DisplayType.StackedBar, DisplayType.StackedLine])( + 'sizes a %s axis to the stack total, whatever the selection or fit', + displayType => { + // Regression: 60 and 40 individually reach a stack height of 100, not + // 63. A selection only narrows which series are summed, and fitting + // never lifts a stack off zero. + const lineData = [series('a'), series('b')]; + const graphResults = [{ a: 60, b: 40 }]; + for (const [hasSelection, fitYAxisToData] of [ + [false, false], + [true, false], + [false, true], + [true, true], + ]) { + expect( + computeYAxisBounds( + graphResults, + lineData, + hasSelection, + fitYAxisToData, + displayType, + [], + ), + ).toEqual({ + domain: [0, 105], + ticks: [0, 25, 50, 75, 100], + tickFormatter: expect.any(Function), + }); + } + }, + ); + + it.each([DisplayType.StackedBar, DisplayType.StackedLine])( + 'keeps %s tick labels distinct where Recharts` own ticks collide', + displayType => { + // Regression: Recharts ticked a 1870-high stack at 0/500/1000/1500/2000, + // which a 0-decimal number format labels "0, 500, 1k, 2k, 2k". + const bounds = computeYAxisBounds( + [{ a: 1000, b: 870 }], + [series('a'), series('b')], + false, + false, + displayType, + [], + { output: 'number' }, + ); + expect(bounds.domain).toEqual([0, 2000]); + expect(bounds.ticks).toEqual([0, 500, 1000, 1500, 2000]); + expect(bounds.ticks!.map(t => bounds.tickFormatter!(t))).toEqual([ + '0', + '500', + '1k', + '1.5k', + '2k', + ]); + }, + ); + + it('leaves a stack holding a negative value to Recharts', () => { + // Where a negative value lands depends on Recharts' stackOffset: with + // the default, 5 and -3 reach 5 before the stack settles at 2. + const bounds = computeYAxisBounds( + [ + { a: 5, b: -3 }, + { a: 2, b: 1 }, + ], + [series('a'), series('b')], + false, + false, + DisplayType.StackedBar, + [], + ); + expect(bounds).toEqual({ domain: [0, 'auto'], ticks: undefined }); }); - it('leaves a stacked line`s max to Recharts, selection or not', () => { - const lineData = [series('a'), series('b')]; - const graphResults = [{ a: 60, b: 40 }]; - for (const hasSelection of [false, true]) { - expect( - computeYAxisBounds( - graphResults, - lineData, - hasSelection, - false, - DisplayType.StackedLine, - [], - ), - ).toEqual({ domain: [0, 'auto'], ticks: undefined }); - } + it('defers a stack to Recharts when a reference line is present', () => { + const bounds = computeYAxisBounds( + [{ a: 60, b: 40 }], + [series('a'), series('b')], + false, + false, + DisplayType.StackedLine, + [150], + ); + expect(bounds).toEqual({ domain: [0, 'auto'], ticks: undefined }); + }); + + it('falls back to auto for an all-zero stack', () => { + const bounds = computeYAxisBounds( + [{ a: 0, b: 0 }], + [series('a'), series('b')], + true, + false, + DisplayType.StackedBar, + [], + ); + expect(bounds).toEqual({ domain: [0, 'auto'], ticks: undefined }); }); it('falls back to auto when there is no numeric data', () => {