PieChart.vue 4.76 KB
<template>
  <div class="pie-box" >
    <div :id="id" :style="{ width: width, height: height }" class="pie-chart"></div>
  </div>
</template>

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

  props: {
    id: {
      type: String,
      default: ''
    },
    keys: {
      type: Array,
      default: () => ['name', 'number']
    },
    height: {
      type: String,
      default: '12.7vh'
    },
    width: {
      type: String,
      default: '7.2vw'
    },
    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' }
      ]
    },
    radius: {
      type: Array,
      default: () => ['55%', '72%']
    },
    colors: {
      type: Array,
      default: () => ['#4FA0FF', '#7CFFB2', '#FDF006', '#EB6001', '#F5AA01', '#BF98FF', '#58D9F9']
    }
  },

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

  created() {
    this.initData()
  },

  mounted() {
    let id = this.id
    this.echartsPie = echarts.init(document.getElementById(id))
    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() {
      // 指定图表的配置项和数据
      this.option = {
        // tooltip: {
        //   trigger: 'item'
        // },

        color: this.colors,
        title: {
          text: this.text,
          // 副标题
          subtext: this.subtext,
          // 主副标题间距
          itemGap: 5,
          x: 'center',
          y: 'center',
          top: '50',
          // 主标题样式
          textStyle: {
            fontSize: '12',
            color: '#ffffff',
            textAlign: 'center',
            fontWeight: 500
          },
          // 副标题样式
          subtextStyle: {
            fontSize: '10',
            fontWeight: '400',
            color: '#606266'
          }
        },
        series: [
          {
            name: '客户年龄分布',
            type: 'pie',
            radius: this.radius,
            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
            },
            animationType: 'scale',
            animationEasing: 'elasticOut',
            animationDurationUpdate: 3000,
            animationTypeUpdate: 'transition',
            animation: true,
            data: this.viewData
          }
        ]
      }
      this.echartsPie.setOption(this.option)
      this.pieActive()
      // 鼠标划出
      this.echartsPie.on('mouseout', () => {
        this.pieActive()
      })
    },
    // 事件触发
    emitEvent(data) {
      this.text = data.name
      this.subtext = data.value
      this.echartsPie.setOption(this.option)
      this.$emit('pieAction', data)
    },
    //高亮
    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
        }
        this.emitEvent(this.viewData[index])
      }, 2000)
      // 鼠标划入
      this.echartsPie.on('mouseover', data => {
        // 停止定时器,清除之前的高亮
        clearInterval(this.mTime)
        this.echartsPie.dispatchAction({
          type: 'downplay',
          seriesIndex: 0,
          dataIndex: index
        })
        console.log('mouseover')
        console.log(data)
        this.emitEvent(data.data)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.pie-box {
  background: #262525;
  .pie-chart {
  }
}
</style>