美文网首页
Vue-cli3 中引入ECharts并实现自适应

Vue-cli3 中引入ECharts并实现自适应

作者: 取个帅气的名字真好 | 来源:发表于2019-05-08 21:29 被阅读0次
效果

1、安装echarts

npm install echarts

2、components/echarts/index.vue

<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
  import echarts from 'echarts'
  require('echarts/theme/macarons') // echarts theme
  import {debounce} from '@/utlis/index.js'
  const animationDuration = 6000
  export default {
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '100%'
      },
      // 数据源
      echartsData: {
        type: Object,
        default: {}
      },
    },
    data() {
      return {
        chart: null,
      }
    },
    watch: {
    },
    //初始化
    mounted() {
      this.initChart()
      this.resizeHandler = debounce(() => {
        if (this.chart) {
          this.chart.resize()
        }
      }, 100)
      window.addEventListener('resize', this.resizeHandler)
    },
  //销毁
    beforeDestroy() { 
      if (!this.chart) {
        return
      }
      window.removeEventListener('resize', this.resizeHandler)
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      initChart() {
        this.chart = echarts.init(this.$el, 'macarons')
        this.chart.setOption(this.echartsData, animationDuration)
      }
    }
  }
</script>

3、utlis/index.js

export function debounce(func, wait, immediate) {
  let timeout, args, context, timestamp, result

  const later = function() {
    // 据上一次触发时间间隔
    const last = +new Date() - timestamp
    // 上次被包装函数被调用时间间隔last小于设定时间间隔wait
    if (last < wait && last > 0) {
      timeout = setTimeout(later, wait - last)
    } else {
      timeout = null
      // 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
      if (!immediate) {
        result = func.apply(context, args)
        if (!timeout) context = args = null
      }
    }
  }

  return function(...args) {
    context = this
    timestamp = +new Date()
    const callNow = immediate && !timeout
    // 如果延时不存在,重新设定延时
    if (!timeout) timeout = setTimeout(later, wait)
    if (callNow) {
      result = func.apply(context, args)
      context = args = null
    }
    return result
  }
}

4、在.vue 中使用 test/index.vue

<template>
  <div id="test">
    <echarts :echartsData="echartsData" /> 
  </div>
</template>

<script>
  import echarts from '@/components/echarts/index'
  export default {
    components: {
      echarts
    },
    data() {
      return {
        echartsData: {
          tooltip: {
            trigger: 'axis',
            axisPointer: { // 坐标轴指示器,坐标轴触发有效
              type: 'shadow' // 默认为直线,可选为:'line' | 'shadow'
            }
          },
          grid: {
            top: 10,
            left: '2%',
            right: '2%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: [{
            type: 'category',
            data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
            axisTick: {
              alignWithLabel: true
            }
          }],
          yAxis: [{
            type: 'value',
            axisTick: {
              show: false
            }
          }],
          series: [{
            name: 'pageA',
            type: 'bar',
            stack: 'vistors',
            barWidth: '60%',
            data: [79, 52, 200, 334, 390, 330, 220],
          }, {
            name: 'pageB',
            type: 'bar',
            stack: 'vistors',
            barWidth: '60%',
            data: [80, 52, 200, 334, 390, 330, 220],
          }, {
            name: 'pageC',
            type: 'bar',
            stack: 'vistors',
            barWidth: '60%',
            data: [30, 52, 200, 334, 390, 330, 220],
          }]
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  #test {
    width: 100%;
    height: 100%;
    background: antiquewhite;
    position: absolute;
    top: 0px;
    bottom: 0px;
  }
</style>

注意:还可以结合配置media来完善自适应。具体请看ECharts官网

相关文章

网友评论

      本文标题:Vue-cli3 中引入ECharts并实现自适应

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