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

<script>
import echarts from 'echarts3'
require('echarts3/theme/infographic')
// import { debounce } from '../../utils'
import resize from './mixins/resize'

const animationDuration = 3000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    chartRadarData: {
      type: Object,
      required: true
    },
    showLegend: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      radarChart: null
    }
  },
  watch: {
    chartRadarData: {
      deep: true,
      handler: function(val) {
        this.setOption(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.radarChart) {
      return
    }
    this.radarChart.dispose()
    this.radarChart = null
  },
  methods: {
    initChart() {
      this.radarChart = echarts.init(this.$el, 'infographic')
      this.setOption(this.chartRadarData)
    },
    setOption({ xAxisData, yAxisDataList, title } = {}) {
      const legendList = []; const indicator = []; const max = []
      if (yAxisDataList.length != 0) {
        yAxisDataList.forEach(item => {
          // console.log(item)
          const obj = {}
          obj.name = item.name
          obj.type = 'funnel'
          legendList.push(obj.name)
          max.push(Math.max(...item.value))
        })
      }
      if (xAxisData.length != 0) {
        xAxisData.forEach((item, index) => {
          const obj = {}
          obj.name = item
          obj.max = Math.max(...max)
          indicator.push(obj)
        })
      }

      this.radarChart.setOption({
        tooltip: {
          textStyle: {
            fontSize: 10
          },
          position: function(point, params, dom, rect, size) {
            // 其中point为当前鼠标的位置
            return [point[0], '12%']
          }
        },
        title: {
          text: title
        },
        radar: {
          radius: '66%',
          center: ['50%', '44%'],
          splitNumber: 8,
          splitArea: {
            areaStyle: {
              color: 'rgba(127,95,132,.3)',
              opacity: 1,
              shadowBlur: 45,
              shadowColor: 'rgba(0,0,0,.5)',
              shadowOffsetX: 0,
              shadowOffsetY: 15
            }
          },
          indicator: indicator
        },
        legend: {
          data: legendList,
          show: this.showLegend,
          left: 'right',
          orient: 'horizontal',
          icon: 'rect',
          itemWidth: 12,
          itemHeight: 8,
          right: 10,
          top: 0,
          textStyle: {
            color: '#333',
            fontSize: '12'
          }
        },
        color: ['#0078d7', '#f04244', '#ff8700', '#00e473', '#ff6a00', '#ff00de', '#0000ff'],
        series: [{
          type: 'radar',
          symbolSize: 0,
          areaStyle: {
            normal: {
              shadowBlur: 13,
              shadowColor: 'rgba(0,0,0,.2)',
              shadowOffsetX: 0,
              shadowOffsetY: 10,
              opacity: 1
            }
          },
          data: yAxisDataList,
          animationDuration: animationDuration
        }]
      })
    }
  }
}
</script>
