Skip to content
Open
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
9 changes: 9 additions & 0 deletions .changeset/year-scale-chart-dates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@hyperdx/common-utils': patch
'@hyperdx/app': patch
---

fix: label time charts spanning a year or more by month and year. X-axis ticks
sit on month starts and read like `Mar 2024`, and tooltips show the year
(`2024-03-18`, without a time of day for daily or coarser buckets). Shorter
ranges keep their existing formats.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
useChartTooltipZIndex,
} from '@/components/charts/ChartTooltip';
import type { NumberFormat } from '@/types';
import type { DateFormat } from '@/useFormatTime';

import { getVisibleTooltipRows } from './chartData';
import {
Expand All @@ -23,6 +24,7 @@ import styles from '@styles/HDXLineChart.module.scss';
type HDXLineChartTooltipProps = {
lineDataMap: { [keyName: string]: LineData };
previousPeriodOffsetSeconds?: number;
getDateFormat?: (labelSeconds: number) => DateFormat;
numberFormat?: NumberFormat;
numberFormatByKey: Map<string, NumberFormat>;
/** Per-series active-point pixel Y, captured by the Area active dots. */
Expand Down Expand Up @@ -53,6 +55,7 @@ export const HDXLineChartTooltip = withErrorBoundary(
numberFormatByKey,
lineDataMap,
previousPeriodOffsetSeconds,
getDateFormat,
activePointYByKeyRef,
containerRef,
} = props;
Expand All @@ -76,6 +79,7 @@ export const HDXLineChartTooltip = withErrorBoundary(
<ChartTooltipHeader
labelSeconds={label}
previousPeriodOffsetSeconds={previousPeriodOffsetSeconds}
dateFormat={getDateFormat?.(Number(label))}
/>
);

Expand Down
48 changes: 43 additions & 5 deletions packages/app/src/HDXMultiSeriesTimeChart/MemoChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ import { COLORS } from '@/utils';
import {
compactTickAnchor,
formatAxisTick,
getMonthStartTicks,
getTooltipDateFormat,
getXAxisTickDateFormat,
isYearScaleRange,
MONTH_TICK_MIN_GAP_PX,
} from './axisTicks';
import {
type ActiveClickPayload,
Expand Down Expand Up @@ -461,6 +465,7 @@ export const MemoChart = memo(function MemoChart({

const formatTime = useFormatTime();
const displayTimeZone = useDisplayTimeZone();
const yearScale = isYearScaleRange(dateRange);
const xTickFormatter = useCallback(
(value: number, index: number) => {
return formatTime(value * 1000, {
Expand All @@ -469,14 +474,28 @@ export const MemoChart = memo(function MemoChart({
index,
granularity,
timeZone: displayTimeZone,
yearScale,
}),
});
},
[formatTime, granularity, displayTimeZone],
[formatTime, granularity, displayTimeZone, yearScale],
);
const xAxisTicks = useMemo(
() =>
yearScale
? getMonthStartTicks({
domain: xAxisDomain,
timeZone: displayTimeZone,
plotWidth: containerWidth - yAxisWidth,
})
: undefined,
[yearScale, xAxisDomain, displayTimeZone, containerWidth, yAxisWidth],
);

// Compact mode: anchor the first label to the start and the last to the end
// so neither is clipped on a narrow chart. Renders every tick through one
// so neither is clipped on a narrow chart. Month-start ticks usually sit
// inside the plot, so only those on a domain bound are anchored to it.
// Renders every tick through one
// path (axis color, mono) so the axis stays visually consistent. `fill` is
// the color Recharts gives every tick, so these match the y-axis labels.
const renderCompactXTick = useCallback(
Expand All @@ -488,7 +507,13 @@ export const MemoChart = memo(function MemoChart({
visibleTicksCount,
fill,
}: XAxisTickContentProps) => {
const textAnchor = compactTickAnchor(index, visibleTicksCount);
const tickSeconds = Number(payload.value);
const textAnchor =

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.

🟡 P2 — Compact mode centres month ticks that sit near, but not on, a domain bound, so edge labels clip on narrow charts

Compact mode exists so edge labels stay inside narrow plots, such as the side-by-side RED tiles. With month ticks, only a tick exactly equal to xAxisDomain[0] or xAxisDomain[1] keeps the start/end anchor. Take "Last 1 year" ending Nov 25: the last tick is Nov 1, about 6% of the plot width from the right edge, roughly 18px on a 300px plot. A centred label like Nov 2026 (about 55px) then runs past the SVG's right edge. Fix: keep compactTickAnchor for the first and last ticks whenever the tick is within half a label width (or about 50px) of the plot bound, and centre only the interior ticks.

Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond

xAxisTicks &&
tickSeconds !== xAxisDomain[0] &&
tickSeconds !== xAxisDomain[1]
? 'middle'
: compactTickAnchor(index, visibleTicksCount);
return (
<Text
x={x}
Expand All @@ -504,7 +529,7 @@ export const MemoChart = memo(function MemoChart({
</Text>
);
},
[xTickFormatter, axisTickFontSize],
[xTickFormatter, axisTickFontSize, xAxisTicks, xAxisDomain],
);

const tickFormatter = useCallback(
Expand Down Expand Up @@ -579,6 +604,16 @@ export const MemoChart = memo(function MemoChart({
return map;
}, [lineData]);

const getTooltipLabelDateFormat = useCallback(
(labelSeconds: number) =>
getTooltipDateFormat({
labelSeconds,
yearScale,
granularity,
timeZone: displayTimeZone,
}),
[yearScale, granularity, displayTimeZone],
);
// Memoize the tooltip `content` element: recharts re-evaluates it every hover
// frame, so a fresh element each render defeats HDXLineChartTooltip's memo.
// Refs are stable, so only the listed values are deps.
Expand All @@ -589,6 +624,7 @@ export const MemoChart = memo(function MemoChart({
numberFormatByKey={tooltipNumberFormatsByKey}
lineDataMap={lineDataMap}
previousPeriodOffsetSeconds={previousPeriodOffsetSeconds}
getDateFormat={getTooltipLabelDateFormat}
activePointYByKeyRef={activePointYByKeyRef}
containerRef={containerRef}
/>
Expand All @@ -598,6 +634,7 @@ export const MemoChart = memo(function MemoChart({
tooltipNumberFormatsByKey,
lineDataMap,
previousPeriodOffsetSeconds,
getTooltipLabelDateFormat,
],
);

Expand Down Expand Up @@ -901,8 +938,9 @@ export const MemoChart = memo(function MemoChart({
interval="preserveStartEnd"
scale="time"
type="number"
ticks={xAxisTicks}
tickFormatter={xTickFormatter}
minTickGap={100}
minTickGap={xAxisTicks ? MONTH_TICK_MIN_GAP_PX : 100}
tick={
compactXAxisLabels

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.

🟡 P2 — Compact x-axis anchoring assumes the first and last ticks sit at the plot edges, which month-start ticks do not

renderCompactXTick uses compactTickAnchor, which left-anchors the first tick and right-anchors the last. With getMonthStartTicks those ticks are inside the plot (e.g. Jan 2025 on a Nov 2024–Nov 2026 range), so their labels drift off their tick marks. This is used via compactXAxisLabels (TraceRedMetricsChart). Center the labels when xAxisTicks is set, or anchor only ticks that fall on the domain bounds.

Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fixed in a250f8b. Compact labels stay centered when month-start ticks are in use. Recharts' preserveStartEnd still shifts an edge label inward if it would overflow.

? renderCompactXTick
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ import {
formatAxisTick,
getExpandableYAxisTicks,
getLineCurveType,
getMonthStartTicks,
getNiceYAxisTicks,
getSelectedLineData,
getTooltipDateFormat,
getVisibleLineData,
getVisibleTooltipRows,
getXAxisTickDateFormat,
getYAxisTicks,
HARD_LINES_LIMIT,
isYearScaleRange,
sameActiveClickSeries,
scanStackedYAxisValueRange,
scanYAxisValueRange,
Expand Down Expand Up @@ -1437,4 +1440,194 @@ describe('getXAxisTickDateFormat', () => {
}),
).toBe('time');
});

it.each(['1 hour', '1 day', '30 day'])(
'shows the month and year on every tick of a year-scale range at %s granularity',
granularity => {
for (const index of [0, 1]) {
expect(
getXAxisTickDateFormat({
tickSeconds: seconds('2026-10-27T04:00:00Z'),
index,
granularity,
timeZone: 'America/New_York',
yearScale: true,
}),
).toBe('monthYear');
}
},
);
});

describe('isYearScaleRange', () => {
it('starts at a year', () => {
expect(
isYearScaleRange([
new Date('2025-03-01T00:00:00Z'),
new Date('2026-03-01T00:00:00Z'),
]),
).toBe(true);
expect(
isYearScaleRange([
new Date('2025-03-02T00:00:00Z'),
new Date('2026-03-01T00:00:00Z'),
]),
).toBe(false);
});
});

describe('getTooltipDateFormat', () => {
const seconds = (iso: string) => new Date(iso).getTime() / 1000;
const utcMidnight = seconds('2025-11-08T00:00:00Z');

it('keeps the default format below a year', () => {
expect(
getTooltipDateFormat({
labelSeconds: utcMidnight,
yearScale: false,
granularity: '1 day',
timeZone: 'Etc/UTC',
}),
).toBe('normal');
});

it.each(['1 day', '30 day'])(
'shows only the date with the year for a midnight %s bucket',
granularity => {
expect(
getTooltipDateFormat({
labelSeconds: utcMidnight,
yearScale: true,
granularity,
timeZone: 'Etc/UTC',
}),
).toBe('isoDate');
},
);

it('keeps the time when a daily bucket does not start at local midnight', () => {
// A UTC-aligned bucket starts at 7 PM the previous day in New York.
expect(
getTooltipDateFormat({
labelSeconds: utcMidnight,
yearScale: true,
granularity: '1 day',
timeZone: 'America/New_York',
}),
).toBe('withYear');
expect(
getTooltipDateFormat({
labelSeconds: seconds('2025-11-08T05:00:00Z'),
yearScale: true,
granularity: '1 day',
timeZone: 'America/New_York',
}),
).toBe('isoDate');
});

it('keeps the time, with the year, for sub-day buckets', () => {
expect(
getTooltipDateFormat({
labelSeconds: utcMidnight,
yearScale: true,
granularity: '1 hour',
timeZone: 'Etc/UTC',
}),
).toBe('withYear');
});
});

describe('getMonthStartTicks', () => {
const seconds = (iso: string) => new Date(iso).getTime() / 1000;
const iso = (ticks: number[] | undefined) =>
ticks?.map(t => new Date(t * 1000).toISOString());
const twoYears: [number, number] = [
seconds('2024-11-25T00:00:00Z'),
seconds('2026-11-25T00:00:00Z'),
];

it('returns undefined before the chart is measured', () => {
expect(
getMonthStartTicks({
domain: twoYears,
timeZone: 'Etc/UTC',
plotWidth: 0,
}),
).toBeUndefined();
});

it('places ticks on month starts at the smallest step that fits', () => {
// 500px fits 5 ticks, so 25 months need a 6-month step.
expect(
iso(
getMonthStartTicks({
domain: twoYears,
timeZone: 'Etc/UTC',
plotWidth: 500,
}),
),
).toEqual([
'2025-01-01T00:00:00.000Z',
'2025-07-01T00:00:00.000Z',
'2026-01-01T00:00:00.000Z',
'2026-07-01T00:00:00.000Z',
]);
});

it('keeps two ticks on a narrow chart', () => {
expect(
iso(
getMonthStartTicks({
domain: twoYears,
timeZone: 'Etc/UTC',
plotWidth: 280,
}),
),
).toEqual(['2025-01-01T00:00:00.000Z', '2026-01-01T00:00:00.000Z']);
});

it('uses every month when there is room', () => {
const ticks = getMonthStartTicks({
domain: twoYears,
timeZone: 'Etc/UTC',
plotWidth: 4000,
});
expect(ticks).toHaveLength(24);
expect(iso(ticks)?.[0]).toBe('2024-12-01T00:00:00.000Z');
});

it('uses midnight in the display time zone, across DST changes', () => {
expect(
iso(
getMonthStartTicks({
domain: twoYears,
timeZone: 'America/New_York',
plotWidth: 500,
}),
),
).toEqual([
'2025-01-01T05:00:00.000Z',
'2025-07-01T04:00:00.000Z',
'2026-01-01T05:00:00.000Z',
'2026-07-01T04:00:00.000Z',
]);
});

it('steps by whole years on very long ranges', () => {
const ticks = getMonthStartTicks({
domain: [
seconds('2016-06-01T00:00:00Z'),
seconds('2026-06-01T00:00:00Z'),
],
timeZone: 'Etc/UTC',
plotWidth: 500,
});
expect(iso(ticks)).toEqual([
'2018-01-01T00:00:00.000Z',
'2020-01-01T00:00:00.000Z',
'2022-01-01T00:00:00.000Z',
'2024-01-01T00:00:00.000Z',
'2026-01-01T00:00:00.000Z',
]);
});
});
Loading
Loading