<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'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '210px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      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')
      console.log(this.chartData)
      this.setOptions(this.chartData)
    },

    setOptions({y,x1, x2, } = {}) {
      console.log(y)
      this.chart.setOption({
        title:{text: "",
          left: "center",
          top: 0,
          textStyle: {
            color: "#161616",
            fontWeight: 'bold',
            fontSize: '15px',
            }
          },
        color:['#04BFA1', '#4875E8','#FFB132'],
        xAxis: {
          data: y,
          axisLabel:{
            color:'#AAAAAA'
          },
          axisLine: {
          	lineStyle: {
          		color: '#DADADA',
          		width: 1
          	}
          },
          splitLine: {
                show: false,
              },
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 10,
          top: 10,
          show:false,
          containLabel: true
        },
        toolbox: {
          show:false,
        },
        tooltip: {
          trigger: 'axis',
          backgroundColor: "#FFF",
          color: "#161616",
          textStyle: {
                color: "#161616",
                // fontWeight:'bold'
              },
          axisPointer: {
            type: 'cross',
          },
          padding: [5, 10]
        },
        yAxis: {
          splitArea: {
            show: false
          },
          axisLabel:{
            color:'#AAAAAA'
          },
          axisLine: {
            show:true,
          	lineStyle: {
          		color: '#DADADA',
          		width: 1
          	}
          },
          splitLine:{  //决定是否显示坐标中网格
            show:false
          }
        },
        legend: {
          x:'center',
          y:'bottom',
          textStyle: {
            color: "#161616",
            padding:[0,3]//文字与图形之间的左右间距
          },
          itemWidth: 38,//图例标记的图形宽度。
          itemHeight: 12,
          itemGap: 20,
          data: y
        },
        series: [{
          name: '上月',
          itemStyle: {
			   normal: {
			                          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
			                              offset: 0,
			                              color: '#FB9C67'
			                          }, {
			                              offset: 1,
			                              color: '#FFC6A9'
			                          }]),
                    barBorderRadius:[10, 10, 0, 0]
			           }

		  },
          type: 'bar',
          barGap:"90%",
          barCategoryGap:'50%',
          data: x2,
          // animationDuration: 2800,
          // animationEasing: 'cubicInOut'
        },
        {
          name: '本月',
		  itemStyle: {
		  			   normal: {
		  			                          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
		  			                              offset: 0,
		  			                              color: '#59CAA7'
		  			                          }, {
		  			                              offset: 1,
		  			                              color: '#B1EAD8'
		  			                          }]),
                              barBorderRadius:[10, 10, 0, 0]
		  			           }
		  },
          type: 'bar',
          barGap:"90%",
          barCategoryGap:'50%',
          data: x1,
          // animationDuration: 2800,
          // animationEasing: 'quadraticOut'
        },

        ]
      })
    }
  }
}
</script>
