vue 中echart折线自适应

前端时间做一个vue的项目,echart是按需引入的如下:

// 引入 ECharts 主模块
import echarts from 'echarts/lib/echarts'
// 引入折线图
import 'echarts/lib/chart/line'
// 引入提示框和图例组件
import 'echarts/lib/component/tooltip'
import 'echarts/lib/component/legendScroll'

然后发现在缩放浏览器窗口时折线图并不会自适应,费了好一会才解决,记录下来给需要的小伙伴,
解决方法是在mounted里面调用如下方法:

init () {
        //折线图宽高自适应
        const self = this;
        setTimeout(() => {
          window.onresize = function () {
            if(self.$refs.lineChart) {
               self.chart = echarts.init(self.$refs.lineChart);
               self.chart.resize();
            }
          };
        }, 20);
    }
    原文作者:hailey
    原文地址: https://segmentfault.com/a/1190000013385584
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞