Skip to content

Commit 059e66b

Browse files
committed
refactor: improve NetworkChart x-axis tick display logic for better time range visualization
1 parent a184017 commit 059e66b

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/components/NetworkChart.tsx

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,16 +268,34 @@ export const NetworkChartClient = React.memo(function NetworkChart({
268268
axisLine={false}
269269
tickMargin={8}
270270
minTickGap={80}
271-
interval={0}
272271
ticks={processedData
273-
.filter((item) => {
272+
.filter((item, index, array) => {
273+
if (array.length < 6) {
274+
return index === 0 || index === array.length - 1
275+
}
276+
277+
// 计算数据的总时间跨度(毫秒)
278+
const timeSpan = array[array.length - 1].created_at - array[0].created_at
279+
const hours = timeSpan / (1000 * 60 * 60)
280+
281+
// 根据时间跨度调整显示间隔
282+
if (hours <= 12) {
283+
// 12小时内,每60分钟显示一个刻度
284+
return (
285+
index === 0 ||
286+
index === array.length - 1 ||
287+
new Date(item.created_at).getMinutes() % 60 === 0
288+
)
289+
}
290+
// 超过12小时,每2小时显示一个刻度
274291
const date = new Date(item.created_at)
275-
return date.getMinutes() === 0 && date.getHours() % 3 === 0
292+
return date.getMinutes() === 0 && date.getHours() % 2 === 0
276293
})
277294
.map((item) => item.created_at)}
278295
tickFormatter={(value) => {
279296
const date = new Date(value)
280-
return `${date.getHours()}:00`
297+
const minutes = date.getMinutes()
298+
return minutes === 0 ? `${date.getHours()}:00` : `${date.getHours()}:${minutes}`
281299
}}
282300
/>
283301
<YAxis tickLine={false} axisLine={false} tickMargin={15} minTickGap={20} tickFormatter={(value) => `${value}ms`} />

0 commit comments

Comments
 (0)