PieChart.vue 4.09 KB
<template>
  <div class="pie-box">
    <div id="pie-chart" class="pie-chart"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts'
export default {
  name: 'PieChart',

  props: {
    keys: {
      type: Array,
      default: () => ['name', 'number']
    },
    list: {
      type: Array,
      default: () => [
        { number: 1048, name: 'Search Engine' },
        { number: 735, name: 'Direct' },
        { number: 580, name: 'Email' },
        { number: 484, name: 'Union Ads' },
        { number: 300, name: 'Video Ads' }
      ]
    }
  },

  data() {
    return {
      echartsPie: '',
      mTime: '',
      viewData: []
    }
  },

  created() {
    this.initData()
  },

  mounted() {
    this.echartsPie = echarts.init(document.getElementById('pie-chart'))
    window.addEventListener('resize', () => {
      this.echartsPie.resize()
    })
    this.draw()
  },

  methods: {
    //初始化数据
    initData() {
      let { list, viewData, keys } = this
      list.forEach(item => {
        viewData.push({
          name: item[keys[0]],
          value: item[keys[1]]
        })
      })
    },
    draw() {
      // 指定图表的配置项和数据
      const option = {
        tooltip: {
          trigger: 'item'
        },

        // color: ['#289df5', '#ff5050'],
        // graphic: {
        //   type: 'text',
        //   left: 'center',
        //   top: '40%',
        //   style: {
        //     text: '运动达标率',
        //     textAlign: 'center',
        //     fill: '#333',
        //     fontSize: 20,
        //     fontWeight: 700
        //   }
        // },
        title: {
          text: '主标题',
          // 副标题
          subtext: '50%',
          // 主副标题间距
          itemGap: 40,
          x: 'center',
          y: 'center',
          top: '280',
          // 主标题样式
          textStyle: {
            fontSize: '12',
            color: '#ffffff',
            textAlign: 'center',
            fontWeight: 500
          },
          // 副标题样式
          subtextStyle: {
            fontSize: '10',
            fontWeight: '400',
            color: '#606266'
          }
        },
        series: [
          {
            name: '客户年龄分布',
            type: 'pie',
            radius: ['55%', '70%'],
            center: ['50%', '50%'],
            avoidLabelOverlap: false,
            label: {
              normal: {
                show: false,
                position: 'center'
              },
              show: false,
              position: 'center'
            },
            emphasis: {
              label: {
                show: false,
                fontSize: '14',
                fontWeight: 'bold'
              }
            },
            labelLine: {
              show: false
            },
            data: this.viewData
          }
        ]
      }
      this.echartsPie.setOption(option)
      this.pieActive()
      // 鼠标划出
      this.echartsPie.on('mouseout', () => {
        this.pieActive()
      })
    },
    //高亮
    pieActive() {
      let index = -1 //高亮所在下标
      let dataLength = this.viewData.length // 当前饼图有多少个扇形
      // 用定时器控制饼图高亮
      this.mTime = setInterval(() => {
        // 清除之前的高亮
        this.echartsPie.dispatchAction({
          type: 'downplay',
          seriesIndex: 0,
          dataIndex: index
        })
        index = (index + 1) % dataLength
        // 当前下标高亮
        this.echartsPie.dispatchAction({
          type: 'highlight',
          seriesIndex: 0,
          dataIndex: index
        })

        if (index > dataLength) {
          index = 0
        }
      }, 2000)
      // 鼠标划入
      this.echartsPie.on('mouseover', () => {
        // 停止定时器,清除之前的高亮
        clearInterval(this.mTime)
        this.echartsPie.dispatchAction({
          type: 'downplay',
          seriesIndex: 0,
          dataIndex: index
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.pie-box {
  height: 14vh;
  .pie-chart {
    flex: 1;
    height: 100%;
  }
}
</style>