Java后端数据连接VUe下Echarts自适应,初始化加载时图表收缩变载,dom元素丢失问题, 网上百度的this.$nextTick、延时加载、监听父元素、都解决不了(如果直接写死宽高就没问题)

 <template>

               <div  class="containers" id="containers" ref="containers">

                    <div ref="mychart" class="echart" id="mychart" :style="myChartStyle" ></div>

               </div>

 </template>

data() {

      return {

           isActivated: false,//判断资源加载

          myChart:null,

         contract_fee_htqd:null,

         crane_num_htqd:null,

          myChartStyle: { float: "left", width: "100%", height: "550px",margin: "0 0px 0 0"  }, //图表样式

         }

},

activated() {

      if(!this.isActivated){//判断资源加载次数,只加载一次

        this.isActivated = true 

            this.topLeft()

          window.addEventListener("resize", () => { this.handleResize()}) //监听图表,自适应     

      }

     window.dispatchEvent(new Event('resize'))   //  向windows发送自定义事件的触发自适应

},

beforeDestroy() {

       window.removeEventListener('resize', this.handleResize()); 

    },

 methods: {

 handleResize(){

         //获取初始化图表名称

          this.myChart = echarts.init(this.$refs.mychart);

          //自适应图表

          this.myChart.resize();

      },

 topLeft() {

            selectTHQD().then(res =>{

               const result  =res.result

                  for(var i=0; i< result.length;i++){

                      this.month_htqd.push(result[i].month);

                      this.contract_fee_htqd.push(result[i].contract_fee);  

                      this.crane_num_htqd.push(result[i].crane_num)                

                  }  

                  const option = {

                     title: {

                        text: '近一年销量/销售额状况'

                      },

                                  tooltip: {

                                      trigger: 'axis',

                                      axisPointer: { type: 'cross' }

                                  },

                                  legend: {},

                                  xAxis: [

                                      {

                                      type: 'category',

                                      axisTick: {

                                          alignWithLabel: true

                                      },

                                      data: this.month_htqd,

                                      name: '月份',

                                      }

                                  ],

                                  yAxis: [

                                      {

                                      type: 'value',

                                      name: '销售金额',

                                      min: 0,

                                      //max: 250,

                                      position: 'right',

                                      axisLabel: {

                                          formatter: '{value} '

                                      }

                                      },

                                      {

                                      type: 'value',

                                      name: '销量',

                                      min: 0,

                                      //max: 25,

                                      position: 'left',

                                      axisLabel: {

                                          formatter: '{value} '

                                      }

                                      }

                                  ],

                                  series: [

                                      {

                                      name: '销售金额',

                                      type: 'bar',

                                      yAxisIndex: 0,

                                      data: this.contract_fee_htqd

                                      },

                                      {

                                      name: '销量',

                                      type: 'line',

                                      smooth: true,

                                      yAxisIndex: 1,

                                      data: this.crane_num_htqd

                                      }

                                  ]

                              };

                  const myChart = echarts.init(document.getElementById("mychart"));

                  myChart.setOption(option);

            })

          },

}

  • 25
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值