tanghaolin
2025-04-17 6716b2a7ecc3ce3745c17f8361c781abd63d40d7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// 数值格式化函数
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;
};