更新:js容错算法改善,增加2份Ai报表

This commit is contained in:
杨水淼
2025-07-22 16:16:59 +08:00
parent bd4f92d465
commit bd82783ddb
6 changed files with 2379 additions and 10 deletions
+13 -9
View File
@@ -92,7 +92,7 @@ function pickData(data, dateKey, valueKey,fixed=2) {
return data.map(item => ({
value: [
item[dateKey].slice(0, 4) + '-' + item[dateKey].slice(4, 6) + '-' + item[dateKey].slice(6, 8), // 格式化日期 yyyymmdd 转为 yyyy-mm-dd
item[valueKey].toFixed(fixed) // 保留两位小数
item[valueKey] === null || item[valueKey] === undefined || item[valueKey] === '' ? '' : item[valueKey].toFixed(fixed)
]
}));
}
@@ -112,9 +112,13 @@ function calculateStats(data,fixed=2) {
let min = Infinity;
let sum = 0;
let last = null;
let count = 0;
for (const item of data) {
const value = parseFloat(item.value[1]); // 提取 value 数组中的第二个元素(数值部分)
const valueStr = item.value[1];
if (valueStr === '' || valueStr === null || valueStr === undefined) continue;
const value = parseFloat(valueStr);
if (isNaN(value)) continue;
if (value > max) {
max = value;
@@ -123,15 +127,15 @@ function calculateStats(data,fixed=2) {
min = value;
}
sum += value;
last = value; // 更新最后一个值
last = value;
count++;
}
const avg = sum / data.length;
const avg = count > 0 ? sum / count : null;
return {
max: max.toFixed(fixed),
min: min.toFixed(fixed),
avg: avg.toFixed(fixed),
last: last.toFixed(fixed)
max: max !== -Infinity ? max.toFixed(fixed) : null,
min: min !== Infinity ? min.toFixed(fixed) : null,
avg: avg !== null ? avg.toFixed(fixed) : null,
last: last !== null ? last.toFixed(fixed) : null
};
}