fix: 修复折线图时区问题

This commit is contained in:
tbphp
2025-07-13 23:50:49 +08:00
parent 1a800dd452
commit 2740254cf1
2 changed files with 15 additions and 3 deletions

View File

@@ -135,7 +135,7 @@ func (s *Server) Chart(c *gin.Context) {
for i := 0; i < 24; i++ { for i := 0; i < 24; i++ {
hour := twentyFourHoursAgo.Add(time.Duration(i) * time.Hour).Truncate(time.Hour) hour := twentyFourHoursAgo.Add(time.Duration(i) * time.Hour).Truncate(time.Hour)
labels = append(labels, hour.Format("15:04")) labels = append(labels, hour.Format(time.RFC3339))
if data, ok := statsByHour[hour]; ok { if data, ok := statsByHour[hour]; ok {
successData = append(successData, data["success"]) successData = append(successData, data["success"])

View File

@@ -72,6 +72,16 @@ const yTicks = computed(() => {
return Array.from({ length: tickCount }, (_, i) => min + i * step); return Array.from({ length: tickCount }, (_, i) => min + i * step);
}); });
// 格式化时间标签
const formatTimeLabel = (isoString: string) => {
const date = new Date(isoString);
return date.toLocaleTimeString(undefined, {
hour: "2-digit",
minute: "2-digit",
hour12: false,
});
};
// 生成可见的X轴标签避免重叠 // 生成可见的X轴标签避免重叠
const visibleLabels = computed(() => { const visibleLabels = computed(() => {
if (!chartData.value) { if (!chartData.value) {
@@ -82,7 +92,9 @@ const visibleLabels = computed(() => {
const maxLabels = 8; // 最多显示8个标签 const maxLabels = 8; // 最多显示8个标签
const step = Math.ceil(labels.length / maxLabels); const step = Math.ceil(labels.length / maxLabels);
return labels.map((label, index) => ({ text: label, index })).filter((_, i) => i % step === 0); return labels
.map((label, index) => ({ text: formatTimeLabel(label), index }))
.filter((_, i) => i % step === 0);
}); });
// 位置计算函数 // 位置计算函数
@@ -266,7 +278,7 @@ const handleMouseMove = (event: MouseEvent) => {
}; };
tooltipData.value = { tooltipData.value = {
time: chartData.value.labels[closestTimeIndex], time: formatTimeLabel(chartData.value.labels[closestTimeIndex]),
datasets: datasetsAtTime, datasets: datasetsAtTime,
}; };
} else { } else {