fix(charts): simplify time labels in investigation views
This commit is contained in:
@@ -41,3 +41,32 @@ export function formatDisplayText(text: string | null | undefined): string {
|
||||
});
|
||||
}).join("");
|
||||
}
|
||||
|
||||
/** Chart labels use the display clock without repeating its zone to readers. */
|
||||
export function formatChartDisplayText(value: string): string {
|
||||
return formatDisplayText(value)
|
||||
.replace(/[((]\s*(?:上海时间|Asia\/Shanghai|UTC\s*\+\s*8(?::00)?)\s*[))]/g, "")
|
||||
.replace(/(?:上海时间|Asia\/Shanghai|UTC\s*\+\s*8(?::00)?)(?![\w:+-])\s*/g, "")
|
||||
.replace(/[,,]\s*([))])/g, "$1")
|
||||
.trim();
|
||||
}
|
||||
|
||||
type ChartDisplayFields = {
|
||||
title: string;
|
||||
x_axis_name?: string;
|
||||
y_axis_name?: string;
|
||||
x_data: string[];
|
||||
series: Array<{ name: string }>;
|
||||
};
|
||||
|
||||
/** Preserve values and original sample timestamps; convert only visible labels. */
|
||||
export function withChartDisplayTimes<T extends ChartDisplayFields>(chart: T): T {
|
||||
const utcAxis = /UTC(?:\s*\+?0(?::00)?)?(?![\w+:-])/.test(chart.x_axis_name ?? "");
|
||||
const bareTime = /^(?:\d{4}-\d{2}-\d{2}[T ])?\d{2}:\d{2}(?::\d{2}(?:\.\d{1,9})?)?$/;
|
||||
return { ...chart,
|
||||
title: formatChartDisplayText(chart.title),
|
||||
...(chart.x_axis_name ? { x_axis_name: formatChartDisplayText(chart.x_axis_name.replace(/UTC(?:\s*\+?0(?::00)?)?(?![\w+:-])/g, "UTC+8")) || "时间" } : {}),
|
||||
x_data: chart.x_data.map((label) => formatChartDisplayText(utcAxis && bareTime.test(label) ? `${label} UTC` : label)),
|
||||
series: chart.series.map((series) => ({ ...series, name: formatChartDisplayText(series.name) }))
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user