<template>
  <div class="chart" ref="chart"></div>
</template>

<script>
import * as echarts from 'echarts'
export default {
  name: 'statisticPieChart', //统计饼图
  mounted() {
    this.$nextTick(() => {
      this.initialChart()
    })
  },
  methods: {
    initialChart() {
      const color = [
        'rgba(17, 156, 255, 1)',
        'rgba(232, 190, 76, 1)',
        'rgba(21, 208, 234, 1)',
        'rgba(6, 174, 97, 1)',
        'rgba(212, 112, 13, 1)',
        'rgba(175, 45, 230, 1)'
      ]
      const color2 = [
        'rgba(17, 156, 255, 0.15)',
        'rgba(232, 190, 76, 0.15)',
        'rgba(21, 208, 234, 0.15)',
        'rgba(6, 174, 97, 0.15)',
        'rgba(212, 112, 13, 0.15)',
        'rgba(175, 45, 230, 0.15)'
      ]
      const data = [
        {
          name: '三峡分公司',
          value: 16
        },
        {
          name: '重庆分公司',
          value: 3
        },
        {
          name: '上海分公司',
          value: 3
        }
      ]
      // 设置数据
      function setChartOption(data) {
        const formatData = []
        data.forEach(function (item, index) {
          formatData.push(
            {
              value: item.value,
              name: item.name,
              label: {
                show: false
              },
              labelLine: {
                show: false
              },
              emphasis: {
                disabled: true,
                labelLine: {
                  show: false
                }
              },
              itemStyle: {
                color: color[index],
                shadowBlur: 20,
                shadowColor: color[index]
              }
            },
            {
              value: 0.5,
              name: '',
              itemStyle: {
                color: 'rgba(0, 0, 0, 0)',
                borderColor: 'rgba(0, 0, 0, 0)',
                borderWidth: 0
              }
            }
          )
        })

        return formatData
      }
      // 设置阴影数据
      function setChartOption2(data) {
        const formatData = []
        data.forEach(function (item, index) {
          formatData.push(
            {
              value: item.value,
              name: item.name,
              label: {
                show: false
              },
              labelLine: {
                show: false
              },
              itemStyle: {
                color: color2[index],
                shadowBlur: 20,
                shadowColor: color2[index]
              }
            },
            {
              value: 0.5,
              name: '',
              itemStyle: {
                color: 'rgba(0, 0, 0, 0)',
                borderColor: 'rgba(0, 0, 0, 0)',
                borderWidth: 0
              }
            }
          )
        })

        return formatData
      }
      let option = {
        backgroundColor: 'transparent',
        title: {
          text: '{a|22}' + '{b|个}\n' + '{c|项目总数}',
          top: 'center',
          left: '29%',
          textAlign: 'center',
          textStyle: {
            rich: {
              a: { fontSize: 24, color: 'rgba(255, 255, 255, 1)' },
              b: { fontSize: 20, color: 'rgba(255, 255, 255, 1)' },
              c: { fontSize: 14, color: 'rgba(0,141,236,0.9)' }
            }
          }
        },
        tooltip: {
          trigger: 'item',
          textStyle: {
            color: '#fff'
          },
          borderWidth: 0,
          backgroundColor: 'rgba(50,50,50,0.7)'
        },
        legend: {
          type: 'scroll',
          orient: 'vertical',
          itemWidth: 8,
          itemHeight: 8,
          itemGap: 15,
          icon: 'react',
          left: '60%',
          top: 'center',
          textStyle: {
            rich: {
              a: {
                fontSize: 14,
                color: '#fff'
              },
              b: {
                fontSize: 16,
                fontWeight: 'bold',
                color: '#fff'
              }
            }
          },
          formatter: function (name) {
            //图例后添加数值
            let data = option.series[0].data
            let tarValue
            for (let i = 0; i < data.length; i++) {
              if (data[i].name === name) {
                tarValue = data[i].value
              }
            }
            return '{a|' + name + '}' + '     ' + '{b|' + tarValue + '个}'
          },
          data: data
        },
        series: [
          {
            name: '',
            type: 'pie',
            radius: ['60%', '70%'],
            center: ['30%', '50%'],
            data: setChartOption(data)
          },
          {
            name: '',
            type: 'pie',
            radius: ['50%', '60%'],
            center: ['30%', '50%'],
            hoverAnimation: false,
            silent: true,
            data: setChartOption2(data)
          }
        ]
      }
      this.creatChart(option, this.$refs.chart)
    },
    creatChart(option, ref) {
      let chart = echarts.init(ref) //this.$refs.chart
      chart.resize()
      chart.setOption(option, {
        notMerge: true
      })
      //图表大小自适应
      window.addEventListener('resize', () => {
        chart.resize()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.chart {
  height: 100%;
  width: 100%;
}
</style>