<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'
    },
    chartData: {
      type: Array,
      required: true
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  data() {
    return {
      chart: null
    }
  },
  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.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions(data) {
      this.chart.setOption({
        // tooltip: {
        //   trigger: 'item',
        //   formatter: '{a} <br/>{b} : {c} ({d}%)'
        // },
        // legend: {
        //   left: 'center',
        //   bottom: '10',
        //   data: ['Industries', 'Technology', 'Forex', 'Gold', 'Forecasts']
        // },
        // series: [
        //   {
        //     name: 'WEEKLY WRITE ARTICLES',
        //     type: 'pie',
        //     roseType: 'radius',
        //     radius: ["40", "70%"],
        //     avoidLabelOverlap: false,
        //     center: ['50%', '38%'],
        //     data: [
        //       { value: 320, name: 'Industries' },
        //       { value: 240, name: 'Technology' },
        //       { value: 149, name: 'Forex' },
        //       { value: 100, name: 'Gold' },
        //       { value: 59, name: 'Forecasts' }
        //     ],
        //     // animationEasing: 'cubicInOut',
        //     animationDuration: 2600
        //   }
        // ]

         tooltip: {
            trigger: 'item'
          },
          color:['#65C6F3', '#FEC46C','#A195FB','#F98463','#88A9F3'],
          legend: {
            bottom: 10,
            left: 'center',

          },
          series: [
            {
              name: 'Access From',
              type: 'pie',
              radius: ['40%', '70%'],
              avoidLabelOverlap: false,
               center: ['50%', '38%'],
              itemStyle: {
                  borderRadius: 10,
                  borderColor: '#fff',
                  borderWidth: 2

              },
              label: {
                show: false,
                position: 'center'
              },
              emphasis: {
                label: {
                  show: true,
                  fontSize: '18',
                  fontWeight: '400',
                  color:"#333"
                }
              },
              labelLine: {
                show: true
              },
              // data:data,
              data: [
                { value: 6, name: '五保户' },
                { value: 3, name: '低保户' },
                { value: 5, name: '贫困户' },
                { value: 2, name: '党员' },
                { value: 1, name: '退伍军人' }
              ]
            }
          ]
      })
    }

  }
}
</script>
