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

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    chart1Data: {
      type: Object,
      required: true
    }    
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chart1Data: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },  
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chart1Data)
    },
    setOptions({ guahaodata, tuihaodata, jiedata,chaxundata,wenjuandata,weekdata } = {}) {          
       this.chart.setOption({
        tooltip: {
          trigger: 'axis'
          // axisPointer: {
          //   type: 'cross',
          //   crossStyle: {
          //     color: '#999'
          //   }
          // }
        },

        legend: {
          data: ['挂号', '退号', '结算', '报告查询', '问卷']
        },
        xAxis: [
          {
            type: 'category',
            data: weekdata,
            axisPointer: {
              type: 'shadow'
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: '',
            min: 0,
            max: 20,
            interval: 5,
            axisLabel: {
              formatter: '{value}'
            }
          }
        ],
        series: [
          {
            name: '挂号',
            type: 'bar',
            tooltip: {
              valueFormatter: function (value) {
                return value + ' ml';
              }
            },
            data:guahaodata
          },
          {
            name: '退号',
            type: 'bar',
            tooltip: {
              valueFormatter: function (value) {
                return value + ' ml';
              }
            },
            data:tuihaodata
          },
          {
            name: '结算',
            type: 'bar',
            tooltip: {
              valueFormatter: function (value) {
                return value + ' ml';
              }
            },
            data: jiedata
          },
          {
            name: '报告查询',
            type: 'bar',
            tooltip: {
              valueFormatter: function (value) {
                return value + ' ml';
              }
            },
            data: chaxundata
          },
          {
            name: '问卷',
            type: 'bar',
            tooltip: {
              valueFormatter: function (value) {
                return value + ' ml';
              }
            },
            data: wenjuandata
          }                              
        ]
      })
    }
  }
}
</script>
