echart使用小记

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

<script>
import echarts from 'echarts'
import { debounce } from '@/utils'
export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    data: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    return {
      chart: null,
      sidebarElm: null,
      charData: [],
      timer: null
    }
  },
  computed: {
    xArr() {
      const arr = []
      this.data.forEach(item => {
        arr.push(item.time)
      })
      return arr
    },
    yArr() {
      const arr = []
      this.data.forEach(item => {
        arr.push(item.retailerNum)
      })
      return arr
    }
  },
  watch: {
    data: {
      handler(val, oldVal) {
        this.initChart()
      }
    }
  },
  mounted() {
    this.initChart()
    // this.getData()
    // this.timer = setInterval(this.getData, 900000)
    if (this.autoResize) {
      this.__resizeHandler = debounce(() => {
        if (this.chart) {
          this.chart.resize()
        }
      }, 10)
      window.addEventListener('resize', this.__resizeHandler)
    }
    // 监听侧边栏的变化
    this.sidebarElm = document.getElementsByClassName('sidebar-container')[0]
    this.sidebarElm && this.sidebarElm.addEventListener('transitionend', this.sidebarResizeHandler)
  },
  beforeDestroy() {
    clearInterval(this.timer)
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    getData() {
      this.initChart()
      // getLineCount({ circleId: this.f_circle_id }).then(res => {
      //   this.charData = res.data
      //   this.initChart()
      // })
    },
    sidebarResizeHandler(e) {
      if (e.propertyName === 'width') {
        this.__resizeHandler()
      }
    },
    initChart() {
      this.chart = echarts.init(this.$el)
      this.chart.setOption({
        // dataZoom: {
        //   type: 'slider',
        //   xAxisIndex: [0],
        //   start: 1,
        //   end: 100,
        //   backgroundColor: 'rgba(0,0,0,0.1)',
        //   fillerColor: 'rgba(0,0,0,0.1)',
        //   borderColor: 'none',
        // },
        dataZoom: {
          // type: 'slider',
          type: 'inside',
          xAxisIndex: [0],
          start: 100 - 650 / (this.xArr.length),
          end: 100
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器,坐标轴触发有效
            type: 'line', // 默认为直线,可选为:'line' | 'shadow'
            lineStyle: {
              type: 'dashed',
              color: '#bababa'
            }
          }
        },
        title: {
          text: '当前活跃人数',
          left: '1%',
          top: '4%',
          textStyle: {
            color: '#333',
            fontSize: 16,
            textShadow: '0 0 1px #333'
          }
        },
        grid: {
          top: '30%',
          left: '3%',
          right: 50,
          bottom: '5%',
          containLabel: true
        },
        yAxis: [{
          name: '人数',
          nameTextStyle: {
            color: '#333',
            fontSize: '14'
          },
          type: 'value',
          min: 0,
          max: function(value) {
            return value.max + 1
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: '#c8c8c8'
            }
          },
          axisTick: {
            show: true,
            lineStyle: {
              color: '#c8c8c8'
            }
          },
          splitLine: {
            show: false
          },
          axisLabel: {
            color: '#333'
          }
        }
        ],
        xAxis: [{
          type: 'category',
          boundaryGap: false,
          // data: ['0-8时', '9时', '10时', '11时', '12时', '13时', '14时', '15时', '16时', '17时', '18-24时'],
          data: this.xArr,
          axisLine: {
            show: true,
            lineStyle: {
              color: '#e9e9e9'
            }
          },
          axisTick: {
            show: true,
            lineStyle: {
              color: '#e9e9e9'
            }
          },
          splitLine: {
            show: false,
            lineStyle: {
              color: '#536284'
            }
          },
          axisLabel: {
            color: '#333',
            showMaxLabel: true,
            showMinLabel: true,
            min: 1
          }
        }],
        series: [{
          name: '在线人数',
          barWidth: '30',
          data: this.yArr,
          // data: [14, 56, 37, 6, 33, 34, 56, 34, 38, 22, 80],
          type: 'line',
          smooth: true,
          symbol: 'none',
          label: {
            normal: {
              show: false,
              position: 'top'
            }
          },
          markPoint: {
            data: [
              { type: 'max', name: '最大值' },
              { type: 'min', name: '最小值' }
            ],
            symbolOffset: [0, -10]
          },
          areaStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
              offset: 0,
              color: '#0033BE'
            }, {
              offset: 1,
              color: '#D583DF'
            }])
          },
          itemStyle: {
            normal: {
              color: '#365BC2',
              lineStyle: {
                width: 1 // 设置线条粗细
              }
            }
          },
          animationEasing: 'cubicInOut',
          animationDuration: 1500
        }
        ]
      })
    }
  }
}
</script>
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
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值