wujingjing
2024-08-29 19b778d2d04bed31ce2e1f167c6ff2fda9906421
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
export const calMax = (arr) => {
    const maxA = Math.max(...arr); // 最大值A
    const ceilMaxA = Math.ceil(maxA);
    return Math.max(ceilMaxA, 10); // 为了防止maxA数据为0时,Y轴不显示,给个默认最大值10
};
 
/**留白配置 */
export const normalBoundGap = ['0', '17%'];
 
/**
 *
 * @param arr
 * @param zeroStart 默认兜底取 0,为false 时,取数据中最小的
 * @returns
 */
export const calMin = (arr) => {
    const minA = Math.min(...arr);
    const floorMinA = Math.floor(minA);
    const min = floorMinA;
    return min;
};
 
export const calInterval = (max, min, spaceNum = 5) => {
    return Math.round((max - min) / spaceNum);
};
 
/**
 * 根据坐标点的值,设置合适的坐标 max、min 和 interval 配置
 * @param axisLineSection 轴线分段数,不够 interval 会加一个
 */
export const calMaxMinIntervalByChartData = (seriesData: number[], axisLineSection = 5) => {
    if (!seriesData || seriesData.length === 0)
        return {
            max: undefined,
            min: undefined,
            interval: undefined,
        };
 
    //#region ====================== 轴最小值计算 ======================
    const minVal = Math.floor(Math.min(...seriesData));
 
    //#endregion
 
    //#region ====================== 轴最大值计算 ======================
    let maxVal = Math.ceil(Math.max(...seriesData));
    const len = maxVal - minVal;
    if (len === 0) {
        maxVal += 10;
        // 轴最大值要多出 1/6 长度
    } else {
        const spanVal = Math.floor((1 / 6) * len);
        maxVal += spanVal === 0 ? 1 : spanVal;
    }
    //#endregion
 
    //#region ====================== 轴间距计算 ======================
    const curLen = maxVal - minVal;
    let interval = Math.floor(curLen / axisLineSection);
    if (interval === 0) {
        interval = 1;
    } else {
        // 余数
        const restVal = curLen % axisLineSection;
        // 有余数就补一个
        if (restVal !== 0) {
            maxVal += restVal;
        }
    }
    //#endregion
 
    return {
        max: maxVal,
        min: minVal,
        interval,
    };
};
 
//根据最大值最小值改变y轴的刻度尺
export const getChartMaxMinInterval = (max, min, splitNumber) => {
    //计算echarts 的 max ,min ,interval
    const getMaxMinNumber = (n, l) => {
        const a1 = Math.floor(Math.log(n) / Math.LN10);
        let b;
        if (l) {
            a1 < 2
                ? (b =
                        n / Math.pow(10, a1) - parseInt(n / Math.pow(10, a1)) > 0.5
                            ? Math.round(n / Math.pow(10, a1)) * Math.pow(10, a1)
                            : (parseInt(n / Math.pow(10, a1)) + 0.5) * Math.pow(10, a1))
                : (b = Math.ceil(n / Math.pow(10, 1)) * Math.pow(10, 1));
        } else {
            a1 < 2
                ? (b =
                        n / Math.pow(10, a1) - parseInt(n / Math.pow(10, a1)) > 0.5
                            ? (parseInt(n / Math.pow(10, a1)) + 0.5) * Math.pow(10, a1)
                            : Math.floor(n / Math.pow(10, a1)) * Math.pow(10, a1))
                : (b = Math.floor(n / Math.pow(10, 1)) * Math.pow(10, 1));
        }
        return l ? (-20 <= a1 ? +b.toFixed(a1 < 0 ? -a1 + 1 : 0) : b) : b;
    };
    let interval = 0;
    if ((max - min) % splitNumber != 0) {
        interval = getMaxMinNumber((max - min) / splitNumber, 1);
        max = parseFloat((parseFloat((interval * splitNumber).toFixed(12)) + min).toFixed(12)); //解决小数精度一般问题,极端问题并不能解决。
        min = min;
    } else {
        interval = (max - min) / splitNumber;
        min = min;
        max = max;
    }
    return { max: max, min: min, interval: interval };
};
 
const MyRound = (v, num) => {
    if (num == 1) return Math.round(v * 10) / 10.0;
    else return Math.round(v * 100) / 100.0;
};
 
export const axisLabelFormatter = (value, index) => {
    if (value >= 1000000) {
        return value / 1000000 + 'M';
    }
    if (value >= 10000) {
        return value / 10000 + 'W';
    }
    if (value >= 1000) {
        return value / 1000 + 'K';
    }
    return value + '';
};