allPieChart.vue 8.09 KB
<template>
  <div :class="className" :style="{height:height,width:width}"></div>
</template>

<script>
import echarts from 'echarts'
import {debounce} from '../utils/debounce'

export default {
  name: 'allPieChart',
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      chart: null,
    }
  },
  watch: {
    chartData: {
      deep: true,
    }
  },
  mounted() {
    this.initChart()
    if (this.autoResize) {
      this.__resizeHandler = debounce(() => {
        if (this.chart) {
          this.chart.resize()
        }
      }, 100)
      window.addEventListener('resize', this.__resizeHandler)
    }
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    if (this.autoResize) {
      window.removeEventListener('resize', this.__resizeHandler)
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    setOptions(data) {
      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: "{b} : {c} ({d}%)"
        },
        series: [
          {
            type: 'pie',
            radius: ['30%', '31%'],
            center: ['50%', '50%'],
            hoverAnimation:false,
            tooltip: {
              show:false
            },
            label: {
              show: false
            },
            lableLine: {
              show: false
            },
            data: [
              {
                value: 0,
              },
              {
                value: 55,
                itemStyle: {
                  normal: {
                    color: 'rgba(89,170,247,.3)'
                  }
                }
              },

            ]
          },
          {
            type: 'pie',
            radius: ['40%', '41%'],
            center: ['50%', '50%'],
            hoverAnimation:false,
            tooltip: {
              show:false
            },
            label: {
              show: false
            },
            lableLine: {
              show: false
            },
            data: [
              {
                value: 0,
              },
              {
                value: 55,
                itemStyle: {
                  normal: {
                    color: 'rgba(89,170,247,.3)'
                  }
                }
              },

            ]
          },
          {
            type: 'pie',
            radius: ['50%', '51%'],
            center: ['50%', '50%'],
            hoverAnimation:false,
            tooltip: {
              show:false
            },
            label: {
              show: false
            },
            lableLine: {
              show: false
            },
            data: [
              {
                value: 0,
              },
              {
                value: 55,
                itemStyle: {
                  normal: {
                    color: 'rgba(89,170,247,.3)'
                  }
                }
              },

            ]
          },
          {
            type: 'pie',
            radius: ['60%', '61%'],
            center: ['50%', '50%'],
            hoverAnimation:false,
            tooltip: {
              show:false
            },
            label: {
              show: false
            },
            lableLine: {
              show: false
            },
            data: [
              {
                value: 0,
              },
              {
                value: 55,
                itemStyle: {
                  normal: {
                    color: 'rgba(89,170,247,.3)'
                  }
                }
              },

            ]
          },
          {
            type: 'pie',
            radius: ['70%', '71%'],
            center: ['50%', '50%'],
            hoverAnimation:false,
            tooltip: {
              show:false
            },
            label: {
              show: false
            },
            lableLine: {
              show: false
            },
            data: [
              {
                value: 0,
              },
              {
                value: 55,
                itemStyle: {
                  normal: {
                    color: 'rgba(89,170,247,.3)'
                  }
                }
              },

            ]
          },
          {
            type: 'pie',
            radius: ['80%', '81%'],
            center: ['50%', '50%'],
            hoverAnimation:false,
            tooltip: {
              show:false
            },
            label: {
              show: false
            },
            lableLine: {
              show: false
            },
            data: [
              {
                value: 0,
              },
              {
                value: 55,
                itemStyle: {
                  normal: {
                    color: 'rgba(89,170,247,.3)'
                  }
                }
              },

            ]
          },
          {
            type: 'pie',
            radius: ['90%', '91%'],
            center: ['50%', '50%'],
            hoverAnimation:false,
            tooltip: {
              show:false
            },
            label: {
              show: false
            },
            lableLine: {
              show: false
            },
            data: [
              {
                value: 0,
              },
              {
                value: 55,
                itemStyle: {
                  normal: {
                    color: 'rgba(89,170,247,.3)'
                  }
                }
              },

            ]
          },


          {
          name: '内环',
          type: 'pie',
          silent: true,
          clockWise: true,
          hoverAnimation: false,
          animationType: 'scale',
          radius: ['10%','16%'],
          center: ['50%', '50%'],
          label: {
            normal: {
              position: 'center'
            }
          },
          data: [{
            value: 100,
            itemStyle: {
              normal: {
                color: {
                  colorStops: [{
                    offset: 0,
                    color: '#157ADB' // 0% 处的颜色
                  }, {
                    offset: 1,
                    color: '#157ADB' // 100% 处的颜色
                  }]
                }
              }
            }
          }]
        },

          {
            name: '半径模式',
            type: 'pie',
            radius: ['20%', '80%'],
            center: ['50%', '50%'],
            hoverAnimation:false,
            roseType: 'radius',
            label: {
              show: false
            },
            lableLine: {
              show: false
            },
            data: [
              {
                value: data[0],
                name: '诱导屏',
                itemStyle: {
                  normal: {
                    color: {
                      colorStops: [{
                        offset: 0,
                        color: '#FFBA00' // 0% 处的颜色
                      }, {
                        offset: 1,
                        color: '#FF8100' // 100% 处的颜色
                      }]
                    },
                  }
                }
              },
              {
                value: data[1],
                name: '道闸',
                itemStyle: {
                  normal: {
                    color: {
                      colorStops: [{
                        offset: 0,
                        color: '#00CAFE' // 0% 处的颜色
                      }, {
                        offset: 1,
                        color: '#2772F4' // 100% 处的颜色
                      }]
                    },
                  }
                }
              },

            ],
            animationType: 'scale',
            animationEasing: 'elasticOut'
          }

        ]
      })
    },
    initChart() {
      this.chart = echarts.init(this.$el)
      this.setOptions(this.chartData)
    }
  }
}
</script>