vue中使用echarts时 将echarts做响应式

2 篇文章 0 订阅

子组件: Echartsline

<template>
  <div :style="{width:childWidth,height:childHeight}" class="line-wrap">
    <!--echarts 带面积的折线图-->
    <div id="elines" :style="{width:childWidth,height:childHeight}" ref="odiv"></div>
  </div>
</template>

 props:{
          lineData:Object,
          isClick:Boolean
        },
data:function () {
          return {
            lineCharts:'',
            childWidth:'',
            childHeight:''
          }
        },
//--在watch中监听 宽高变化   
//(宽高有变化的时候会触发watch  然后跟新视图,由于父组件直接传过来的数据不能直接更新子组件视图,
//    所以在子组件用一个新的变量去接收父组件传递过来的参数)
		watch:{
	          lineData:{
	            deep:true,
	            handler(newVla,old){
	              this.childWidth = newVla.divOffset.eWidth;
	              this.childHeight = newVla.divOffset.eHeight;
	              this.initChart(newVla.options,this.isClick);
	            }
	          }
       	 },
       	 
//--在methods中添加方法 

		initChart(options,isClick){
              const _this = this;
              const linesDiv = document.getElementById('elines');
              linesDiv.style.width = this.childWidth;
              linesDiv.style.height = this.childHeight;

              this.$nextTick(()=>{
                _this.lineCharts = _this.$echarts.init(linesDiv);
                _this.lineCharts.setOption(options);

                //--支持点击事件
                if(isClick){
                  _this.lineCharts.on('click',function (params) {
                    const index = params.dataIndex;
                    _this.$emit('clickFn',index);
                  })
                }
                _this.lineCharts.resize();
              })
            }
//------离开页面  注销echarts
		destroyed:function () {
          const _this = this;
          if(_this.lineCharts!==''){
            _this.lineCharts.dispose();
          }
        }

父组件 Parent

//----使用栅格布局  响应式
		<el-col  :sm="24" :md="24" :lg="24" :xl="24" class="first-item">
            <div class="panel-loading-icon" v-show="showLoadingPanel[3]"></div>
	            <div class="tendency panel_content_wrap" style="width: 100%" ref="chartBoxRef" id="ptrendDiv">
	            	 <Echartsline  :lineData="lineData" :isClick="true" @clickFn="trendChartClick"></Echartsline>
	              </div>
            </div>
          </el-col>
//----在data中定义变量
		lineData:{         // 传递给子组件的 数据 + 宽高
	          options:{},
	          divOffset:{
	            eWidth:'',
	            eHeight:''
	          }
	        },
//----在mounted中 使用window.onresize函数     (_throttle函数是节流函数)
		this.$nextTick(()=>{
	        window.onresize = _throttle(function () {
	          Vue.set(_this.lineData.divOffset,'eWidth',(_this.$refs.chartBoxRef.offsetWidth-15) +'px');
	          Vue.set(_this.lineData.divOffset,'eHeight',(_this.$refs.chartBoxRef.offsetHeight-25) +'px');
	        },200);
	      });

//------axios获取到数据后  更新子组件视图
	axios.get(url).then((res)=>{
		Vue.set(this.lineData,'options',{ echarts数据 })
		 Vue.set(this.lineData.divOffset,'eWidth',(this.$refs.chartBoxRef.offsetWidth-15) +'px');
              		Vue.set(this.lineData.divOffset,'eHeight',(this.$refs.chartBoxRef.offsetHeight-25) +'px');
		})
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值