Skip to content

Commit 74c45f7

Browse files
committed
Fix for memo issue with hiding graph labels
1 parent 0312aeb commit 74c45f7

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

apps/webapp/app/components/code/QueryResultsChart.tsx

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -771,18 +771,14 @@ export const QueryResultsChart = memo(function QueryResultsChart({
771771
return dateValues.length > 0 ? detectTimeGranularity(dateValues) : null;
772772
}, [dateValues, timeRange]);
773773

774-
// X-axis tick formatter for date-based axes
775-
// De-duplicates consecutive labels so e.g. "Feb 4" isn't repeated for every
776-
// data point within the same day
774+
// X-axis tick formatter for date-based axes (pure – no deduplication).
775+
// Label deduplication is handled inside dateAxisTick below so that the
776+
// mutable "lastLabel" state is correctly reset on each Recharts render pass.
777777
const xAxisTickFormatter = useMemo(() => {
778778
if (!isDateBased || !timeGranularity) return undefined;
779-
let lastLabel = "";
780779
return (value: number) => {
781780
const date = new Date(value);
782-
const label = formatDateByGranularity(date, timeGranularity);
783-
if (label === lastLabel) return "";
784-
lastLabel = label;
785-
return label;
781+
return formatDateByGranularity(date, timeGranularity);
786782
};
787783
}, [isDateBased, timeGranularity]);
788784

@@ -872,11 +868,25 @@ export const QueryResultsChart = memo(function QueryResultsChart({
872868

873869
// Custom tick renderer for date-based axes: renders a tick mark alongside
874870
// each label, and for unlabelled points (de-duplicated) just a subtle tick mark.
871+
// De-duplication lives here (not in xAxisTickFormatter) so that the mutable
872+
// lastLabel is reset when Recharts starts a new render pass (index === 0).
875873
const dateAxisTick = useMemo(() => {
876874
if (!isDateBased || !xAxisTickFormatter) return undefined;
875+
let lastLabel = "";
877876
return (props: Record<string, unknown>) => {
878-
const { x, y, payload } = props as { x: number; y: number; payload: { value: number } };
879-
const label = xAxisTickFormatter(payload.value);
877+
const { x, y, payload, index } = props as {
878+
x: number;
879+
y: number;
880+
payload: { value: number };
881+
index: number;
882+
};
883+
884+
// Reset dedup state at the start of each Recharts render pass
885+
if (index === 0) lastLabel = "";
886+
887+
const formatted = xAxisTickFormatter(payload.value);
888+
const label = formatted === lastLabel ? "" : formatted;
889+
lastLabel = formatted;
880890
// y is the tick text position, offset from the axis by tickMargin + internal padding
881891
const axisY = (y as number) - 12;
882892
if (label) {

0 commit comments

Comments
 (0)