<template>
  <div
    id="deptAnalysis"
    v-loading="Loading"
    style="width: 100%; height: 300px"
  ></div>
</template>

<script>
import * as echarts from 'echarts';
import { dept } from '@/api/sys';
export default {
  props: {
    time: {
      type: String,
    },
  },
  data() {
    return {
      chartPie: null,
      Loading: true,
      data: [],
      month: '',
    };
  },
  watch: {
    time(value) {
      this.month = value;
      this.getUserAnalysis();
      setTimeout(() => {
        this.drawPieChart();
      }, 2000);
    },
  },

  mounted: function () {},
  methods: {
    async getUserAnalysis() {
      const { data } = await dept({ month: this.month });
      var list = [];
      data.forEach(function (v, k) {
        list.push({ name: v.dept, value: v.count });
      });
      this.data = list;
    },
    drawPieChart() {
      this.chartPie = echarts.init(document.getElementById('deptAnalysis'));
      this.chartPie.setOption({
        tooltip: {
          trigger: 'item',
          axisPointer: {
            type: 'shadow',
          },
        },
        legend: {
          top: '15%',
          left: 'center',
        },
        title: {
          left: 'center',
          text: '部门分析',
        },
        series: {
          type: 'pie',
          top: 65,
          radius: [60, 100],
          left: 'center',
          itemStyle: {
            borderColor: '#fff',
            borderWidth: 1,
          },
          label: {
            alignTo: 'edge',
            minMargin: 5,
            edgeDistance: 10,
            lineHeight: 15,
            rich: {
              time: {
                fontSize: 10,
                color: '#999',
              },
            },
          },
          labelLine: {
            length: 15,
            length2: 0,
            maxSurfaceAngle: 80,
          },
          data: this.data,
        },
      });
      this.Loading = false;
    },
  },
};
</script>

<style scoped></style>
