vue3+echarts实现带有间隔的双重环图

1.在template中定义

<div ref="chartRef" class="w-full h-[200px]"></div>

2.在js中完成对数据的处理


  const datalist = [
    {id: 1, name: '标题1', value: 25},
    {id: 2, name: '标题2', value: 25},
    {id: 3, name: '标题3', value: 25},
    {id: 4, name: '标题4', value: 25}
  ]
  let colorList = [
    "#D3F0FF",
    "#F05A23",
    "#00FFFF",
    "#00B2FF",
  ];
  const convertToRGBA = (color, opacity) => {
    const hex = color.replace('#', '');
    const r = parseInt(hex.substring(0, 2), 16);
    const g = parseInt(hex.substring(2, 4), 16);
    const b = parseInt(hex.substring(4, 6), 16);
    return `rgba(${r}, ${g}, ${b}, ${opacity})`;
};

  const colors = colorList.map(color => convertToRGBA(color, 0.3));
  let data = [];
  let data2 = []
  for (let i = 0; i < datalist.length; i++) {
    data.push(
      {
        value: datalist[i].value,
        name: datalist[i].name,
        itemStyle: {
          // shadowBlur: 20,
          color: colorList[i],
          // shadowColor: colorList[i],
        },
      },
      {
        value: 2,
        name: "",
        itemStyle: {
          label: {
            show: false,
          },
          labelLine: {
            show: false,
          },
          color: "rgba(0, 0, 0, 0)",
          borderColor: "rgba(0, 0, 0, 0)",
          borderWidth: 0,
        },
      }
    );
    data2.push(
      {
        value: datalist[i].value,
        name: datalist[i].name,
        itemStyle: {
          // shadowBlur: 20,
          color: colors[i],
          // shadowColor: colorList[i],
        },
      },
      {
        value: 2,
        name: "",
        itemStyle: {
          label: {
            show: false,
          },
          labelLine: {
            show: false,
          },
          color: "rgba(0, 0, 0, 0)",
          borderColor: "rgba(0, 0, 0, 0)",
          borderWidth: 0,
        },
      }
    );
    
  }

3.对圆环图进行配置

 const chartRef = ref()
  const pieChart = ref()

  function initCharts() {
    if (!pieChart.value)
        pieChart.value = markRaw(echarts.init(chartRef.value));
    else pieChart.value.clear();
    pieChart.value.setOption({
      tooltip: {
        show: true,
      },
      series: [
        {
          //外环大小
          type: 'pie',
          radius: ['58%', '68%'],
          center: ["51.5%", "41%"],
          z: 10,
          clockWise:false,
          label: {//对牵引线的内容进行自定义
            show: true,
            position: 'outer',
            align: 'left',
            clockWise:false,
            height:30,
            lineHeight: 10,
            formatter: function (params) {
              if (params.name !== "") {
                let data = datalist;
                let total = 0;
                let tarValue;
                for (let i = 0; i < data.length; i++) {
                  total += data[i].value;
                  if (data[i].name == params.name) {
                    tarValue = data[i].value;
                  }
                }
                let v = tarValue;
                let per = getPercent(v, total, 1);
                return [
                  `{a|${params.name}}\n\n` + `{c|${per}}` + `{d|%}  `,
                ];
              } else {
                return "";
              }
            },
            rich: {
              a: {
                fontSize: 16,
                color: '#c5f1ff',
                fontWeight: 400,
                fontFamily: 'Regular'
              },
              c: {
                color: '#00ffff', 
                fontSize: 24,
                fontWeight: 600,
                fontFamily: 'AGENCYR',
                padding: [10, 0, 0 , 0]
              },
              d: {
                color: '#fff', 
                fontSize: 14,
                fontWeight: 400,
                padding: [10, 0 ,0 ,5]
              }
              },
            borderWidth:10,
            padding: [0, -50],
          },
        labelLine: {
          show: true,
          length: 40,
          length2: 80,
          smooth: 0,
          lineStyle: {
            type: 'dashed'
          }
        },
          emphasis: {
            label: {
              show: true,
              fontSize: "14",
            },
          },
          data: data,
        },
        {
           //不透明的圆环的设置
          name:'',
          radius: ['45%','68%'],
          center: ["51.5%", "41%"],
          type:'pie',
          gap: 1.71,
          clockWise:false,
          itemStyle: {
            normal: {
              color: function (params) {
                return colorList[params.dataIndex % colorList.length] + "40";
              },
            }
          },
          label: {
            normal: {
              show:false
            },
            emphasis: {
              show:false
            }
          },
          labelLine: {
            normal: {
              show:false
            },
            emphasis: {
              label: {
                show: true,
                fontSize: "14",
              },
            },
          },
          animation:true,
          tooltip: {
            show:true
          },
          silent: true,
          data: data2
        }
      ],
    });
  }
  onMounted(() => {
    initCharts()
  })

最后就能实现双重的圆环图效果

  • 12
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值