pieChart.vue 3.01 KB
<template>
  <div class="echart" :ref="id" :style="{ width: width, height: height }"></div>
</template>

<script>
import * as echarts from 'echarts'
export default {
  props: {
    id: {
      type: String,
      default: ''
    },
    colors: {
      type: Array,
      default: () => ['#60D68E', '#708DF4']
    },
    serveData: {
      type: Array,
      default: () => []
    },
    height: {
      type: String,
      default: '72px'
    },
    width: {
      type: String,
      default: '72px'
    }
  },
  data() {
    return {
      myChart: null
    }
  },
  watch: {
    serveData: {
      handler(val) {
        this.updateChart(val)
      },
      deep: true
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initEcharts()
    })
  },
  beforeDestroy() {
    if (this.myChart) {
      this.myChart.dispose()
      this.myChart = null
    }
  },
  methods: {
    initEcharts() {
      const client = this.$refs[this.id]
      if (!client) {
        console.warn('ECharts container not found')
        return
      }

      if (this.myChart) {
        this.myChart.dispose()
      }
      
      this.myChart = echarts.init(client)
      this.updateChart(this.serveData)
    },

    updateChart(data) {
      if (!this.myChart) {
        return
      }

      const option = {
        color: this.colors,
        textStyle: {
          fontFamily: 'numberBold'
        },
        series: [
          {
            type: 'pie',
            radius: ['55%', '80%'],
            avoidLabelOverlap: false,
            label: {
              normal: {
                show: false,
                position: 'center',
                formatter: (params) => {
                  const percent = params.percent ? params.percent.toFixed(2) : 0
                  return '占比\n' + percent + '%'
                },
                textStyle: {
                  fontFamily: 'numberBold'
                }
              },
              emphasis: {
                show: true,
                textStyle: {
                  fontSize: 12,
                  color: '#333',
                  fontFamily: 'numberBold'
                }
              }
            },
            labelLine: { 
              show: false 
            },
            data: data
          }
        ]
      }

      this.myChart.setOption(option, true)
      
      this.myChart.off('mouseover')
      
      if (data && data.length > 0) {
        this.myChart.dispatchAction({ 
          type: 'highlight', 
          seriesIndex: 0, 
          dataIndex: 0 
        })
        
        this.myChart.on('mouseover', (e) => {
          this.myChart.dispatchAction({ 
            type: 'downplay', 
            seriesIndex: 0, 
            dataIndex: 0 
          })
          
          if (e.dataIndex === 0) {
            this.myChart.dispatchAction({ 
              type: 'highlight', 
              seriesIndex: 0, 
              dataIndex: e.dataIndex 
            })
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.echart {
  display: inline-block;
}
</style>