Echarts 动态更改数据

<template>
  <div>
    <div     
      :class="['chart',{'bigClass':chartClass}]"
      :id="id"
      ref="charts"
    ></div>
  </div>
</template>
<script>
var echarts = require('echarts');
// import echarts from 'echarts'
export default {
  name: 'lineChart',
  data () {
    return {
      myChart: null,
    }
  },
  props: {
    id: {
      type: String,
      default: ''
    },
    chartClass: {
      type: Boolean,
      default: false
    },
    options: {
      type: Object,
      default: {}
    },
    h: {
      type: Number,
      default: 130

    }
  },
  mounted () {
    // console.log(9)
    this.$nextTick(() => {
      // console.log(this.myChart)
      if (this.myChart != null && this.myChart != "" && this.myChart != undefined) {
        this.myChart.dispose();
      }

      this.drawLine()
      window.addEventListener('resize', this.handleWindowResize);

    })
    console.log('echart')

  },
  methods: {
    drawLine () {
      // console.log(8)
      // 基于准备好的dom,初始化echarts实例
      this.myChart = echarts.init(document.getElementById(this.id))
      this.myChart.setOption(this.options)
    },
    /**
    * 当窗口缩放时,echart动态调整自身大小
    */
    handleWindowResize () {
      if (!this.myChart) return;
      this.myChart.resize();
    }
  },
  watch: {
    /* 如果图表数据是后台获取的,监听父组件中的数据变化,重新触发Echarts */
    options: {
      deep: true,
      handler (val) {
        console.log( this.chartClass)
        console.log(val)
        // this.setOptions(val)
        this.myChart.setOption(val, true)
      }
    },
  },
  beforeDestroy () {
    if (!this.myChart) {
      return
    }
    /* 释放图表实例 */
    this.myChart.dispose()
    /* dispose 会释放内部占用的一些资源和事件绑定,但是解除实例的引用我们是做不到的,所以需要重新赋值为null */
    this.myChart = null
  },

}
</script>
<style scoped>
.chart {
  height: 200px;
}
.chart.bigClass {
  height: 600px;
}
</style>



  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值