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

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

const animationDuration = 3000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '310px'
    },
    chartData: {
      type: Object,
      required: true
    }
  },
 watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  data() {
    return {
      chart: null
    }
  },
  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.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
		// { y1, x ,y2 } = {}
      setOptions({ y1, x ,y2 } = {}) {
      this.chart.setOption({
                title: {
                   text: ''
                 },
                 tooltip: {
                   trigger: 'axis',
                   axisPointer: {
                     type: 'shadow'
                   }
                 },
                 legend: {
					 bottom:0,
				 },
                 grid: {
                   left: '3%',
                   right: '4%',
				   top:'0%',
                   bottom: '12%',
                   containLabel: true
                 },
                 xAxis: {
                   type: 'value',
                   boundaryGap: [0, 0.01],
				   axisLabel: {
				          show: true,
				           textStyle: {
				                     color: '#666',  //更改坐标轴文字颜色
				                     fontSize : 12      //更改坐标轴文字大小
				                   }
				        },
				  
				   
                 },
                 yAxis: {
                   type: 'category',
                   data: ['0~100', '100~200', '200~300', '300~400', '400~500', '500~600','600~700','700~800'],
				    axisLabel: {//y轴文字的配置
				                   textStyle: {
				                        color: "rgba(138, 144, 163, 1)",//Y轴内容文字颜色
				                   },
				           },
				    axisLine: {//y轴线的配置
				                   show: true,//是否展示
				                   lineStyle: {
				                       color: "#ECEDF0",//y轴线的颜色（若只设置了y轴线的颜色，未设置y轴文字的颜色，则y轴文字会默认跟设置的y轴线颜色一致）
				                       width: 1,//y轴线的宽度
				                       type: "solid"//y轴线为实线
				                   },
				           }

                 },
				 color: ['#5470C6', '#91CC75'],
                 series: [
					 
                   {
                     name: '上次',
                     type: 'bar',
                     data: y1,
                   },
                   {
                     name: '本次',
                     type: 'bar',
                     data: y2
                   }
                 ]
            })
      }
  }
}
</script>
