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

<script>
import echarts from 'echarts3'
require('echarts3/theme/infographic')
// import { debounce } from '../../utils'
import resize from './mixins/resize'

export default {
  name: 'BarChart',
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    height: {
      type: String,
      default: '300px'
    },
    width: {
      type: String,
      default: '100%'
    },
    chartPieData: {
      type: Object,
      required: true
    },
    showLegend: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartPieData: {
      deep: true,
      handler(val) {
        // console.log(333,val)
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    // 渲染柱状图
    initChart() {
      this.chart = echarts.init(this.$el, 'infographic')
      this.setOptions(this.chartPieData)
    },
    setOptions({ xAxisData, yAxisDataList, title } = {}) {
      const legendList = []
      const colorList = ['#0078d7', '#f04244', '#ff8700', '#00e473', '#ff6a00',
        '#ff00de', '#0000ff'
      ]
      if (yAxisDataList.length != 0) {
        yAxisDataList.forEach(item => {
          const obj = {}
          obj.name = item.name
          legendList.push(obj.name)
        })
      }
      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)',
          textStyle: {
            fontSize: 10
          },
          position: function(point, params, dom, rect, size) {
            // 其中point为当前鼠标的位置
            return [point[0], '12%']
          }
        },
        title: {
          text: title,
          show: false
        },
        grid: {
          left: '3%',
          right: '3%',
          bottom: '8%',
          top: '10%',
          containLabel: true
        },
        legend: {
          show: this.showLegend,
          left: 'right',
          orient: 'horizontal',
          icon: 'rect',
          itemWidth: 12,
          itemHeight: 8,
          right: 10,
          top: 0,
          textStyle: {
            color: '#333',
            fontSize: '12'
          },
          data: legendList
        },
        calculable: true,
        series: [
          {
            type: 'pie',
            roseType: 'radius',
            radius: ['28%', '55%'],
            center: ['50%', '46%'],
            clockwise: true,
            avoidLabelOverlap: true,
            data: yAxisDataList,
            //  hoverOffset: 15,
            itemStyle: {
              normal: {
                color: function(params) {
                  return colorList[params.dataIndex]
                }
              }
            },
            label: {
              show: false,
              position: 'outside',
              formatter: '{a|{b}：{d}%}\n{hr|}',
              rich: {
                hr: {
                  backgroundColor: 't',
                  borderRadius: 3,
                  width: 3,
                  height: 3,
                  padding: [3, 3, 0, -12]
                },
                a: {
                  padding: [-30, 15, -20, 15]
                }
              }
            },
            labelLine: {
              normal: {
                length: 10,
                length2: 6,
                lineStyle: {
                  width: 1
                }
              }
            }
          }
        ]
      })
    }
  }
}
</script>

<style scoped>

</style>
