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

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Array,
      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');
      this.setOptions(this.chartData);
    },
    setOptions(chartData) {
      let xAxis = [];
      let data1 = [];
      let data2 = [];
      if (chartData) {
        chartData.forEach((item, index) => {
          if (index - chartData.length == -1) {
            xAxis.push('今天');
          } else {
            xAxis.push(item.date.substring(5, 10));
          }
          data1.push({ value: item.wx, name: item.date });
          data2.push({ value: item.member, name: item.date });
        });
      }
      this.chart.setOption({
        title: {
          text: '近7天注册人数',
          padding: 3
        },
        legend: {
          icon: 'roundRect',
          data: ['微信公众号', '小程序'],
          textStyle: {
            fontSize: 15
          }
        },
        xAxis: {
          type: 'category',
          data: xAxis,
          boundaryGap: false,
          axisLabel: {
            margin: 16,
            fontSize: 15
          }
        },
        grid: {
          left: 10,
          right: 20,
          bottom: 0,
          top: 75,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          type: 'value',
          minInterval: 1,
          axisLabel: {
            fontSize: 15
          }
        },
        series: [
          {
            name: '微信公众号',
            itemStyle: {
              normal: {
                color: '#FF005A',
                lineStyle: {
                  color: '#FF005A',
                  width: 3
                },
                areaStyle: {
                  color: '#FF5722'
                }
              }
            },
            smooth: true,
            type: 'line',
            data: data1,
            markPoint: {
              data: [{ type: 'max', name: '最大值' }, { type: 'min', name: '最小值' }]
            },
            animationDuration: 2800,
            animationEasing: 'cubicInOut'
          },
          {
            name: '小程序',
            itemStyle: {
              normal: {
                color: '#5f79ca',
                lineStyle: {
                  color: '#5f79ca',
                  width: 3
                },
                areaStyle: {
                  color: '#8da2e4'
                }
              }
            },
            smooth: true,
            type: 'line',
            data: data2,
            markPoint: {
              data: [{ type: 'max', name: '最大值' }, { type: 'min', name: '最小值' }]
            },
            animationDuration: 2800,
            animationEasing: 'cubicInOut'
          }
        ]
      });
    }
  }
};
</script>
