Vue ECharts 二次渲染/重新渲染宽高为0,图表不显示,改变浏览器窗口大小即可显示图表的问题

13 篇文章 0 订阅

ECharts:一个基于 JavaScript 的开源可视化图表库。


前言:这篇博客是基于父组件传值(option)给子组件即echart页面为基础的渲染图标的。话不多说,直接看代码

目录

一、介绍

二、解决办法

tips

 欢迎扫描下方二维码关注VX公众号


一、介绍

1、官方文档:Apache ECharts

Apache EChartsApache ECharts,一款基于JavaScript的数据可视化图表库,提供直观,生动,可交互,可个性化定制的数据可视化图表。icon-default.png?t=N7T8https://echarts.apache.org/zh/index.html

二、解决办法

data() {
      return {
        chart: null,
        drawTiming: null
      };
    },
    watch: {
      options: {
        handler(options) {
          this.$nextTick(() => {
            this.initChart();
          });
        },
        deep: true,
      },
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart();
      });
      window.addEventListener('resize', this.resize);
    },
    beforeUnmount() {
      window.removeEventListener('resize', this.resize);
      this.chart.dispose();
      this.chart = null;
      clearTimeout(this.drawTiming);
      this.drawTiming = null;
    },
    methods: {
      resize() {
        clearTimeout(this.drawTiming);
        this.drawTiming = setTimeout(() => {
          console.log('setTimeout');
          let { clientWidth: width, clientHeight: height } = this.$el.parentElement;
          this.chart.resize({ width, height });
        }, 200);
      },
      initChart() {
        // 初始化echart
        let { clientWidth: width, clientHeight: height } = this.$el.parentElement;
        this.chart.resize({ width, height });
        this.chart.setOption(this.options, true); // 设置true清空echart缓存
      },
    },

tips

        无论是在mouted还是watch初始echarts,一定要加上nextTick,否则很有可能找不到DOM节点,导致图表不能正常显示。

 欢迎扫描下方二维码关注VX公众号

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值