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

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

export default {
  name:'echartsPie',
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '390px'
    }
  },
  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: '',
          textStyle: {
            color: '#333333',
            fontSize: 16,
            fontWeight: 700
          },
          subtextStyle: {
            fontSize: 12,
            color: '#333',
            fontWeight: 700
          }
        },
        tooltip: {
          show: false,
          trigger: 'item',
          formatter: "{a} <br/>{b}: {c} ({d}%)"
        },
        animation: false,
        series: [
          {
            type: 'pie',
            radius: ['40%', '55%'],
            center: ['25%', '50%'],
            label: {
              position: 'center',
              show: true,
              color: '#1E7CE8',
              formatter: [
                '{a|6,256 m³}',
                '{b|已使用}',
              ].join('\n'),
              rich: {
                a: {
                  color: '#333333',
                  fontWeight: 700,
                  textAlign: 'center',
                  fontSize: '22px', 
                },
                b: {
                  color: '#999999',
                  fontSize: '14px',
                  fontWeight: '400',
                  padding:[32,0,0,0]
                }
              },
              textStyle: {
                align: "center",
                color: '#333',
                fontWeight: 700,
                fontSize: 16,
                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>
