echart中Y轴的单位变化
原需求:
新需求:
上代码
yAxis: { type: 'value', axisLabel: { // formatter: '{value} MB' formatter: (value) => { // 这里后台传过来的是MB需要选算成B const b = value * 1024 * 1024; return this.GLOBAL.byteConversion(b); } } },
// 字节转换方法,传入byte
function byteConversion(b){
const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
let l = 0, n = parseInt(b, 10) || 0;
while(n >= 1024 && ++l){
n = n/1024;
}
return(n.toFixed(n < 10 && l > 0 ? 1 : 0) + ' ' + units[l]);
}