美文网首页
echart 在vue中的封装自适应

echart 在vue中的封装自适应

作者: 宏_4491 | 来源:发表于2021-01-30 23:00 被阅读0次

    在项目中使用echart,使echart 自适应外层div 的大小

    1 、在components 里面新建一个 echarts 文件夹index.vue,然后混入动态计算宽高的resize.js 方法。

    image.png

    index.vue

    <template>
      <div>
        <div :id="echarts" :style="styles" />
      </div>
    </template>
    
    <script>
    import echarts from 'echarts'
    export default {
      name: 'Index',
      props: {
        styles: {
          type: Object,
          default: null
        },
        optionData: {
          type: Object,
          default: null
        }
      },
      data() {
        return {
          myChart: null
        }
      },
      computed: {
        echarts() {
          return 'echarts' + Math.ceil(Math.random() * 100)
        }
      },
      watch: {
        optionData: {
          handler(newVal, oldVal) {
            this.handleSetVisitChart()
          },
          deep: true // 对象内部属性的监听,关键。
        }
      },
      mounted: function() {
        const vm = this
        vm.$nextTick(() => {
          vm.handleSetVisitChart()
          window.addEventListener('resize', this.wsFunc)
        })
      },
      beforeDestroy() {
        window.removeEventListener('resize', this.wsFunc)
        if (!this.myChart) {
          return
        }
        this.myChart.dispose()
        this.myChart = null
      },
      methods: {
        wsFunc() {
          this.myChart.resize()
        },
        handleSetVisitChart() {
          this.myChart = echarts.init(document.getElementById(this.echarts))
          let option = null
          option = this.optionData
          // 基于准备好的dom,初始化echarts实例
          this.myChart.setOption(option, true)
        }
      }
    }
    </script>
    
    <style scoped>
    
    </style>
    

    resize.js

    import { debounce } from '@/utils'
    
    export default {
      data() {
        return {
          $_sidebarElm: null,
          $_resizeHandler: null
        }
      },
      mounted() {
        this.initListener()
      },
      activated() {
        if (!this.$_resizeHandler) {
          // avoid duplication init
          this.initListener()
        }
    
        // when keep-alive chart activated, auto resize
        this.resize()
      },
      beforeDestroy() {
        this.destroyListener()
      },
      deactivated() {
        this.destroyListener()
      },
      methods: {
        // use $_ for mixins properties
        // https://vuejs.org/v2/style-guide/index.html#Private-property-names-essential
        $_sidebarResizeHandler(e) {
          if (e.propertyName === 'width') {
            this.$_resizeHandler()
          }
        },
        initListener() {
          this.$_resizeHandler = debounce(() => {
            this.resize()
          }, 100)
          window.addEventListener('resize', this.$_resizeHandler)
    
          this.$_sidebarElm = document.getElementsByClassName('sidebar-container')[0]
          this.$_sidebarElm && this.$_sidebarElm.addEventListener('transitionend', this.$_sidebarResizeHandler)
        },
        destroyListener() {
          window.removeEventListener('resize', this.$_resizeHandler)
          this.$_resizeHandler = null
    
          this.$_sidebarElm && this.$_sidebarElm.removeEventListener('transitionend', this.$_sidebarResizeHandler)
        },
        resize() {
          const { chart } = this
          chart && chart.resize()
        }
      }
    }
    

    2、组件的使用

    <template>
      <echarts-from :option-data="optionData" :styles="style" />
    </template>
    
    <script>
    import echartsFrom from '@/components/echarts/index'
    //组册组件
    export default {
    components: { echartsFrom },
    data(){
           retrun{
                      style: { height: '281px' },  //默认高度
                      optionData: {}, //echart  的配置数据
       }
    }
    </script>
    //导入组件
    
    
    
    
    

    相关文章

      网友评论

          本文标题:echart 在vue中的封装自适应

          本文链接:https://www.haomeiwen.com/subject/mfqltltx.html