PieChart.vue 7.04 KB
<template>
  <div class="pie-box">
    <div :id="id" :style="{ height: height }" class="pie-chart"></div>
    <div class="center-content" :style="{ fontSize: size }">
      <div :class="[id, 'center-content-num', 'center-content-num-color']"></div>
    </div>
    <div class="content-desc">
      <div class="content-desc-icon" :style="{ background: descColor }"></div>
      {{ numDesc }}
    </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: () => ['50%', '70%']
    },
    colors: {
      type: Array,
      default: () => ['#4FA0FF', '#FF6E76', '#7CFFB2', '#FDF006', '#EB6001', '#F5AA01', '#BF98FF', '#58D9F9']
    },
    size: {
      type: String,
      default: '0.6vw'
    }
  },

  data() {
    return {
      option: '',
      echartsPie: '',
      mTime: '',
      viewData: [],
      text: '',
      subtext: '',
      currentIndex: -1,
      mainValue: 0,
      numDesc: '',
      descColor: ''
    }
  },
  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,
            minAngle: 30
          }
        ]
      }
      this.echartsPie.clear()
      this.mainValue = 0
      this.numDesc = ''
      //   this.setAnamiteText()
      this.echartsPie.setOption(this.option)
      this.pieActive()
      this.mTime = setInterval(this.pieActive, 15000)
      // 鼠标划出
      this.echartsPie.on('mouseout', () => {
        if (this.mTime) clearInterval(this.mTime)
        this.mTime = setInterval(this.pieActive, 15000)
      })
      this.echartsPie.on('mouseover', data => {
        this.mainValue = data.data.value
        this.numDesc = data.data.name
        this.descColor = data.color
        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.descColor = this.option.color[this.currentIndex]
      this.setAnamiteText()
    },
    setAnamiteText() {
      // document.querySelector(`.${this.id}`).nextElementSibling.style = 'animation-iteration-count:infinite;'
      // if (document.querySelector(`.${this.id}`))
      if (document.querySelector(`.${this.id}`)) {
        if (typeof this.mainValue == 'string') {
          let dom = document.querySelector(`.${this.id}`)
          dom.classList.remove('center-content-num')
          dom.innerText = this.mainValue + '%'
        } else {
          document.querySelector(`.${this.id}`).style = `--step:${this.mainValue}`
        }
      }
      // document.querySelector("div").style.setProperty("--percent", Math.random())
      // setTimeout(() => {
      // document.querySelector(`.${this.id}`).nextElementSibling.style = 'animation-iteration-count:0'
      // }, 3000)
    },
    //高亮
    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-color {
      color: #fff;
    }
    &-num {
      // overflow: hidden;
      // text-overflow: ellipsis;
      // white-space: nowrap;
      //   font-size: 0.6vw;
      --step: 0;
      counter-reset: count var(--step);
      z-index: var(--step);
      transition: --step 0.5s;
      &::before {
        content: counter(count);
      }
    }
    &-desc {
      font-size: 0.5vw;
      color: #606266;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      animation: rotateIn 3s infinite;
    }
  }
  .content-desc {
    display: flex;
    align-items: center;
    font-size: 8px;
    color: #c8c8c8;
    text-align: center;
    font-weight: 400;
    position: absolute;
    top: 91%;
    left: 50%;
    transform: translateX(-50%);
    text-align: center;
    height: 14px !important;
    white-space: nowrap;
    &-icon {
      width: 8px;
      height: 4px;
      border-radius: 2px;
      margin-right: 4px;
    }
  }
}
@property --step {
  syntax: '<integer>';
  inherits: false;
  initial-value: 15;
}
@keyframes rotateIn {
  from {
    transform: rotateY(0turn);
  }
  to {
    transform: rotateY(-1turn);
  }
}
</style>