Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/stacked-chart-distinct-y-ticks.md
Original file line number Diff line number Diff line change
@@ -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)
43 changes: 34 additions & 9 deletions packages/app/src/HDXMultiSeriesTimeChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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[],
Expand All @@ -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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/nit Could this extend the domain to the reference line instead of handing ticks back to Recharts?

A stacked tile with an alert threshold still goes through here, so does it keep showing 2k, 2k? The fit-to-data branch further down already widens its bounds with referenceLineValues before picking ticks. Would the same work here?

const lowerBound = cleanNumber(Math.min(0, min, ...referenceLineValues));
const upperBound = cleanNumber(
  Math.max(max + Math.abs(max) * 0.05, ...referenceLineValues),
);

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;
}
Expand Down
135 changes: 103 additions & 32 deletions packages/app/src/__tests__/HDXMultiSeriesTimeChart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
getYAxisTicks,
HARD_LINES_LIMIT,
sameActiveClickSeries,
scanStackedYAxisValueRange,
scanYAxisValueRange,
} from '@/HDXMultiSeriesTimeChart';
import { COLORS } from '@/utils';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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', () => {
Expand Down
Loading