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

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'

const animationDuration = 6000

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '200px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  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({
        tooltip:{
          trigger:'item',
          triggerOn: 'click',
          // formatter: '{b}的成绩是{c}分',
          formatter:(argv) => {
            return `${argv.name}的入库是${argv.data}`
          }
        },
        grid: {
          top: 10,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        }, legend:{
          data:['出库','入库']
        },
        xAxis:{ //X轴
          type:'category', //类目名称
          data: ['2023-04-30','2023-05-01','2023-05-02','2023-05-03','2023-05-04','2023-05-05','2023-05-06','2023-05-07','2023-05-08']
        },
        yAxis:{
          type:'value' //数值轴
        },series:[
          {
            name:'出库',
            type:'bar',
            itemStyle : { normal: {label : {show: true, position: 'top'}}},
            barGap:'0',
            data: [,1196,196,0,16,4,1,1383,0,6]
          },
          {
            name:'入库',
            type:'bar',
            barGap:'0',
            itemStyle : { normal: {label : {show: true, position: 'top'}}},
            data:[1,6100,4010,0,53,4,1,3032,0,4]
          }
        ]
      })
    }
  }
}
</script>
