<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: '200px'
    },
    chartData: {
      type: Object,
      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) {
      // console.log(data)
      this.chart.setOption({
        graphic:{
          type: 'text',
          left: 'center',
          top: 'center',
          style:{
            text:'本周与上周\n核酸人数',
            textAlign:"center",
            fill:"#333",
            fontSize:14,
          },
        },
         tooltip: {
            trigger: 'item',
          },

          color:['#18A6E2', '#02DB80'],
          legend: {
            show:false,
            // bottom: 10,
            // left: 'center',
          },
          series: [
            {
              name: '',
              type: 'pie',
              radius: ['40%', '70%'],
              avoidLabelOverlap: false,
              center: ['50%', '50%'],
              itemStyle: {
                  borderRadius: 10,
                  borderColor: '#fff',
                  borderWidth: 2

              },
              label: {
                show: false,
                position: 'center'
              },
              // emphasis: {
              //   label: {
              //     show: true,
              //     fontSize: '18',
              //     fontWeight: '400',
              //     color:"#333"
              //   }
              // },
              labelLine: {
                show: false
              },
              // data:data,
              data: [
                { value: data.indexDataLastWeek, name: '上周核酸人数' },
                { value: data.indexDataWeek, name: '本周核酸人数' }

              ]
            }
          ]
      })
    }

  }
}
</script>
