lineChart.vue 3.02 KB
<template>
  <div :class="className" :style="{height:height,width:width}"></div>
</template>

<script>
import echarts from 'echarts'

import {debounce} from '../utils/debounce'

export default {
  name: 'lineChart',
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null,
      sidebarElm: null
    }
  },
  watch: {
    chartData: {
      deep: true,
    }
  },
  mounted() {
    this.initChart()
    if (this.autoResize) {
      this.__resizeHandler = debounce(() => {
        if (this.chart) {
          this.chart.resize()
        }
      }, 100)
      window.addEventListener('resize', this.__resizeHandler)
    }
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    if (this.autoResize) {
      window.removeEventListener('resize', this.__resizeHandler)
    }

    this.chart.dispose()
    this.chart = null
  },
  methods: {
    setOptions({xData, yData} = {}) {
      this.chart.setOption({
        tooltip: {
          trigger: 'axis'
        },
        color:['#FFB130','#01AEFE'],
        legend: {
          data:['出场','入场'],
          bottom:'2%',
          textStyle: {
            color: '#59AAF7'
          }
        },
        grid: {
          top:'8%',
          left: '3%',
          right: '4%',
          bottom: '14%',
          containLabel: true
        },

        xAxis: {
          axisLabel : {
            textStyle: {
              color: '#fff'
            }
          },
          axisLine:{
            lineStyle:{
              color:'rgba(89,170,247,.3)'
            }
          },
          axisTick: {
            show: false
          },
          splitLine: {
            show: false
          },
          data: ['周一','周二','周三','周四','周五','周六','周日']
        },
        yAxis: {
          type: 'value',
          axisLabel : {
            formatter: '{value}',
            textStyle: {
              color: '#fff'
            }
          },
          axisLine:{
            lineStyle:{
              color:'rgba(89,170,247,.3)'
            }
          },
          axisTick: {
            show: false
          },
          splitLine: {
            show: false
          },
          splitNumber:3
        },
        series: [
          {
            name:'出场',
            type:'line',
            data:[120, 132, 101, 134, 90, 230, 210],
            lineStyle: {
              color: '#FFB130'
            }
          },
          {
            name:'入场',
            type:'line',
            data:[220, 182, 191, 234, 290, 330, 310],
            lineStyle: {
              color: '#08B9FC'
            }
          }
        ]

      })
    },
    initChart() {
      this.chart = echarts.init(this.$el)
      this.setOptions(this.chartData)
    }
  }
}
</script>