Repository navigation
fix(app): label year-scale time charts by month and year #3832
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
|---|---|---|
|
|
@@ -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, | ||
|
|
@@ -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, { | ||
|
|
@@ -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( | ||
|
|
@@ -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 = | ||
| xAxisTicks && | ||
| tickSeconds !== xAxisDomain[0] && | ||
| tickSeconds !== xAxisDomain[1] | ||
| ? 'middle' | ||
| : compactTickAnchor(index, visibleTicksCount); | ||
| return ( | ||
| <Text | ||
| x={x} | ||
|
|
@@ -504,7 +529,7 @@ export const MemoChart = memo(function MemoChart({ | |
| </Text> | ||
| ); | ||
| }, | ||
| [xTickFormatter, axisTickFontSize], | ||
| [xTickFormatter, axisTickFontSize, xAxisTicks, xAxisDomain], | ||
| ); | ||
|
|
||
| const tickFormatter = useCallback( | ||
|
|
@@ -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. | ||
|
|
@@ -589,6 +624,7 @@ export const MemoChart = memo(function MemoChart({ | |
| numberFormatByKey={tooltipNumberFormatsByKey} | ||
| lineDataMap={lineDataMap} | ||
| previousPeriodOffsetSeconds={previousPeriodOffsetSeconds} | ||
| getDateFormat={getTooltipLabelDateFormat} | ||
| activePointYByKeyRef={activePointYByKeyRef} | ||
| containerRef={containerRef} | ||
| /> | ||
|
|
@@ -598,6 +634,7 @@ export const MemoChart = memo(function MemoChart({ | |
| tooltipNumberFormatsByKey, | ||
| lineDataMap, | ||
| previousPeriodOffsetSeconds, | ||
| getTooltipLabelDateFormat, | ||
| ], | ||
| ); | ||
|
|
||
|
|
@@ -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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' |
||
| ? renderCompactXTick | ||
|
|
||
There was a problem hiding this comment.
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]orxAxisDomain[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 likeNov 2026(about 55px) then runs past the SVG's right edge. Fix: keepcompactTickAnchorfor 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