Echarts实现窗口变化图表同步大小

思路:在data里获取当前屏幕的宽和建立一个定时器,在mounted钩子函数里监听窗口大小

           watch监听data里的当前屏幕然后进行调用methods方法 销毁组件的时候清除定时器

tip:在methods里加上resize()方法

<div id="Column"></div>
 data(){
     screenWidth:document.body.clientWidth, // 屏幕宽度
     timer: null, //设置一个定时器
 },
 mounted () {
    // 监听窗口大小
    window.onresize = () => {
      return (() => {
        this.screenWidth = document.body.clientWidth
      })()
    }
},
watch:{
    screenWidth(val) {
      // 为了避免频繁触发resize函数导致页面卡顿
      if (!this.timer) {
        // 监听screenWidth值改变,就将其重新赋给screenWidth
        this.screenWidth = val;
        this.timer = true;
        let that = this;
        setTimeout(function () {
          that.setCylinder();  //(调用图表的方法)
          that.timer = false;
        }, 300);
      }
    },
}
mounted(){
    //Echarts实例
    setCylinder() {
    var CylinderEcharts = this.$echarts.init(document.getElementById("Column"));
    ………………
    CylinderEcharts.setOption(CylinderOption);
    CylinderEcharts.resize();
  },
}
 // 销毁定时器
  destroyed() {
    clearInterval(this.timer);
    this.timer= null;
  },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值