• echart——vue封装成公共组件


     

    <!-- 自定义Echarts
    *    options: Object,//数据
    *    theme: String,//主题
    *    initOptions: Object,//初始化
    *    group: String,//分组
    *    autoResize: Boolean,//自适应
    *    modules: Array,//模型
     -->
    <template>
      <div style="height: 100%;100%"></div>
    </template>
    <script>
      // enumerating ECharts events for now
      const ACTION_EVENTS = [
        'legendselectchanged',
        'legendselected',
        'legendunselected',
        'datazoom',
        'datarangeselected',
        'timelinechanged',
        'timelineplaychanged',
        'restore',
        'dataviewchanged',
        'magictypechanged',
        'geoselectchanged',
        'geoselected',
        'geounselected',
        'pieselectchanged',
        'pieselected',
        'pieunselected',
        'mapselectchanged',
        'mapselected',
        'mapunselected',
        'axisareaselected',
        'brush',
        'brushselected'
      ];
      const MOUSE_EVENTS = [
        'click',
        'dblclick',
        'mouseover',
        'mouseout',
        'mousedown',
        'mouseup',
        'globalout'
      ];
      export default {
        props: {
          options: Object,
          theme: String,
          initOptions: Object,
          group: String,
          autoResize: Boolean,
          modules: Array
        },
        data () {
          return {
            chart: null,
          }
        },
        computed: { // 数据驱动,不更新DOM
           {
            cache: false,
            get () {
              return this.chart.getWidth()
            }
          },
          height: {
            cache: false,
            get () {
              return this.chart.getHeight()
            }
          },
          isDisposed: {
            cache: false,
            get () {
              return this.chart.isDisposed()
            }
          }
        },
        watch: {// 通过监听参数:options、group进行重绘
          options: {
            handler (options) {
              if (!this.chart && options) {
                this.echartInit();
              } else {
                this.chart.setOption(this.options, true);
              }
            },
            deep: true
          },
          group: {
            handler (group) {
              this.chart.group = group
            }
          }
        },
        methods: {
          // 设置数据
          mergeOptions (options) {
            this._delegateMethod('setOption', options)
          },
          // 是否重新绘制
          resize (options) {
            this._delegateMethod('resize', options)
          },
          dispatchAction (payload) {
            this._delegateMethod('dispatchAction', payload)
          },
          convertToPixel (finder, value) {
            return this._delegateMethod('convertToPixel', finder, value)
          },
          convertFromPixel (finder, value) {
            return this._delegateMethod('convertFromPixel', finder, value)
          },
          containPixel (finder, value) {
            return this._delegateMethod('containPixel', finder, value)
          },
          showLoading (type, options) {
            this._delegateMethod('showLoading', type, options)
          },
          hideLoading () {
            this._delegateMethod('hideLoading')
          },
          getDataURL (options) {
            return this._delegateMethod('getDataURL', options)
          },
          getConnectedDataURL (options) {
            return this._delegateMethod('getConnectedDataURL', options)
          },
          clear () {
            this._delegateMethod('clear')
          },
          dispose () {
            this._delegateMethod('dispose')
          },
          _delegateMethod (name, ...args) {
            if (!this.chart) {
              return
            }
            return this.chart[name](...args)
          },
          echartInit () { // 按需引入
            if (this.chart) return false;
            import('echarts').then(({ init }) => {
              const { $el, theme, initOptions, group, options, autoResize, _resizeHanlder } = this
              let chart = init($el, theme, initOptions);
              if (group) {
                chart.group = group
              }
              chart.setOption(options, true);
              // 自定义点击事件回调
              ACTION_EVENTS.forEach(event => {
                chart.on(event, params => {
                  this.$emit(event, params)
                })
              });
              // 自定义鼠标事件回调
              MOUSE_EVENTS.forEach(event => {
                chart.on(event, params => {
                  this.$emit(event, params)
                })
              });
              if (autoResize) {
                window.addEventListener('resize', _resizeHanlder, false)
              }
              this.chart = chart;
              console.log('加载成功', chart)
            }).catch(_ => {
              console.log('加载失败')
            })
          },
          _resizeHanlder () {
            window.setTimeout(() => {
              this.chart.resize()
            }, 100)
          }
        },
        mounted () { // 初始化
          if (this.options) {
            this.echartInit()
          }
        },
        beforeDestroy () {
          if (!this.chart) {
            return
          }
          if (this.autoResize) {
            window.removeEventListener('resize', this._resizeHanlder, false)
          }
          this.dispose()
        },
        connect (group) {
          const { chart } = this;
          if (typeof group !== 'string') {
            group = group.map(chart => chart.chart)
          }
          this.chart.connect(group)
        },
        disconnect (group) {
          this.chart.disConnect(group)
        },
        registerMap (...args) {
          this.chart.registerMap(...args)
        },
        registerTheme (...args) {
          this.chart.registerTheme(...args)
        }
      }
    </script>

    转载于:https://www.cnblogs.com/wheatCatcher/p/11201683.html

     
  • 相关阅读:
    TCP 重置攻击的工作原理
    Openshift 4.4 静态 IP 离线安装系列:初始安装
    跟我学SpringCloud | 终篇:文章汇总(持续更新)
    老司机带你玩转面试(4):Redis 高可用之哨兵模式
    老司机带你玩转面试(3):Redis 高可用之主从模式
    老司机带你玩转面试(2):Redis 过期策略以及缓存雪崩、击穿、穿透
    Python 图像处理 OpenCV (14):图像金字塔
    老司机带你玩转面试(1):缓存中间件 Redis 基础知识以及数据持久化
    Python 图像处理 OpenCV (13): Scharr 算子和 LOG 算子边缘检测技术
    Python 图像处理 OpenCV (12): Roberts 算子、 Prewitt 算子、 Sobel 算子和 Laplacian 算子边缘检测技术
  • 原文地址:https://www.cnblogs.com/onesea/p/15838632.html
Copyright © 2020-2023  润新知