diff --git a/.changeset/stacked-chart-distinct-y-ticks.md b/.changeset/stacked-chart-distinct-y-ticks.md new file mode 100644 index 000000000..3a9437cdf --- /dev/null +++ b/.changeset/stacked-chart-distinct-y-ticks.md @@ -0,0 +1,6 @@ +--- +'@hyperdx/app': patch +--- + +fix: stacked bar and stacked line charts no longer show duplicate Y-axis labels +(e.g. "0, 500, 1k, 2k, 2k" when 1.5k rounds to 2k) diff --git a/packages/app/src/HDXMultiSeriesTimeChart.tsx b/packages/app/src/HDXMultiSeriesTimeChart.tsx index 3de997cf7..a2cfecd20 100644 --- a/packages/app/src/HDXMultiSeriesTimeChart.tsx +++ b/packages/app/src/HDXMultiSeriesTimeChart.tsx @@ -1134,6 +1134,32 @@ export function scanYAxisValueRange( return { min, max }; } +// Mirrors Recharts' stackOffset="none": each series sits on the running sum +// of the ones before it, starting from a zero baseline, and the axis must +// cover every one of those running sums. +export function scanStackedYAxisValueRange( + graphResults: any[], + lineData: LineData[], +): { min: number; max: number } { + let min = Infinity; + let max = -Infinity; + graphResults.forEach(dataPoint => { + let sum = 0; + lineData.forEach(ld => { + const value = dataPoint[ld.dataKey]; + if (typeof value === 'number' && !isNaN(value)) { + sum += value; + min = Math.min(min, sum); + max = Math.max(max, sum); + } + }); + }); + if (max === -Infinity) { + return { min, max }; + } + return { min: Math.min(min, 0), max: Math.max(max, 0) }; +} + export interface YAxisBounds { domain: AxisDomain; ticks: number[] | undefined; @@ -1168,8 +1194,6 @@ 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. export function computeYAxisBounds( graphResults: any[], visibleLineData: LineData[], @@ -1179,21 +1203,22 @@ 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) { + // Stacked charts always stay zero-pinned: fit-to-data is line-only, and a + // selection already narrows visibleLineData, so it changes the stack sums. + 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); + const { min, max } = isStacked + ? scanStackedYAxisValueRange(graphResults, visibleLineData) + : scanYAxisValueRange(graphResults, visibleLineData); if (max === -Infinity) { return DEFAULT_Y_AXIS_BOUNDS; } diff --git a/packages/app/src/__tests__/HDXMultiSeriesTimeChart.test.ts b/packages/app/src/__tests__/HDXMultiSeriesTimeChart.test.ts index 9a53c8438..f2dd15690 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,39 @@ describe('scanYAxisValueRange', () => { }); }); +describe('scanStackedYAxisValueRange', () => { + const series = (dataKey: string): LineData => ({ + dataKey, + currentPeriodKey: dataKey, + previousPeriodKey: `${dataKey}.prev`, + displayName: dataKey, + valueColumnName: dataKey, + groupValues: [], + color: '#a', + }); + + it('spans every running sum of the stack, as Recharts draws it', () => { + // Recharts stacks each series on the previous one's top (stackOffset + // "none"), so a negative series can dip a segment below its baseline. + const lineData = [series('a'), series('b'), series('c')]; + const graphResults = [ + { a: 10, b: 30, c: -50 }, + { a: 5, c: 20 }, + ]; + expect(scanStackedYAxisValueRange(graphResults, lineData)).toEqual({ + min: -10, + max: 40, + }); + }); + + it('returns an empty range when no series has a numeric value', () => { + expect(scanStackedYAxisValueRange([{ a: null }], [series('a')])).toEqual({ + min: Infinity, + max: -Infinity, + }); + }); +}); + describe('computeYAxisBounds', () => { const series = (dataKey: string): LineData => ({ dataKey, @@ -536,40 +570,77 @@ 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 height, not the tallest series', + displayType => { + // 60 and 40 individually reach a stack height of 100, not 63. + 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, + [], + ), + ).toEqual({ + domain: [0, 105], + ticks: [0, 25, 50, 75, 100], + tickFormatter: expect.any(Function), + }); + } + }, + ); + + it.each([DisplayType.StackedBar, DisplayType.StackedLine])( + 'never shows duplicate %s tick labels when 1.5k rounds to 2k', + displayType => { + // Regression: stacked charts deferred to Recharts' ticks, which + // rendered 0/500/1k/2k/2k for a 2000-high stack at 0 decimals. + const bounds = computeYAxisBounds( + [ + { a: 1200, b: 800 }, + { a: 600, b: 300 }, + ], + [series('a'), series('b')], + false, + false, + displayType, + [], + { output: 'number', mantissa: 0 }, + ); + const labels = bounds.ticks!.map(t => bounds.tickFormatter!(t)); + expect(labels).toEqual(['0', '500', '1k', '1.5k', '2k']); + }, + ); + + it('keeps a stacked axis pinned at zero even when fit-to-data is on', () => { + // Fit-to-data only applies to line charts; a stacked bar cut off above + // zero would misrepresent each segment's height. + const bounds = computeYAxisBounds( + [{ a: 900, b: 100 }], + [series('a'), series('b')], + false, + true, + DisplayType.StackedBar, + [], + ); + expect(bounds.domain).toEqual([0, 1050]); }); - 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('leaves a stacked axis with a reference line to Recharts', () => { + const bounds = computeYAxisBounds( + [{ a: 60, b: 40 }], + [series('a'), series('b')], + false, + false, + DisplayType.StackedBar, + [150], + ); + expect(bounds).toEqual({ domain: [0, 'auto'], ticks: undefined }); }); it('falls back to auto when there is no numeric data', () => {