PieChart.vue 3.68 KB
<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    option: {
      type: Object,
      default: () => {}
    },
    optionData: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      chart: null,
      colorList: [
        '#ff7e50', '#97d3f9', '#dd70d9', '#34cf34',
        '#6497ef', '#85802b', '#D7504B', '#C6E579',
        '#F4E001', '#F0805A', '#26C0C0'
      ]
    }
  },
  watch: {
    option: {
      handler(newVal) {
        // console.log('获取配置', newVal)
        if (this.chart) {
          this.setOption()
        }
      },
      immediate: true,
      deep: true
    },
    optionData: {
      handler(newVal) {
        console.log('获取饼状数据', newVal, this.chart)
        if (this.chart) {
          this.initData()
        }
      },
      immediate: true,
      deep: true
    }
  },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      const option = {
        title: {
          show: false,
          right: 'center',
          textStyle: {
            fontSize: 14
          }
        },
        tooltip: {
          trigger: 'item'
        },
        grid: {
          top: '10%',
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        series: [
          {
            name: '标签人数分布',
            type: 'pie',
            radius: '50%',
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      }
      this.chart.setOption(option)
      if (Object.keys(this.option).length > 0) {
        this.setOption()
      }
      console.log(this.optionData)
      if (Object.keys(this.optionData).length > 0) {
        this.initData()
      }
    },
    initData() {
      console.log(123)
      console.log(this.optionData)
      const keyArray = Object.keys(this.optionData)
      const yAxis = []
      const xAxis = []
      if (typeof this.optionData[keyArray[0]] !== 'object') {
        keyArray.forEach(item => {
          // yAxis.push(item)
          // xAxis.push(this.optionData[item])
          xAxis.push({
            value: this.optionData[item],
            name: item
          })
        })
        console.log(yAxis, xAxis)
      } else {
        keyArray.forEach(item => {
          const array = Object.keys(this.optionData[item])
          array.forEach(itemArray => {
            yAxis.push(item + ':' + itemArray)
            xAxis.push(this.optionData[item][itemArray])
          })
        })
      }
      const option = {
        // yAxis: {
        //   data: yAxis
        // },
        series: [
          {
            data: xAxis
          }
        ]
      }
      console.log(option)
      this.chart.setOption(option)
    },
    setOption() {
      this.chart.setOption(this.option)
    }
  }
}
</script>