Skip to content

Commit ba98fb5

Browse files
authored
Stats: format dates using UTC time instead of local time by default (#3103)
* Stats: format dates using UTC time instead of local time by default * hide chart tooltip whenever new data comes in
1 parent 386606c commit ba98fb5

File tree

5 files changed

+27
-11
lines changed

5 files changed

+27
-11
lines changed

toolkit/components/charts/parts/ChartTooltip.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@ export const ChartTooltip = React.memo(({
151151
};
152152
}, [ anchorEl, createPointerTracker, draw, hideContent, showContent ]);
153153

154+
const lastItemDateString = data[0].items[data[0].items.length - 1]?.date?.toISOString();
155+
156+
React.useEffect(() => {
157+
if (trackerId.current) {
158+
trackerId.current = undefined;
159+
hideContent();
160+
}
161+
}, [ hideContent, lastItemDateString ]);
162+
154163
return (
155164
<g
156165
ref={ ref }

toolkit/components/charts/parts/tooltip/ChartTooltipRow.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export function useRenderRows(ref: React.RefObject<SVGGElement | null>, { data,
7070
if (index === 0) {
7171
const date = xScale.invert(x);
7272
const dateValue = data[0].items.find((item) => item.date.getTime() === date.getTime())?.dateLabel;
73-
const dateValueFallback = d3.timeFormat('%e %b %Y')(xScale.invert(x));
73+
const dateValueFallback = d3.utcFormat('%e %b %Y')(xScale.invert(x));
7474
return dateValue || dateValueFallback;
7575
}
7676

toolkit/components/charts/utils/getDateLabel.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import { Resolution } from '../types';
55
export function getDateLabel(date: Date, dateTo?: Date, resolution?: Resolution): string {
66
switch (resolution) {
77
case Resolution.WEEK:
8-
return d3.timeFormat('%e %b %Y')(date) + (dateTo ? ` – ${ d3.timeFormat('%e %b %Y')(dateTo) }` : '');
8+
return d3.utcFormat('%e %b %Y')(date) + (dateTo ? ` – ${ d3.utcFormat('%e %b %Y')(dateTo) }` : '');
99
case Resolution.MONTH:
10-
return d3.timeFormat('%b %Y')(date);
10+
return d3.utcFormat('%b %Y')(date);
1111
case Resolution.YEAR:
12-
return d3.timeFormat('%Y')(date);
12+
return d3.utcFormat('%Y')(date);
1313
default:
14-
return d3.timeFormat('%e %b %Y')(date);
14+
return d3.utcFormat('%e %b %Y')(date);
1515
}
1616
}

toolkit/components/charts/utils/timeChartAxis.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ export function getAxesParams(data: Data, axesConfig?: AxesConfig) {
2222
return {
2323
x: {
2424
scale: getAxisParamsX(data).scale,
25-
tickFormatter: tickFormatterX,
25+
tickFormatter: axesConfig?.x?.tickFormatter ?? tickFormatterX,
2626
},
2727
y: {
2828
scale: yScale,
2929
labelFormatParams: labelFormatParamsY,
30-
tickFormatter: getTickFormatterY(labelFormatParamsY),
30+
tickFormatter: axesConfig?.y?.tickFormatter ?? getTickFormatterY(labelFormatParamsY),
3131
},
3232
};
3333
}
@@ -48,13 +48,13 @@ const tickFormatterX = (axis: d3.Axis<d3.NumberValue>) => (d: d3.AxisDomain) =>
4848
const span = Number(extent[1]) - Number(extent[0]);
4949

5050
if (span > 2 * YEAR) {
51-
format = d3.timeFormat('%Y');
51+
format = d3.utcFormat('%Y');
5252
} else if (span > 4 * MONTH) {
53-
format = d3.timeFormat('%b \'%y');
53+
format = d3.utcFormat('%b \'%y');
5454
} else if (span > 2 * DAY) {
55-
format = d3.timeFormat('%d %b');
55+
format = d3.utcFormat('%d %b');
5656
} else {
57-
format = d3.timeFormat('%H:%M');
57+
format = d3.utcFormat('%H:%M');
5858
}
5959

6060
return format(d as Date);

ui/megaEth/uptime/UptimeCharts.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ const AXES_CONFIG_BASE: AxesConfigFn = ({ isEnlarged, isMobile }) => ({
2727
},
2828
x: {
2929
ticks: isEnlarged && !isMobile ? 8 : 5,
30+
tickFormatter: () => (d: d3.AxisDomain) => {
31+
return d3.timeFormat('%H:%M')(d as Date);
32+
},
3033
},
3134
});
3235

@@ -108,6 +111,10 @@ const UptimeCharts = ({ historyData }: Props) => {
108111
const items = smoothedData
109112
.map(({ value, timestamp }) => {
110113
const date = new Date(timestamp * SECOND);
114+
// Here and below,
115+
// despite the other charts in the stats page, this one should display dates & time in the local timezone, not UTC.
116+
// This is because it is a "time-based" chart not a "date-based" one.
117+
// That is why we use d3.timeFormat() instead of dayjs.utcFormat() here.
111118
return { date, value: Number(value.toFixed(0)), dateLabel: d3.timeFormat(TIME_FORMAT)(date) };
112119
})
113120
.filter(filterByInterval(interval, now));

0 commit comments

Comments
 (0)