Echarts 圆环 一类数据对应一环

这篇博客详细介绍了如何使用Vue.js和ECharts库创建一个展示风险等级分布的饼图。代码中展示了如何配置不同等级的颜色、阴影和透明度,并通过多个重叠的饼图层次来呈现各等级的占比,以及整体的百分比显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

new Vue({
  el: "#app",
  data() {
    return {
      riskLegendList: [1, 2, 3, 4], // 各等级预警数量
      riskColor: ['#24d29b', '#69cde1', '#fdd27b', '#ff8c53'],
      riskColorOpacity: ['rgba(36, 210, 155,0.2)', 'rgba(105, 205, 225,0.2)', 'rgba(253, 210, 123,0.2)','rgba(255, 140, 83,0.2)'],
    }
  },
  created() {

  },
  mounted() {
    this.initChart(this.riskLegendList[0], this.riskLegendList[1], this.riskLegendList[2], this.riskLegendList[3], this.riskLegendList.reduce((previousValue, currentItem) => previousValue + currentItem, 0));
  },
  methods: {


    initChart(level4Count, level5Count, level6Count, level7Count, levelTotalCount) {
      levelTotalCount = levelTotalCount === 0 ? 1 : levelTotalCount;
      let dataStyle = {
        normal: {
          label: {
            show: false
          },
          labelLine: {
            show: false
          },
          shadowBlur: 40,
          borderWidth: 10,
          shadowColor: 'rgba(0, 0, 0, 0)' //边框阴影
        }
      };

      let option = {
        title: {
          show: false,
        },
        legend: {
          show: false
        },
        tooltip: {
          trigger: 'item',
          show: true,
          formatter: "{b} : <br/>{d}% {c}",

          padding: [8, 10], //内边距
          extraCssText: 'box-shadow: 0 0 3px rgba(255, 255, 255, 0.1);', //添加阴影
        },
        backgroundColor: 'rgb(255,255,255,0)',
        series: [{
          name: 'Level7',
          type: 'pie',
          clockWise: false,
          radius: [116, 130],
          center: ['50%', '50%'],
          itemStyle: dataStyle,
          hoverAnimation: false,
          startAngle: 90,
          label: {
            borderRadius: '10',
          },
          data: [{
            value: level7Count,
            name: '7级',
            itemStyle: {
              normal: {
                color: this.riskColor[0]
              }
            }
          },
            {
              value: levelTotalCount - level7Count,
              name: '',
              tooltip: {
                show: false
              },
              itemStyle: {
                normal: {
                  color: this.riskColorOpacity[0],
                  label: {
                    show: false
                  },
                  labelLine: {
                    show: false
                  }
                },
                emphasis: {
                  color: this.riskColorOpacity[0]
                }
              }
            },
          ]
        }, {
          name: 'Level7',
          type: 'pie',
          clockWise: false,
          radius: [116, 130],
          center: ['50%', '50%'],
          itemStyle: dataStyle,
          hoverAnimation: false,
          startAngle: 90,
          label: {
            borderRadius: '10',
          },
          data: [{
            value: level7Count,
            name: '7级',
            itemStyle: {
              normal: {
                color: this.riskColor[0]
              }
            }
          },
            {
              value: levelTotalCount - level7Count,
              name: '',
              tooltip: {
                show: false
              },
              itemStyle: {
                normal: {
                  color: 'transparent',
                  label: {
                    show: false
                  },
                  labelLine: {
                    show: false
                  }
                },
                emphasis: {
                  color: this.riskColorOpacity[0]
                }
              }
            },
          ]
        },
          {
            name: 'Level6',
            type: 'pie',
            clockWise: false,
            radius: [86, 100], // 灰色线弧度
            center: ['50%', '50%'],
            itemStyle: dataStyle,
            hoverAnimation: false,
            startAngle: 90,
            data: [{
              value: level6Count,
              name: '6级',
              itemStyle: {
                normal: {
                  color: this.riskColor[1]
                }
              }
            },
              {
                value: levelTotalCount - level6Count,
                name: '',
                tooltip: {
                  show: false
                },
                itemStyle: {
                  normal: {
                    color: this.riskColorOpacity[1],
                    label: {
                      show: false
                    },
                    labelLine: {
                      show: false
                    }
                  },
                  emphasis: {
                    color: this.riskColorOpacity[1]
                  }
                }
              },
            ]
          },
          {
            name: 'Level6',
            type: 'pie',
            clockWise: false,
            radius: [86, 100],
            center: ['50%', '50%'],
            itemStyle: dataStyle,
            hoverAnimation: false,
            startAngle: 90,
            data: [{
              value: level6Count,
              name: '6级',
              itemStyle: {
                normal: {
                  color: this.riskColor[1]
                }
              }
            },
              {
                value: levelTotalCount - level6Count,
                name: '',
                tooltip: {
                  show: false
                },
                itemStyle: {
                  normal: {
                    color: 'transparent',
                    label: {
                      show: false
                    },
                    labelLine: {
                      show: false
                    }
                  },
                  emphasis: {
                    color: this.riskColorOpacity[1]
                  }
                }
              },
            ]
          },
          {
            name: 'Level5',
            type: 'pie',
            clockWise: false,
            radius: [56, 70],
            center: ['50%', '50%'],
            itemStyle: dataStyle,
            hoverAnimation: false,
            startAngle: 90,
            data: [{
              value: level5Count,
              name: '5级',
              itemStyle: {
                normal: {
                  color: this.riskColor[2]
                }
              }
            },
              {
                value: levelTotalCount - level5Count,
                name: '',
                tooltip: {
                  show: false
                },
                itemStyle: {
                  normal: {
                    color: this.riskColorOpacity[2],
                    label: {
                      show: false
                    },
                    labelLine: {
                      show: false
                    }
                  },
                  emphasis: {
                    color: this.riskColorOpacity[2]
                  }
                }
              },
            ]
          },
          {
            name: 'Level5',
            type: 'pie',
            clockWise: false,
            radius: [56, 70],
            center: ['50%', '50%'],
            itemStyle: dataStyle,
            hoverAnimation: false,
            startAngle: 90,
            data: [{
              value: level5Count,
              name: '5级',
              itemStyle: {
                normal: {
                  color: this.riskColor[2]
                }
              }
            },
              {
                value: levelTotalCount - level5Count,
                name: '',
                tooltip: {
                  show: false
                },
                itemStyle: {
                  normal: {
                    color: 'transparent',
                    label: {
                      show: false
                    },
                    labelLine: {
                      show: false
                    }
                  },
                  emphasis: {
                    color: this.riskColorOpacity[2]
                  }
                }
              },
            ]
          },
          {
            name: 'Level4',
            type: 'pie',
            clockWise: false,
            radius: [26, 40],
            center: ['50%', '50%'],
            itemStyle: dataStyle,
            hoverAnimation: false,
            startAngle: 90,
            data: [{
              value: level4Count,
              name: '4级及以上',
              itemStyle: {
                normal: {
                  color: this.riskColor[3]
                }
              }
            },
              {
                value: levelTotalCount - level4Count,
                name: '',
                tooltip: {
                  show: false
                },
                itemStyle: {
                  normal: {
                    color: this.riskColorOpacity[3],
                    label: {
                      show: false
                    },
                    labelLine: {
                      show: false
                    }
                  },
                  emphasis: {
                    color: this.riskColorOpacity[3]
                  }
                }
              },
            ]
          },
          {
            name: 'Level4',
            type: 'pie',
            clockWise: false,
            radius: [26, 40],
            center: ['50%', '50%'],
            itemStyle: dataStyle,
            hoverAnimation: false,
            startAngle: 90,
            data: [{
              value: level4Count,
              name: '4级及以上',
              itemStyle: {
                normal: {
                  color: this.riskColor[3]
                }
              }
            },
              {
                value: levelTotalCount - level4Count,
                name: '',
                tooltip: {
                  show: false
                },
                itemStyle: {
                  normal: {
                    color: 'transparent',
                    label: {
                      show: false
                    },
                    labelLine: {
                      show: false
                    }
                  },
                  emphasis: {
                    color: this.riskColorOpacity[3]
                  }
                }
              },
            ]
          }
        ]
      };
      let chart = document.getElementById("chart");
      echarts.init(chart).setOption(option);
    }

  }
})

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值