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

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '../mixins/resize'
import { trainNewstaffCount} from "@/api/training/newstaff/tnNew";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '210px'
    }
  },
  data() {
    return {
      chart: null,
      dataOut: []
    }
  },
  mounted() {
    this.$nextTick(() => {
      trainNewstaffCount().then(res=>{
        this.dataOut = [
          {value: res.data.count1, name: res.data.count1Name},
          {value: res.data.count0, name: res.data.count0Name},
        ]
        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: {
          trigger: 'item',
          formatter: "{b}: {c} " + " | " + "{d}%"
        },
        title:
          {
            text: '',
          }
        ,
        legend: {
          orient: 'vertical',
          left: 'left',
          itemWidth: 10,
          itemHeight: 10,
          textStyle: {
            fontSize: 12,
            padding:[0,0]
          },
          selectedMode: true,
          data: [],
        },
        series: [
          {
            name: '',
            type: 'pie',
            selectedMode: 'single',
            label: {
              position: 'outside',
              fontSize: 10,
            },
            animationEasing: 'cubicInOut',
            animationDuration: 1300,
            labelLine: {
              show: true
            },
            data: this.dataOut
          }
        ],
      })
    }
  }
}
</script>
