// 数值格式化函数
|
export const formatNumber = (value: number) => {
|
const units = [
|
{ value: 1e8, symbol: 'b' },
|
{ value: 1e4, symbol: 'w' },
|
{ value: 1e3, symbol: 'k' },
|
];
|
|
for (const unit of units) {
|
if (Math.abs(value) >= unit.value) {
|
return (value / unit.value).toFixed(1) + unit.symbol;
|
}
|
}
|
return value.toString();
|
};
|
|
// 处理数值格式化的配置
|
export const processNumberFormat = (options: any) => {
|
if (!options) return options;
|
|
const newOptions = { ...options };
|
|
// 处理 yAxis 配置
|
if (newOptions.yAxis && newOptions.yAxis.type === 'value') {
|
if (!newOptions.yAxis.axisLabel) {
|
newOptions.yAxis.axisLabel = {};
|
}
|
newOptions.yAxis.axisLabel.formatter = (value: number) => formatNumber(value);
|
}
|
|
// 处理 tooltip 配置
|
if (!newOptions.tooltip) {
|
newOptions.tooltip = {};
|
}
|
const originalFormatter = newOptions.tooltip.formatter;
|
newOptions.tooltip.formatter = (params: any) => {
|
if (originalFormatter) {
|
return originalFormatter(params);
|
}
|
if (Array.isArray(params)) {
|
return (
|
params[0].axisValueLabel +
|
'<br/>' +
|
params.map((item) => `${item.marker} ${item.seriesName || item.name}: ${formatNumber(item.value)}`).join('<br/>')
|
);
|
}
|
return `${params.name}: ${formatNumber(params.value)}`;
|
};
|
|
return newOptions;
|
};
|