修改测试问题
This commit is contained in:
@@ -161,6 +161,41 @@ export const exportCSV = (title: object, data: any, filename: string) => {
|
||||
URL.revokeObjectURL(link.href)
|
||||
}
|
||||
|
||||
/**
|
||||
* 将多条折线按时间对齐合并为 CSV 行(避免各 series 长度不一致时按索引对齐丢数据)
|
||||
*/
|
||||
export const buildSeriesCsvData = (seriesList: Array<{ name: string; data?: any[] }>) => {
|
||||
if (!seriesList?.length) {
|
||||
return { titles: [], rows: [] }
|
||||
}
|
||||
const titles = seriesList.map(s => s.name)
|
||||
const timeMap = new Map<string, (string | number | null)[]>()
|
||||
|
||||
seriesList.forEach((series, seriesIndex) => {
|
||||
series.data?.forEach((point: any) => {
|
||||
const time = point?.[0]
|
||||
if (!time) return
|
||||
const timeKey = String(time)
|
||||
if (!timeMap.has(timeKey)) {
|
||||
timeMap.set(timeKey, [timeKey, ...Array(seriesList.length).fill(null)])
|
||||
}
|
||||
const row = timeMap.get(timeKey)!
|
||||
row[seriesIndex + 1] = point[1] ?? null
|
||||
})
|
||||
})
|
||||
|
||||
const rows = Array.from(timeMap.values()).sort(
|
||||
(a, b) => new Date(a[0] as string).getTime() - new Date(b[0] as string).getTime()
|
||||
)
|
||||
return { titles, rows }
|
||||
}
|
||||
|
||||
export const exportSeriesCSV = (seriesList: Array<{ name: string; data?: any[] }>, filename: string) => {
|
||||
const { titles, rows } = buildSeriesCsvData(seriesList)
|
||||
if (!rows.length) return
|
||||
exportCSV(titles, rows, filename)
|
||||
}
|
||||
|
||||
/**
|
||||
* 补全时间序列数据中缺失的条目
|
||||
* @param rawData 原始数据,格式为 [["时间字符串", "数值", "单位", "类型"], ...]
|
||||
|
||||
Reference in New Issue
Block a user