<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%'
    },
    chartData: {
      type: Array,
      required: true
    },
    height: {
      type: String,
      default: '310px'
    }
  },
  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() {
      setTimeout(() => {
        this.chart = echarts.init(this.$el, 'macarons');
        this.setOptions(this.chartData);
      }, 300);
    },
    setOptions(chartData) {
      let xAxisData = [];
      let seriesData = [];
      if (chartData) {
        chartData.forEach((item, index) => {
          xAxisData.push(item.name);
          seriesData.push(item.value);
        });
      }
      this.chart.setOption({
        color: ['rgba(37, 97, 239, 1)'],
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          },
          formatter: '{b} : {c}个'
        },
        grid: {
          top: '13%',
          left: '2%',
          right: '2%',
          bottom: '2%',
          containLabel: true,
          show: false
        },
        xAxis: [
          {
            type: 'category',
            data: xAxisData,
            axisTick: {
              show: false
            },
            axisLine: {
              lineStyle: {
                color: '#e6e6e6'
              }
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: '#545555',
                fontSize: '13'
              }
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            minInterval: 1,
            axisTick: {
              show: false
            },
            splitArea: {
              show: false
            },
            splitLine: {
              lineStyle: {
                color: '#e6e6e6',
                width: 0.7
              }
            },
            axisLine: {
              lineStyle: {
                color: '#e6e6e6'
              }
            },
            axisLabel: {
              textStyle: {
                color: '#545555'
              }
            }
          }
        ],
        series: [
          {
            type: 'bar',
            barWidth: '30%',
            data: seriesData,
            animationDuration,
            itemStyle: {
              normal: {
                opacity: 1,
                barBorderRadius: [3, 3, 0, 0],
                label: {
                  show: true,
                  position: 'top',
                  textStyle: {
                    color: '#545555',
                    fontSize: 14
                  }
                }
              }
            }
          }
        ]
      });
    }
  }
};
</script>
