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

<script>
import echarts from 'echarts'
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '200px'
    },
    height: {
      type: String,
      default: '200px'
    }
  },
  data() {
    return {
      chart: null,
      text: ''
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id))
      this.text = this.id === 'local' ? '本地存储' : '七牛云OSS'

      const xAxisData = []
      const data = [
        {
          name: '邀请海报',
          value: 1
        },
        {
          name: '后台上传',
          value: 1
        },
        {
          name: '小程序分享商品',
          value: 1
        }, {
          name: '会员头像',
          value: 1
        }
      ]
      for (let i = 0; i < 50; i++) {
        xAxisData.push(i)
      }
      this.chart.setOption({
        backgroundColor: 'white',
        title: {
          text: this.text,
          x: 'center',
          textStyle: {
            fontSize: 20
          },
          subtext: `已使用：0B`,
          subtextStyle: {
            fontSize: 14,
            color: '#1890FF'
          }
        },
        tooltip: {
          trigger: 'item',
          formatter: '{b} <br/>比例: {d}%',
          backgroundColor: 'white',
          textStyle: {
            fontSize: 14,
            color: '#1890FF'
          }
        },
        legend: {
          left: 'center',
          bottom: '-5',
          data: ['邀请海报', '后台上传', '小程序分享商品', '会员头像']
        },
        grid: {
          left: '5%',
          right: '5%'
        },
        color: ['#D87A80', '#38C7C9', '#5AB1EF', '#B6A2DE'],
        series: [{
          name: '',
          type: 'pie',
          data,
          xAxisIndex: 1,
          radius: ['10%', '60%'],
          z: 3,
          roseType: 'radius',
          itemStyle: {
            normal: {
              label: {
                show: true,
                formatter: '{b}'
              },
              labelLine: { show: true }
            },
            emphasis: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }],
        animationEasing: 'elasticOut',
        animationEasingUpdate: 'elasticOut',
        animationDelay(idx) {
          return idx * 20
        },
        animationDelayUpdate(idx) {
          return idx * 20
        }
      })
    }
  }
}
</script>
