<template>
  <div :id="id" :class="className" :style="{ height:height,width:width }" />
</template>
<script>
  import echarts from 'echarts'
  import resize from './mixins/resize'

  export default {
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: "chart"
      },
      id: {
        type: String,
        default: "chart"
      },
      width: {
        type: String,
        default: "100%"
      },
      height: {
        type: String,
        default: "400px"
      },
      data: {
        type: Object,
        default: {}
      },
    },
    data() {
      return {
        chart: null
      };
    },
      watch: {
          //观察option的变化
          data: {
              handler(newVal, oldVal) {
                  if (newVal) {
                      this.initChart();
                  }
              },
              deep: true //对象内部属性的监听，关键。
          }
      },
    mounted() {
        this.$nextTick(() => {
            this.initChart();
        })
    },
    beforeDestroy() {
      if (!this.chart) {
        return;
      }
      this.chart.dispose();
      this.chart = null;
    },
    methods: {
      initChart() {
        let that = this;
        this.chart = echarts.init(document.getElementById(this.id));
        let seriesData = [], legendData = [];
        that.data.seriesData.forEach(function(item,index){
          let series = {
            name: '',
            type: 'bar',
            // barWidth: '50%',
            data:[]
          }
          item.stack ? series.stack = item.stack : '';
          item.data ? series.data = [].concat(item.data) : '';
          if(item.name){
             series.name = item.name;
             legendData.push(item.name);
          }
          if(index == 1){
            series.yAxisIndex = 1;
          }
          seriesData.push(series)
        })
        let option = {
          tooltip: {
            trigger: 'axis',
            axisPointer: { // 坐标轴指示器，坐标轴触发有效
              type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
            },
            formatter:'{b}<br />{a0}: {c0}万<br />{a1}: {c1}元'
          },
          color:that.data.color,
          legend: {
            left: "center",
            top: 10,
            data: legendData
          },
          grid: {
            top: '30%',
            left: '2%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: [
            {
              type: 'category',
              data: that.data.xAxisData,
              axisTick: {
                show:false
              },
              axisLine: {
                lineStyle:{
                  color:'#4FAAF6',
                },
              },
              axisLabel:{
                  fontSize: 10,
                color:'#333',
                rotate:40
              },
            }
          ],
          yAxis: [
            {
              type: 'value',
              name: that.data.yAxisNameL,
              nameTextStyle:{
                color:'#333',
                fontSize: 10,
              },
              axisTick: {
                show: false
              },
              axisLine: {
                lineStyle:{
                  color:'#4FAAF6',
                },
              },
              axisLabel:{
                color:'#333',
              },
              splitLine: {
                lineStyle:{
                  color:'#EFEFEF',
                },
              },
            },
            {
              type: 'value',
              name: that.data.yAxisNameR,
              nameTextStyle:{
                color:'#333',
                fontSize: 10,
              },
              axisTick: {
                show: false
              },
              axisLine: {
                lineStyle:{
                  color:'#F39801',
                },
              },
              axisLabel:{
                color:'#333',
              },
              splitLine: {
                lineStyle:{
                  color:'#EFEFEF',
                },
              },
            },
          ],
          series: seriesData,
        };
        this.chart.setOption(option);
      }
    }
  };
</script>
