ParCharts.vue 8.26 KB
<template>
  <div class="pie-box">
    <div :id="id" :style="{ height: height }" class="pie-chart"></div>
    <div v-if="!configObj.hideTip">{{ list.length ? list[0].advise : '' }}
    </div>
  </div>
</template>
<script>
import * as echarts from 'echarts'
export default {
  name: 'PieChart',
  props: {
    id: {
      type: String,
      default: ''
    },
    height: {
      type: String,
      default: '8.54vh'
    },
    list: {
      type: Array,
      default: () => [
        // {
        //   alarmName: "防拆报警",
        //   alarmLevel: 20,
        // }
      ]
    },
    configObj: {
      type: Object,
      default: () => ({})
    }, 
  },
  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]],
      //     avarage: item[keys[2]]
      //   })
      // })

    },
    draw() {
      if (this.mTime) clearInterval(this.mTime)
      this.echartsPie = echarts.init(document.getElementById(this.id))
      // 指定图表的配置项和数据
      var storedList = this.list;
      const pointerData = this.list.length ? this.list[0].value : 0
      console.log(pointerData,storedList, 'pointerData' , '---888----');
      const alarmLevel = storedList[0] && storedList[0].alarmLevel ? storedList[0].alarmLevel : 0
      this.option = {
        backgroundColor: "",
        series: [{
          type: "gauge",
          radius: '80%',
          startAngle: 225,
          endAngle: -45,
          splitNumber: 50,
          title: {
            color: 'red',
          },
          pointer: {
            itemStyle: {
              color:  this.configObj.pointerColor || '#2196F3', // 修改指针颜色
            },
          },
          splitLine: {
            show: false,
            length: 15,
            distance: -10,
            lineStyle: {
              color: "#fff",
              width: 1
            }
          },
          detail: {
            show: true,
            offsetCenter: [0, 80],
            fontSize: 12,
            formatter: function(val) {
              let value = ''
              switch (alarmLevel / 1) {
                case 1:
                  value = '极高风险';
                  break;
                case 2:
                  value = '高风险';
                  break;
                case 3:
                  value = '中风险';
                  break;
                case 4:
                  value = '低风险';
                  break;
                case 5:
                  value = '安全';
                  break;
                default:
                  value = '无风险';
              }
              if (!storedList.length) {
                return `5级警情\n{b|${value}}`;
              } else {
                return `${alarmLevel}级警情\n{b|${value}}`;
              }
            },
            rich: {
              a: {
                fontSize: 30,
                color: this.configObj.tipColor || '#ffffff',
                fontweight: 'bold',
              },
              b: {
                color: pointerData > 0 ? '#F38906' : '#4ED5D7',
                fontSize: 14,
              }
            },
            textStyle: {
              fontSize: 18,
              color: this.configObj.tipColor || '#ffffff',
              top: '80%',
            },
          },
          // 仪表盘的线,颜色值为一个数组
          axisLine: {
            show: true,
            // 两端是否设置为圆角;在5.0之后的版本有效
            roundCap: true,
            lineStyle: {
              width: 15,
              opacity: 1,
              color: [
                [pointerData / 100, {
                  x: 0,
                  y: 1,
                  x1: 1,
                  y1: 0,
                  colorStops: [
                    {
                      offset: 0,
                      color: '#4ED5D7'
                    },
                    {
                      offset: 0.3,
                      color: '#58D068'
                    },
                    {
                      offset: 0.5,
                      color: '#FBD924'
                    },
                    {
                      offset: 0.8,
                      color: '#F38906'
                    },
                    {
                      offset: 1,
                      color: '#F23204'
                    }
                  ]
                }],
                [1, this.configObj.backgroundColor || "#46495E"]
              ]
            }
          },
          // 仪表盘刻度标签
          axisLabel: {
            show: false,
            color: "rgba(84, 108, 198, 0.65)",
            fontSize: 14,
            distance: 20,
            formatter: val => {
              const num = Math.floor(val);
              return num % 20 === 0 ? num : "";
            }
          },
          // 刻度
          axisTick: {
            show: false
          },
          // 指针,此设置仅对5.0以上的版本生效
          anchor: {
            show: true,
            icon: "circle",
            showAbove: true,
            size: 15,
            itemStyle: {
              borderWidth: 3,
              borderColor: this.configObj.pointerbgColor
            }
          },
          data: [pointerData]
        }
        ]
      };
      this.echartsPie.setOption(this.option)
    },
    // 事件触发
    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: rgba(24 ,28, 54 ,0.9);
  font-weight: 500;
  font-size: 12px;
  border-radius: 8px;
  color: #A3A4AF;
  text-align: center;

  .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>