echarts 通过源码方法 传入对应data数据获取分割步长值

 通过源码方法获取这里的分割数字长度

/**
 * Quantity of a number. e.g. 0.1, 1, 10, 100
 *
 * @param  {number} val
 * @return {number}
 */
function quantity(val) {
    return Math.pow(10, quantityExponent(val));
}

function quantityExponent(val) {
    return Math.floor(Math.log(val) / Math.LN10);
}

/**
 * find a “nice” number approximately equal to x. Round the number if round = true,
 * take ceiling if round = false. The primary observation is that the “nicest”
 * numbers in decimal are 1, 2, and 5, and all power-of-ten multiples of these numbers.
 *
 * See "Nice Numbers for Graph Labels" of Graphic Gems.
 *
 * @param  {number} val Non-negative value.
 * @param  {boolean} round
 * @return {number}
 */
function nice(val, round) {
  console.log('get real splitNum==1111===>', 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.
  const nice = exponent >= -20 ? +val.toFixed(exponent < 0 ? -exponent : 0) : val;
    return nice;
}

  

使用方法

// val 传递的是: (你当前data数组中的最大值 / (splitNumber | 5))
// splitNumber 是你写的分割段数 如果没有设置 则使用5相除 
// round 传false


// 例如 我这里传24
nice(24,false); // 返回20, 其中20 就是 步长值

// 如果是堆叠图表 需要进行数据相加后 获取相加后的数组的最大值 然后进行计算
nice(37, false); //

  

猜你喜欢

转载自www.cnblogs.com/MainActivity/p/11511395.html
今日推荐