echarts 饼图渐变色+中间文字+图例后加数据

option = {
  title:{
    text: "{name|饼图渐变}",
    top: 580,
    left: "center",
    textStyle: {
      rich: {
        name: {
          fontSize: 30,
          lineHeight: 20,
          fontWeight: 400,
          color: "rgba(190, 229, 251, 1)",
          textShadowColor:"#158EFF",
          textShadowBlur: 18,
          
        },
      },
    },
  },
  tooltip: {
    trigger: 'item'
  },
  legend: {
    bottom:'5%',
    left: 'center',
    selectedMode: false,
    itemHeight: 16,  // 图例图形的高
    itemWidth: 16,   // 图例图形的宽
    orient: "vertical",  // 竖排
    textStyle: {
			rich: {
				a: {
					align: "left",  // 图例文字的对齐方式
					width: 120      // w文字宽度
				},
				b: {
					align: "left",
					fontWeight: "bold",
					width: 40
				}
			}
		},
    formatter: (name) => {  // 图例后面加数据
        var data = option.series[0].data;//获取series中的data
        var total = 0;
        var tarValue;
        for (var i = 0, l = data.length; i < l; i++) {
            total += data[i].value;
            if (data[i].name == name) {
                tarValue = data[i].value;
            }
        }
        var p = ((tarValue / total) * 100).toFixed(0); // 不保留小数
        return `{a|${name}}{b|${p}%}`;
    },
  },
  series: [
    {
      name: 'Access From',
      type: 'pie',
      radius: ['40%', '70%'], // 饼图的大小
      center: ["50%", "34%"], // 饼图上下左右位置
      avoidLabelOverlap: false,
      label: {
        show: false,
      },
      emphasis: {
        label: {
          show: false,
        }
      },
      labelLine: {
        show: false
      },
      data: [
        { value: 1048, name: 'Search Engine' ,itemStyle: {
        normal: {//颜色渐变 如果echarts 报错未定义 就将echarts改为this.$echarts
            color: new echarts.graphic.LinearGradient(
                1, 1, 1, 0,  //(上-下 渐变)
                // 1, 1, 0, 0,  //(左上-右下 渐变)
                // 1, 0, 0, 0,  //(左-右 渐变)
                // 0, 1, 1, 0,  //(右上-左下 渐变)
                // 0, 1, 1, 1,  //(左-右 渐变)
                // 1, 0, 1, 1,  //(上-下 渐变)
                // 0, 0, 1, 0,  //(左-右 渐变)
                // 0, 0, 0, 1,  //(上-下 渐变)
                [
                    {offset: 0, color: 'rgba(118, 255, 255, 0.2)'},
                    {offset: 1, color: 'rgba(118, 255, 255, 1)'}, 
                ]
            )
        }
      },},
        { value: 735,
          name: 'Direct' ,
          itemStyle: {
            color: new echarts.graphic.LinearGradient( 1, 1, 1, 0,
                [
                    {offset: 0, color: 'rgba(251, 233, 71, 0.2)'},
                    {offset: 1, color: 'rgba(251, 233, 71, 1)'}, 
                ]
            )
          },
        },
        { value: 580, name: 'Email',
          itemStyle: {
            color: new echarts.graphic.LinearGradient( 1, 1, 1, 0,
                [
                    {offset: 0, color: 'rgba(255, 90, 90, 0.2)'},
                    {offset: 1, color: 'rgba(255, 90, 90, 1)'}, 
                ]
            )
          },
        },
        { value: 484, name: 'Union Ads',
          itemStyle: {
            color: new echarts.graphic.LinearGradient( 1, 1, 1, 0,
                [
                    {offset: 0, color: 'rgba(82, 172, 255, 0.2)'},
                    {offset: 1, color: 'rgba(82, 172, 255, 1)'}, 
                ]
            )
          },
        },
      ]
    }
  ]
};

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要实现Echarts饼图显示名称和占比,可以使用以下代码配置: 引用\[1\]: ```javascript series: \[{ name: '', type: 'pie', radius: '65%', center: \['50%', '50%'\], label: { normal: { formatter: '{b}:{d}%', textStyle: { fontWeight: 'normal', fontSize: 15 } } }, data: \[\], itemStyle: { emphasis: { shadowBlur: 10, shadowOffsetX: 0, shadowColor: 'rgba(0, 0, 0, 0.5)' } } }\] ``` 这段代码中,通过设置`formatter: '{b}:{d}%'`来显示名称和占比,其中`{b}`表示名称,`{d}`表示占比。可以根据需要自定义格式。 引用\[2\]: ```javascript series: \[{ name: '产量', type: 'pie', center: \['22%', '50%'\], radius: \['0', '82%'\], avoidLabelOverlap: false, label: { show: true, position: "inside", formatter: `{b}:{d}%`, color: "#ffffff", fontSize: 12 }, data: this.rows }\] ``` 这段代码中,通过设置`formatter: `{b}:{d}%``来显示名称和占比,其中`{b}`表示名称,`{d}`表示占比。可以根据需要自定义格式。 综上所述,以上两段代码都可以实现Echarts饼图显示名称和占比。 #### 引用[.reference_title] - *1* [Echarts饼图显示数值和百分比](https://blog.csdn.net/yayayu_/article/details/105657717)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [echarts饼图显示占比(内部和外部显示百分比设置)](https://blog.csdn.net/weixin_53791978/article/details/127669321)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [echarts饼图pie中间显示总数](https://blog.csdn.net/BDawn/article/details/123841786)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值