PieChartShadow.vue 7.21 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: 'PieChartShadow',

  props: {
    id: {
      type: String,
      default: ''
    },
    keys: {
      type: Array,
      default: () => ['name', 'value']
    },
    height: {
      type: String,
      default: '8.54vh'
    },
    list: {
      type: Array,
      default: () => []
    },
    radius: {
      type: Array,
      default: () => ['60%', '89%']
    },
    colors: {
      type: Array,
      default: () => ['#16E193', '#747DFF', '#E588FF', '#34AFFF', '#FFC200', '#EB6001']
    },
    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() {},

  methods: {
    // 初始化数据
    initData() {
      const { 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%'],
            roseType: 'radius',
            avoidLabelOverlap: false,
            label: {
              normal: {
                show: false,
                position: 'center'
              },
              show: false,
              position: 'center'
            },
            emphasis: {
              label: {
                show: true,
                fontSize: this.size,
                fontWeight: 'bold',
                color: '#fff',
                formatter: '{b}'
              }
            },
            labelLine: {
              show: false
            },
            data: this.viewData,
            minAngle: 30
          },
          {
            name: '外边框',
            type: 'pie',
            tooltip: {
              show: false
            },
            clockWise: false, // 顺时加载
            hoverAnimation: false, // 鼠标移入变大
            center: ['50%', '50%'], // 这里跟上面那组一样即可
            radius: ['80%', '80%'], // 这里根据自己的需要自行调整,但是两个值要一样大哦,如果小于上方设置的最小内圆30%则为内阴影,大于外圆60%则为外阴影
            label: {
              normal: {
                show: false // 重点:此处主要是为了不展示data中的value和name
              }
            },
            data: [
              {
                value: 1, // 此处的值无所谓是多少
                name: '', // 因为不展示label,可不填
                itemStyle: {
                  // 边框样式,此处我们设置的浅蓝色,颜色可自行修改
                  normal: {
                    borderWidth: 20, // 边框宽度
                    borderColor: 'rgba(94, 183, 249,  0.13)'// 边框颜色
                  }
                }
              }
            ]
          }
        ]
      }
      this.echartsPie.clear()
      this.mainValue = 0
      this.numDesc = ''
      this.echartsPie.setOption(this.option)
      this.pieActive()
      this.mTime = setInterval(this.pieActive, 1000)
      // 鼠标划出
      this.echartsPie.on('mouseout', () => {
        if (this.mTime) clearInterval(this.mTime)
        this.mTime = setInterval(this.pieActive, 15000)
      })
      this.echartsPie.on('mouseover', data => {
        console.log(222);
        console.log(data);
        this.mainValue = data.data.value
        this.numDesc = data.data.name
        this.descColor = data.color
        // 停止定时器,清除之前的高亮
        clearInterval(this.mTime)
        this.currentIndex = data.dataIndex
        this.highlightPie()
        this.emitEvent(data.data)
      })
    },
    // 事件触发
    emitEvent(data) {
      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.descColor = this.option.color[this.currentIndex]
    },
    // 高亮
    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>