<template>
  <div :class="className" style="height: 160px;width:160px" />
</template>

<script>
import * as echarts from 'echarts';

export default {
  name: 'echartsPie',
  props: {
    className: {
      type: String,
      default: 'chart'
    }
  },
  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.setOption({
        tooltip:{
          show:false,
        },
        hoverAnimation: false,
        title: {
          text: '',
          subtext: '',
          x: 'center',
          y: 'center',
          top: '36%',
          textStyle: {
            fontSize: 12,
          },
          subtextStyle: {
            fontSize: 12,
            color: '#333',
            fontWeight: 700
          }
        },
        animation: false,
        series: [
          {
            type: 'pie',
            center: [45,45],
            radius: ['48%', '55%'],
            label: {
              position: 'center',
              show: true,
              color: '#1E7CE8',
              formatter: ['{a|60}%','{b|实际用水量}'].join('\n'),
              rich: {
                a: {
                  fontSize:'22px',
                },
                b:{
                  color: '#333333',
                  fontSize: '12px',
                  padding:[10,0,0,0]
                }
              },
              textStyle: {
                align: "center",
                color: '#333',
                fontSize: 12,
                baseline: "middle"
              },
              lineHeight: 16,
              fontSize: 12,
            },
            itemStyle: {
              color: '#c23531',
              shadowBlur: 200,
              shadowColor: 'rgba(0, 0, 0, 0)'
            },
            emphasis: {
              scale: false,
              selectorLabel:{
                show: false
              },
              itemStyle: {
                color: 'inherit'
              },
              textStyle: {
                fontSize: '30',
                fontWeight: 'bold'
              },
              label: {
                show: false,
                fontWeight: 'bold'
              },
              disable:false,  //是否关闭扇区高亮效果
              scale:false,    //扇区是否缩放
              scaleSize:0, 
            },
            labelLine: {
              show: false
            },
            data: [
              { value: 1048, name: 'Direct',itemStyle:{color: '#1890FF'} },
              { value: 1048, name: 'Direct2',itemStyle:{color: '#D9D9D9'} }
            ]
          }
        ]
      })
      this.chart.on('mouseover',e=>{
        let op = this.chart.getOption()
        this.chart.dispatchAction({
          type: 'downplay',
          animation: false,
          seriesIndex: 0,
          dataIndex: e.dataIndex,
          color: e.color
        })
        this.chart.setOption(op, true)
      })
    }
  }
}
</script>
