echarts markline
超出图表显示区域无法显示 markline value <= 1.5
时不显示
// 方法1:(最初找的的解决方法)
// 获取幂指数
// val是非负数
// 详见 node_modules/echarts/lib/util/number.js
function quantityExponent(val) {
if (val === 0) {
return 0;
}
var exp = Math.floor(Math.log(val) / Math.LN10);
/**
* exp is expected to be the rounded-down result of the base-10 log of val.
* But due to the precision loss with Math.log(val), we need to restore it
* using 10^exp to make sure we can get val back from exp. #11249
*/
if (val / Math.pow(10, exp) >= 10) {
exp++;
}
return exp;
}
function nice(val, round) {
var exponent = quantityExponent(val);
var exp10 = Math.pow(10, exponent);
var f = val / exp10; // 1 <= f < 10
var nf;
if (round) {
if (f < 1.5) {
nf = 1;
} else if (f < 2.5) {
nf = 2;
} else if (f < 4) {
nf = 3;
} else if (f < 7) {
nf = 5;
} else {
nf = 10;
}
} else {
if (f < 1) {
nf = 1;
} else if (f < 2) {
nf = 2;
} else if (f < 3) {
nf = 3;
} else if (f < 5) {
nf = 5;
} else {
nf = 10;
}
}
val = nf * exp10; // Fix 3 * 0.1 === 0.30000000000000004 issue (see IEEE 754).
// 20 is the uppper bound of toFixed.
return exponent >= -20 ? +val.toFixed(exponent < 0 ? -exponent : 0) : val;
}
// 方法1:使用
max: extend => {
const max = extend.max > earlyAlarm.value ? extend.max : earlyAlarm.value
return nice(max)
},
min: function(extend) {
if (earlyAlarm && earlyAlarm.value && earlyAlarm.value < 0) {
// 拿到最小值后先转为正数,借助nice函数得到niceNumeral后再转为负数
const min = ~(extend.min > earlyAlarm.value ? earlyAlarm.value : extend.min)
return ~nice(min)
}
// 返回 null/undefined 来表示“自动计算最大值”保证坐标轴刻度的均匀分布。
return null
},
// 这里基本都能显示出来markline了,但是图表看起来会很奇怪,例如当y轴区间为[-1.5, 9999]时
方法2:
// 在series.data的最后面push markline的value
// pushData.data.push(+source.earlyAlarm.value)
pushData.data = pushData.data.concat([0, 0, +source.earlyAlarm.value]) // 解决页面缩放后折线图最后一位显示异常问题