Skip to content

Commit 44548f4

Browse files
authored
fix: custom adaptive time formatter for echart (#7493)
1 parent e9ac338 commit 44548f4

3 files changed

Lines changed: 62 additions & 198 deletions

File tree

packages/web/app/src/components/target/insights/Stats.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { Button } from '@/components/ui/button';
1818
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
1919
import { CHART_PRIMARY_COLOR } from '@/constants';
2020
import { FragmentType, graphql, useFragment } from '@/gql';
21+
import { createAdaptiveTimeFormatter } from '@/lib/date-time';
2122
import {
2223
formatDuration,
2324
formatNumber,
@@ -317,6 +318,13 @@ function OverTimeStats({
317318
{
318319
type: 'time',
319320
boundaryGap: false,
321+
axisLabel: {
322+
formatter: (value: number) =>
323+
createAdaptiveTimeFormatter(
324+
requests[0][0],
325+
requests[requests.length - 1][0],
326+
)(value),
327+
},
320328
},
321329
],
322330
yAxis: [
@@ -889,6 +897,10 @@ function LatencyOverTimeStats({
889897
type: 'dashed',
890898
},
891899
},
900+
axisLabel: {
901+
formatter: (value: number) =>
902+
createAdaptiveTimeFormatter(p75[0][0], p75[p75.length - 1][0])(value),
903+
},
892904
},
893905
],
894906
yAxis: [
@@ -983,6 +995,13 @@ function RpmOverTimeStats({
983995
type: 'dashed',
984996
},
985997
},
998+
axisLabel: {
999+
formatter: (value: number) =>
1000+
createAdaptiveTimeFormatter(
1001+
rpmOverTime[0][0],
1002+
rpmOverTime[rpmOverTime.length - 1][0],
1003+
)(value),
1004+
},
9861005
},
9871006
],
9881007
yAxis: [

packages/web/app/src/lib/date-time.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,45 @@ import { subHours } from 'date-fns';
55
export function subDays(date: Date | number, days: number) {
66
return subHours(date, days * 24);
77
}
8+
9+
export function createAdaptiveTimeFormatter(from?: string, to?: string) {
10+
let options: Intl.DateTimeFormatOptions = {
11+
hour: '2-digit',
12+
minute: '2-digit',
13+
};
14+
15+
if (from && to) {
16+
const range = new Date(to).getTime() - new Date(from).getTime();
17+
const hour = 60 * 60 * 1000;
18+
const day = 24 * hour;
19+
const month = 30 * day;
20+
21+
if (range < 13 * hour) {
22+
options = {
23+
hour: '2-digit',
24+
minute: '2-digit',
25+
};
26+
} else if (range < 2 * day) {
27+
options = {
28+
day: '2-digit',
29+
month: '2-digit',
30+
hour: '2-digit',
31+
minute: '2-digit',
32+
};
33+
} else if (range < 2 * month) {
34+
options = {
35+
day: '2-digit',
36+
month: '2-digit',
37+
};
38+
} else {
39+
options = {
40+
month: '2-digit',
41+
year: 'numeric',
42+
};
43+
}
44+
}
45+
46+
const formatter = new Intl.DateTimeFormat('en-GB', options);
47+
48+
return (value: number) => formatter.format(value);
49+
}

pnpm-lock.yaml

Lines changed: 1 addition & 198 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)