echarts图例旁边加百分比及百分比对齐

一、效果图

在这里插入图片描述

二、代码

javascript 复制代码
import cirle from '@/assets/imgs/dataScree/ybp.png'

let option={
    tooltip: {
      trigger: 'item',
      formatter: function (params) {
        return ''
      }
    },
    legend: {
      orient: 'vertical', // 图例列表的布局朝向,'horizontal'为水平,'vertical'为垂直
      left: '230', // 图例组件离容器左侧的距离
      aligh: 'right',
      top: '20',
      icon: 'circle',
      itemWidth: 8, // 设置图例宽度
      itemHeight: 8, // 设置图例高度
      itemGap: 19,
      textStyle: {
        color: '#FFFFFF',
        fontSize: 11,
        rich: {
        //第一列样式
          oneone: {
            width: 80
          },
          //百分比列样式
          twotwo: {
            width: 30,
            color: '#FFFFFF',
            fontSize: 14,
            fontWeight: 'bolder'
          }
        }
      },
      // 图例百分比计算
      formatter(name) {
        let percentage = 0
        for (let i = 0; i < data1.length; i++) {
          percentage += data1[i].value
        }
        let result = data1.find((item) => item.name == name)
        return `{oneone|${result.name}}{twotwo|${Math.round((result.value / percentage) * 100)} %}`
      }
    },
    series: [
      {
        name: '',
        type: 'pie',
        data: data1,
        radius: ['78%', '88%'],
        center: ['30%', '50%'], // 将饼图位置设置在容器的中间垂直偏下的位置
        avoidLabelOverlap: false,
        padAngle: 3,
        itemStyle: {
          normal: {
            backgroundColor: function (params) {
              var colorList = [
                '#20D49F',
                '#EA9F53',
                '#27FFF7',
                '#275EFF',
                '#DCC821'
              ]
              return colorList[params.dataIndex]
            }
          }
        },
        label: {
          show: false,
          position: 'center'
        },
        rich: {
          percent: {
            fontSize: 24
          }
        },
        labelLine: {
          show: true
        }
      }
    ],
    graphic: [
      {
        type: 'image',
        id: 'background',
        left: '26',
        top: 'center',
        z: -9, // 确保背景图在饼图下层
        bounding: 'raw',
        origin: [200, 200],
        style: {
          image: cirle, // 背景图片地址
          width: 175,
          height: 175,
          opacity: 1
        }
      }
    ]
  }

三、图例百分比主要是legend中的formatter这块,通过textStyle中的rich调整饼图图例的样式

javascript 复制代码
// 图例百分比计算
      formatter(name) {
        let percentage = 0
        for (let i = 0; i < data1.length; i++) {
          percentage += data1[i].value
        }
        let result = data1.find((item) => item.name == name)
        return `{oneone|${result.name}}{twotwo|${Math.round((result.value / percentage) * 100)} %}`
      }

四、如果想实现以下效果,主要是需要配置label这个配置项

  • 代码
javascript 复制代码
  series: [
          {
            name: this.bieText,
            type: "pie",
            radius: "50%",
            data: this.bieData,
            label: {
              normal: {
                show: true,
                formatter: '{b}{d}%'
  // formatter: '{b}: {c}({d}%)' //自定义显示格式(b:name(当前数据的名称), c:value(当前数据的值), d:百分比)
              }
            },
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
相关推荐
是席木木啊9 小时前
RuoYi-Vue-Plus UI前端页面布局调整
前端·vue.js·ui
Web极客码11 小时前
WordPress 6.8有哪些新特性
前端·javascript·html
UXbot16 小时前
UI设计工具推荐合集
前端·人工智能·ui
码路星河16 小时前
基于 Vue + VueUse 的 WebSocket 优雅封装:打造高可用的全局连接管理方案
javascript·vue.js·websocket
摘星编程16 小时前
React Native + OpenHarmony:Accelerometer计步器代码
javascript·react native·react.js
敲敲了个代码16 小时前
如何优化批量图片上传?队列机制+分片处理+断点续传三连击!(附源码)
前端·javascript·学习·职场和发展·node.js
@AfeiyuO16 小时前
Vue 引入全局样式scss
前端·vue·scss
光影少年16 小时前
flex布局和grid布局区别,实现两边固定布局中间自适应
前端·css3·web·ai编程
全栈测试笔记17 小时前
异步函数与异步生成器
linux·服务器·前端·数据库·python
EndingCoder17 小时前
配置 tsconfig.json:高级选项
linux·前端·ubuntu·typescript·json