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

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '../mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    chartData: {
      type: Array,
      default: () => []
    },
    legendData: {
      type: Array,
      default: () => []
    },
    theme: {
      type: String,
      default: 'dark'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    theme() {
      this.initChart()
    },
    chartData: {
      deep: true,
      handler() {
        this.initChart()
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      if (this.chart) {
        this.chart.dispose()
      }
      this.chart = echarts.init(this.$el, 'macarons')

      const isDark = this.theme === 'dark';
      const textColor = isDark ? '#fff' : '#333';

      this.chart.setOption({
        title: {
          text: '项目人数',
          left: 'center',
          textStyle: {
            color: textColor
          }
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          left: 'center',
          bottom: '10',
          data: this.legendData && this.legendData.map(item => item.name),
          textStyle: {
            color: textColor
          }
        },
        series: [
          {
            name: '项目分类占比',
            type: 'pie',
            roseType: 'radius',
            radius: [15, 95],
            center: ['50%', '45%'],
            data: this.chartData || [],
            animationEasing: 'cubicInOut',
            animationDuration: 2600,
            label: {
              show: true,
              formatter: '{b}',
              color: textColor
            },
            labelLine: {
              show: true,
              lineStyle: {
                color: textColor
              }
            },
            itemStyle: {
              color: function(params) {
                // Custom bright colors
                const colorList = ['#37a2da', '#32c5e9', '#67e0e3', '#9fe6b8', '#ffdb5c', '#ff9f7f', '#fb7293', '#e062ae', '#e690d1', '#e7bcf3', '#9d96f5', '#8378ea', '#96bfff'];
                return colorList[params.dataIndex % colorList.length];
              }
            }
          }
        ]
      })
    }
  }
}
</script>

<style scoped>
.chart-legend {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  margin-top: 10px;
  padding: 0 10px;
}
.legend-item {
  display: flex;
  align-items: center;
  margin-right: 15px;
  margin-bottom: 5px;
  font-size: 12px;
  color: #606266;
}
.legend-icon {
  width: 10px;
  height: 10px;
  border-radius: 2px;
  margin-right: 5px;
}
</style>
