<template>
  <div id="pieChart_id" style="width: 100%;height: 280px;">
  </div>
</template>
<script>

export default {
  name: "pieChart",
  components: {

  },
  data () {
    return {
      chartData: []
    }
  },
  props: {
    title: {
      type: String,
      default () {
        return ''
      }
    },
    subtitle: {
      type: String,
      default () {
        return ''
      }
    },
    value: {
      type: Array,
      default () {
        return []
      }
    },
  },
  created () {
    this.chartData = this.value
  },
  mounted () {
    this.$nextTick(() => {
      var timer = setTimeout(() => {
        this.drawChart();
        clearInterval(timer);
      }, 0)
    })
  },
  methods: {
    drawChart () {
      let myChart = this.$echarts.init(document.getElementById("pieChart_id"))
      window.addEventListener('resize', () => {
        myChart.resize()
      })
      let option = {
        title: {
          text: this.title,
          subtext: this.subtitle,
          left: 'center'
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          orient: 'vertical',
          left: 'left'
        },
        series: [
          {
            name: '数据来自',
            type: 'pie',
            radius: '60%',
            data: this.chartData,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      }
      myChart.setOption(option);
    },
  },
  beforeDestroy () {
    window.removeEventListener("resize", () => {
      this.myChart.resize();
    });
  },
  watch: {
    value: {
      handler (value) {
        this.chartData = value
        this.drawChart()
      },
    },
  }
}
</script>
<style lang="less" scoped>
</style>

