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 + '';
|
};
|