PieChart.vue 5.73 KB
<template>
  <div class="pie-box">
    <div :id="id" :style="{ height: height }" class="pie-chart"></div>
    <div class="center-content">
      <div :class="[id, 'center-content-num']"></div>
      <div class="center-content-desc">{{ numDesc }}</div>
    </div>
  </div>
</template>

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

  props: {
    id: {
      type: String,
      default: ''
    },
    keys: {
      type: Array,
      default: () => ['name', 'value']
    },
    height: {
      type: String,
      default: '12.7vh'
    },
    list: {
      type: Array,
      default: () => []
    },
    radius: {
      type: Array,
      default: () => ['55%', '75%']
    },
    colors: {
      type: Array,
      default: () => ['#4FA0FF', '#FF6E76', '#7CFFB2','#FDF006','#EB6001','#F5AA01','#BF98FF','#4FA0FF']
    }
  },

  data() {
    return {
      option: '',
      echartsPie: '',
      mTime: '',
      viewData: [],
      text: '',
      subtext: '',
      currentIndex: -1,
      mainValue: 0,
      numDesc: ''
    }
  },
  watch: {
    list: {
      handler(newVal) {
        if (newVal.length > 0) {
          this.initData()
          this.$nextTick(() => {
            this.draw()
          })
        }
      },
      immediate: true
    }
  },

  mounted() {
    // this.initData()
    // this.draw()
  },

  methods: {
    //初始化数据
    initData() {
      let { list, keys } = this
      this.viewData = []
      list.forEach(item => {
        this.viewData.push({
          name: item[keys[0]],
          value: item[keys[1]]
        })
      })
    },
    draw() {
      if (this.mTime) clearInterval(this.mTime)
      this.echartsPie = echarts.init(document.getElementById(this.id))
      window.addEventListener('resize', () => {
        this.echartsPie.resize()
      })

      // 指定图表的配置项和数据
      this.option = {
        // tooltip: {
        //   trigger: 'item'
        // },

        color: this.colors,
        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
            },
            data: this.viewData
          }
        ]
      }
      this.echartsPie.clear()
      this.mainValue = 0
      this.numDesc = ''
      this.setAnamiteText()
      this.echartsPie.setOption(this.option)

      this.mTime = setInterval(this.pieActive, 3000)
      // 鼠标划出
      this.echartsPie.on('mouseout', () => {
        if (this.mTime) clearInterval(this.mTime)
        this.mTime = setInterval(this.pieActive, 3000)
      })
      this.echartsPie.on('mouseover', data => {
        this.mainValue = data.data.value
        this.numDesc = data.data.name
        this.setAnamiteText()
        // 停止定时器,清除之前的高亮
        clearInterval(this.mTime)
        this.currentIndex = data.dataIndex
        this.highlightPie()
        this.emitEvent(data.data)
      })
    },
    // 事件触发
    emitEvent(data) {
      this.text = data.name
      this.subtext = data.value
      this.echartsPie.setOption(this.option)
      this.$emit('pieAction', data)
    },
    highlightPie() {
      // 取消所有高亮并高亮当前图形
      // 遍历饼图数据,取消所有图形的高亮效果
      for (var idx in this.option.series[0].data)
        this.echartsPie.dispatchAction({
          type: 'downplay',
          seriesIndex: 0,
          dataIndex: idx
        })
      // 高亮当前图形
      this.echartsPie.dispatchAction({
        type: 'highlight',
        seriesIndex: 0,
        dataIndex: this.currentIndex
      })
      //高亮项this.currentIndex
      this.mainValue = this.option.series[0].data[this.currentIndex].value
      this.numDesc = this.option.series[0].data[this.currentIndex].name
      this.setAnamiteText()
    },
    setAnamiteText() {
      if( document.querySelector(`.${this.id}`)) document.querySelector(`.${this.id}`).style = `--step:${this.mainValue}`
    },
    //高亮
    pieActive() {
      const dataLen = this.option.series[0].data.length
      this.currentIndex = (this.currentIndex + 1) % dataLen
      this.highlightPie()
      this.emitEvent(this.viewData[this.currentIndex])
    }
  }
}
</script>

<style lang="scss" scoped>
.pie-box {
  background: #262525;
  position: relative;
  flex: 1;
  .center-content {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
    max-width: 40%;
    &-num {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      font-size: 0.8vw;
      color: #fff;
      --step: 0;
      counter-reset: count var(--step);
      z-index: var(--step);
      transition: --step 1s;
      &::before {
        content: counter(count);
      }
    }
    &-desc {
      font-size: 0.5vw;
      color: #606266;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      animation: rotateIn 2s infinite;
    }
  }
  .pie-chart {
  }
}
@property --step {
  syntax: '<integer>';
  inherits: false;
  initial-value: 15;
}
@keyframes rotateIn {
  from {
    transform: rotateY(0turn);
  }
  to {
    transform: rotateY(-1turn);
  }
}
// ​ @keyframes rotateIn {
//   from {
//     transform: rotateZ(0deg);
//   }
//   to {
//     transform: rotateZ(3600deg);
//   }
// }
</style>