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

<script>
import * as echarts from 'echarts';
require('echarts/theme/macarons') // echarts theme
import { on, off } from '@/libs/tools'
export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    type: {
      type: String,
      default: 'bar'
    },
    height: {
      type: String,
      default: '350px'
    },
    // 自定义配置
    customize: {
      type: Boolean,
      default: false
    },
    chartData: {
      type: Object,
      required: true
    },

  },
  data() {
    return {
      chart: null,
      barConfig: {
        tooltip: {
          trigger: 'axis',
          backgroundColor: "#fff",
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          top: 40,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: [],
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: [{
          type: 'value',
          axisTick: {
            show: false
          }
        }],
        series: []
      },
      pieConfig: {
        tooltip: {
          backgroundColor: "#fff",
          trigger: 'item',
          formatter: '{b} : {c} ({d}%)'
        },
        legend: {
          left: 'center',
          bottom: '10',
          data: []
        },
        series: []
      }
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    off(window, 'resize', this.chart.resize)
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
      on(window, 'resize', this.chart.resize)
    },
    setOptions(chartData) {
      let option = {}

      let type = this.type
      if (type === 'pie') {
        if (this.customize) {
          option = {
            ...this.pieConfig,
            ...chartData,
          }

        } else {
          let xTitle = chartData.xTitle || '';
          let data = chartData.data || [];
          let showData = [];
          data.forEach(items => {
            showData.push({
              name: items.name,
              value: items.value
            });
          });
          this.pieConfig.legend.data = showData.map(_ => _.name)
          this.pieConfig.series = [];
          this.pieConfig.series = [
            {
              type: 'pie',
              radius: [15, 75],
              center: ['50%', '38%'],
              animationEasing: 'cubicInOut',
              animationDuration: 2600,
              data: showData,
            }
          ];
          option = {
            ...this.pieConfig,
            title: {
              text: xTitle
            }
          }
        }
      } else if (type === 'bar') {
        if (this.customize) {
          option = {
            ...this.barConfig,
            ...chartData,
          }
        }
      }

      this.chart.setOption(option)
    }
  }
}
</script>