echarts饼图pie中间显示总数

echarts饼图pie中间显示总数

有这样一种需求,饼图中间显示总数,当鼠标悬浮指定区域时显示对应区域数值
网上搜了好多都是通过zlevel来实现的这种效果,但是如果echarts的背景如果不是纯色的,而是渐变色或者图片时,zlevel的方式就不好用了

实现效果

默认情况
在这里插入图片描述
鼠标悬浮到其中一个区域
在这里插入图片描述

实现代码

const myChart = echarts.init(document.getElementById('fwflEchart'))
// 重点代码
//隐藏title
const hideTitle = (e) => {
	myChart.value.setOption({
		title: {
			text: '',
		},
	})
}
//显示title
const showTitle = (e) => {
	myChart.value.setOption({
		title: {
			text: '房屋总数量\n2230',
		},
	})
}

// 给饼图添加事件
// 当区域高亮时隐藏title,比如当鼠标移动到legend上时
myChart.value.on('highlight', hideTitle)
// 当鼠标悬浮到区域时隐藏title
myChart.value.on('mouseover', hideTitle)
// 当鼠标离开区域时显示title
myChart.value.on('mouseout', showTitle)
// 当区域取消高亮时显示title,比如当鼠标从legend上离开时
myChart.value.on('downplay', showTitle)
// 设置option
myChart.setOption({
	color: ['#00d1d1', '#529fcf', '#71d0aa', '#c5ff7d',],
	title: {
		text: '总数量\n2230',
		top: '160px',
		left: '214px',
		textAlign: 'center',
		textStyle: {
			color: 'white',
			fontSize: '24px',
			lineHeight: 30
		}
	},
	legend: {
		bottom: '60px',
		textStyle: {
			color: 'white',
			fontSize: '16px'
		}
	},
	series: [
		{
			name: '房屋分类统计',
			type: 'pie',
			radius: [80, 160],
			avoidLabelOverlap: false,
			center: ['50%', '40%'],
			label: {
				show: false,
				position: 'center'
			},
			// 鼠标悬浮时中心位置显示对应区域的信息
			emphasis: {
				label: {
					formatter: `{b}\n{c}`,
					show: true,
					fontSize: '24px',
					fontWeight: 'bold',
					color: () => {
					},
					lineHeight: 30
				}
			},
			itemStyle: {
				borderRadius: 10,
				borderColor: 'rgba(255,255,255,0)',
				borderWidth: 2
			},
			labelLine: {
				show: false
			},
			data: [
				{value: 1427, name: 'AAA'},
				{value: 515, name: 'BBB'},
				{value: 113, name: 'CCC'},
				{value: 173, name: 'DDD'},
			]
		}
	]
});
  • 9
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bdawn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值