1. /**
    2. * @description 获取小于0的小数中的数字
    3. * @param { Number } decimal 小数
    4. * @returns { result, digit } 小数点后不包括0的数字,小数点后的位数
    5. */
    6. const getNumberFromDecimal = (decimal) => {
    7. const str = decimal.toString();
    8. if (typeof decimal !== 'number' || !str.includes('.')) {
    9. // eslint-disable-next-line @typescript-eslint/no-throw-literal
    10. throw '参数不是小数';
    11. }
    12. if (decimal >= 1) {
    13. // eslint-disable-next-line @typescript-eslint/no-throw-literal
    14. throw '参数大于等于1';
    15. }
    16. for (let i = 0; i < str.length; i += 1) {
    17. if (str[i] !== '0' && str[i] !== '.') {
    18. return { result: str.substring(i, str.length), digit: i - 2 };
    19. }
    20. }
    21. return null;
    22. };
    23. const SPLIT_NUMBER = 4; // 分割段数
    24. /**
    25. * @description 根据实际最大值,计算轴的最大值
    26. * @param { String} value 实际的最大值(字符串的数字)
    27. * @returns { Number } 计算后的最大值
    28. */
    29. const computeMax = (value) => {
    30. if (Math.floor(value) === 0) {
    31. // 是小数
    32. // eslint-disable-next-line prefer-const
    33. let { result, digit } = getNumberFromDecimal(Number(value)) as any;
    34. // eslint-disable-next-line @typescript-eslint/no-unused-vars
    35. result = result.substr(0, 2); // 取前两位
    36. // eslint-disable-next-line no-restricted-properties
    37. return Number(Math.ceil(value / SPLIT_NUMBER) * SPLIT_NUMBER) / Math.pow(10, digit - 1 + 2);
    38. }
    39. const roundValue = Math.floor(value).toString();
    40. if (roundValue.length > 2) {
    41. // 两位以上
    42. const topTwo = +roundValue.substr(0, 2);
    43. const len = roundValue.length;
    44. // eslint-disable-next-line no-restricted-properties
    45. return Number(Math.ceil(topTwo / SPLIT_NUMBER) * SPLIT_NUMBER) * Math.pow(10, len - 2);
    46. }
    47. return Number(Math.ceil(value / SPLIT_NUMBER) * SPLIT_NUMBER);
    48. };
    49. const getMaxValue = () => {
    50. const yList1 = _.cloneDeep(data)
    51. .sort((a, b) => b.totalCount - a.totalCount)
    52. .map((item) => item.totalCount);
    53. const yList2 = _.cloneDeep(data)
    54. .sort((a, b) => b.money - a.money)
    55. .map((item) => item.money);
    56. return {
    57. leftMax: yList1[0],
    58. rightMax: yList2[0],
    59. };
    60. };
    61. option.yAxis.forEach((y: any, index) => {
    62. // leftMax, rightMax的值,请自行根据自身数据计算,即:默认配置下的最大值
    63. const { leftMax, rightMax } = getMaxValue();
    64. // index === 0时是左轴,1是右轴
    65. let max = index ? rightMax : leftMax;
    66. max = max === null ? 0 : computeMax(max);
    67. // eslint-disable-next-line no-param-reassign
    68. y.splitNumber = SPLIT_NUMBER;
    69. // eslint-disable-next-line no-param-reassign
    70. y.interval = max / SPLIT_NUMBER;
    71. // eslint-disable-next-line no-param-reassign
    72. y.max = max;
    73. });
    74. chartDom.setOption(option);