Files
admin-govern/src/utils/echartMethod.ts

74 lines
2.2 KiB
TypeScript
Raw Normal View History

2024-10-29 16:32:32 +08:00
const dataProcessing = (arr: any[]) => {
return arr
2024-12-25 10:53:07 +08:00
.filter(item => typeof item == 'number' || (typeof item == 'string' && !isNaN(parseFloat(item))))
.map(item => (typeof item == 'number' ? item : parseFloat(item)))
2024-10-29 16:32:32 +08:00
}
2024-10-22 10:50:47 +08:00
// 处理y轴最大最小值
2024-09-29 18:15:54 +08:00
export const yMethod = (arr: any) => {
2024-12-25 10:53:07 +08:00
const numList = dataProcessing(arr);
const maxValue = Math.max(...numList);
const minValue = Math.min(...numList);
const calculateBoundary = (value: number, base: number) => {
return Math[value > 0? 'ceil' : 'floor'](value / base) * base;
};
let max: number;
let min: number;
2024-10-22 15:55:24 +08:00
if (maxValue > 1000 || minValue < -1000) {
2024-12-25 10:53:07 +08:00
max = calculateBoundary(maxValue, 100);
min = minValue == 0? 0 : calculateBoundary(minValue, 100);
} else if (maxValue < 60 && minValue > 40) {
max = 60;
min = 40;
} else if (maxValue == minValue) {
if (maxValue < 10 && minValue > 0) {
max = calculateBoundary(maxValue, 10);
min = calculateBoundary(minValue, 10);
} else if (maxValue!== 0 && minValue!== 0) {
max = calculateBoundary(maxValue / 10 + 1, 10);
min = calculateBoundary(minValue / 10 - 1, 10);
2024-10-14 20:15:54 +08:00
}
2024-09-29 18:15:54 +08:00
} else {
2024-12-25 10:53:07 +08:00
max = calculateBoundary(maxValue, 10);
min = calculateBoundary(minValue, 10);
2024-09-29 18:15:54 +08:00
}
2024-10-10 09:56:34 +08:00
if (maxValue > 0 && maxValue < 1) {
2024-12-25 10:53:07 +08:00
max = 1;
2024-10-14 20:15:54 +08:00
}
2024-12-25 10:53:07 +08:00
if (max == 0 && minValue > -1 && minValue < 0) {
min = -1;
}
return [min, max];
};
2024-10-22 15:55:24 +08:00
/**
* title['A相','B相',]
* data[[1,2],[3,4]]
*/
2024-10-22 10:50:47 +08:00
// 导出csv文件
2024-10-22 15:55:24 +08:00
const convertToCSV = (title: object, data: any) => {
// console.log('🚀 ~ convertToCSV ~ data:', data)
2024-10-22 15:55:24 +08:00
let csv = ''
// 添加列头
csv += ',' + title.join(',') + '\n'
// 遍历数据并添加到CSV字符串中
data?.map(item => {
csv += item.join(',') + '\n'
})
return csv
}
export const exportCSV = (title: object, data: any, filename: string) => {
const csv = convertToCSV(title, data)
const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' })
const link = document.createElement('a')
link.href = URL.createObjectURL(blob)
link.download = filename
link.click()
// 释放URL对象
URL.revokeObjectURL(link.href)
2024-10-22 10:50:47 +08:00
}