美文网首页
echarts 根据时间跨度计算时间轴间隔

echarts 根据时间跨度计算时间轴间隔

作者: 美食家儿菜 | 来源:发表于2020-03-31 23:38 被阅读0次

    最最直接,也最根本的计算方法,应该是跟 平均数相关,确定要显示多少个数据,然后总的时间 / 要显示的个数,是这么的算法吧。

    看到网上的另外一种方法

    class CalculateScale {
      private min;
      private max;
      private maxTicks = 40;
      private tickSpacing;
      private range;
      private fitMin;
      private fitMax;
    
      constructor (start, end) {
        this.min = start;
        this.max = end;
        this.calculate();
      }
    
      getFitNumber = (range) => {
        let exponent;
        let fraction;
        let fitFraction;
        exponent = Math.floor(Math.log10(range));
        fraction = range / Math.pow(10, exponent);
        if (fraction <= 1) {
          fitFraction = 1;
        } else if (fraction <= 2) {
          fitFraction = 2;
        } else if (fraction <= 5) {
          fitFraction = 5;
        } else {
          fitFraction = 10;
        }
        return fitFraction * Math.pow(10, exponent);
      };
    
      calculate = () => {
        this.range = this.getFitNumber(this.max - this.min);
        this.tickSpacing = this.getFitNumber(this.range / (this.maxTicks - 1));
        this.fitMin = Math.floor(this.min / this.tickSpacing) * this.tickSpacing;
        this.fitMax = Math.ceil(this.max / this.tickSpacing) * this.tickSpacing;
      };
    
      setMinMaxPoint = (min, max) => {
        this.min = min;
        this.max = max;
        this.calculate();
      };
    
      setMaxTicks = (maxTicks) => {
        this.maxTicks = maxTicks;
        this.calculate();
      };
    
      getTickSpacing = () =>  {
        return this.tickSpacing;
      };
    
      getFitMin = () => {
        return this.fitMin;
      };
    
      getFitMax = () => {
        return this.fitMax;
      };
    }
    
    export default CalculateScale;
    

    后面可以继续再想想这种方法

    相关文章

      网友评论

          本文标题:echarts 根据时间跨度计算时间轴间隔

          本文链接:https://www.haomeiwen.com/subject/miweiqtx.html