Echart chart self-adaptive size

  1. Adaptive window size
     mounted () {
        this.$nextTick(() => {
          this.init()
        })
        window.addEventListener('resize', this.changeSize)
    
      },
      beforeDestroy () {
        window.removeEventListener('resize', this.changeSize)
        if (this.myChart) {
          this.myChart.dispose()
        }
      },
      methods: {
        changeSize () {
          if (document.getElementById(this.idLine)) {
            this.$echarts.init(document.getElementById(this.idLine)).resize()
          }
        },
        init () {
          if (this.myChart) {
            this.myChart.dispose()
          }
          this.myChart = this.$echarts.init(document.getElementById(this.idLine))
          let option = {
            ...
          }
          this.myChart.setOption(option)
        }
    }

  2. The size of the parent box is adaptive
     props: {
        idLine: {
          type: String,
          default: 'line'
        },
        data: {
          type: Array,
          default: [
          ]
        },
       //父组件向子组件传值,判断是否更新
        updata: {
          type: Boolean,
          default: false
        }
      },
      watch: {
        data (val) {
          console.log('折线图', val)
          this.init()
        },
        updata (val) {
          console.log('更新', val)
          this.changeSize()
        }
      },
      methods: {
        changeSize () {
          if (document.getElementById(this.idLine)) {
            this.$echarts.init(document.getElementById(this.idLine)).resize()
          }
        },
      }

Guess you like

Origin blog.csdn.net/weixin_51258044/article/details/131706732