【eCharts】echarts饼状图数据重叠导致指示线及显示文字重叠解决

转载自:https://www.cnblogs.com/minjh/p/9081893.html   先看他的,再看我的,不看我的也行

1. 解决前

 

2.解决后

3.代码

function makePie(id, title, name, data, show) {
    if (!show) {
        show = false;
    }
    var myChart = echarts.init(document.getElementById(id));
    var option = {
        title: {
			show:false,
            text: title,
            top: 2,
            left: '20',
			textStyle: {fontSize: 14,color:'#767676',fontWeight:600,fontFamily:'Microsoft Yahei'},
			
        },
        tooltip: {
            trigger: 'item',
			textStyle: {fontSize: 12,fontFamily:'Microsoft Yahei'},
            formatter: function (params) {
                return params.data.name + ":<br/>" + fmoneyf(params.data.value, 0) + "(" + params.percent + "%)";
            }
        },
        legend: {
            // type: 'scroll',
			orient:'vertical',
			top:80,
			y:'middle',
			height:'60%',
            right: getValueFromJson(data, 'name').length > 6 ? 0 : 5,
		    textStyle: {fontSize: 12,color:'#848484'},
			icon:'circle',
            data: getValueFromJson(data, 'name')
        },
        series: [
            {
                name: name,
                x: 'left',  
                y: 'center', 
                type: 'pie',
				center:['50%', '60%'],
                //radius: getValueFromJson(data, 'name').length > 6 ?  ['40%', '84px'] :  ['46%','90px'],
				 radius: '55%',
                avoidLabelOverlap: true,
                minAngle: 5,
                label: {
                	align: 'left',
                	normal: {
                        show: show,
						lineHeight:30,
						textStyle: {fontFamily:'Microsoft Yahei'},
						formatter(v) {
                            let text = Math.round(v.percent)+'%' + '' + v.name
                            if(text.length <= 8)
                            {
                                return text;
                            }else if(text.length > 8 && text.length <= 16){
                                return text = `${text.slice(0,8)}\n${text.slice(8)}`
                            }else if(text.length > 16 && text.length <= 24){
                                return text = `${text.slice(0,8)}\n${text.slice(8,16)}\n${text.slice(16)}`
                            }else if(text.length > 24 && text.length <= 30){
                                return text = `${text.slice(0,8)}\n${text.slice(8,16)}\n${text.slice(16,24)}\n${text.slice(24)}`
                            }else if(text.length > 30){
                                return text = `${text.slice(0,8)}\n${text.slice(8,16)}\n${text.slice(16,24)}\n${text.slice(24,30)}\n${text.slice(30)}`
                            }
                        },
						rich: {  
							color1: {  
								color: '#000000',
								fontSize: 14,
								lineHeight:12,
								fontWeight:600
							},  
							color2: {  
								color: '#848484'  ,
								fontSize: 12,
							}  
						},  
                    }, emphasis: {
                        show: !show,
						textStyle: {fontSize: 12,color:'#000000',fontFamily:'Microsoft Yahei'},
                        formatter: function (params) {
                            return params.data.name + ": " + fmoneyf(params.data.value, 0) + "(" + params.percent + "%)";
                        }
                    }
                },
				
				labelLine:{
					length:10,
					length2:100,
					lineStyle:{
						color:'#848484',
						
					}
				},
                data: data,
                color:getValueFromJson(data, 'color'),
            }
        ]
    };
    myChart.setOption(option);
}

4.主要代码段:

series: [
            {
                name: name,
                x: 'left',  
                y: 'center', 
                type: 'pie',
				center:['50%', '60%'],
                //radius: getValueFromJson(data, 'name').length > 6 ?  ['40%', '84px'] :  ['46%','90px'],
				 radius: '55%',
                avoidLabelOverlap: true,
                minAngle: 5,
                label: {
                	align: 'left',
                	normal: {
                        show: show,
						lineHeight:30,
						textStyle: {fontFamily:'Microsoft Yahei'},
						formatter(v) {//此方法为核心代码,解决问题
                            let text = Math.round(v.percent)+'%' + '' + v.name
                            if(text.length <= 8)
                            {
                                return text;
                            }else if(text.length > 8 && text.length <= 16){
                                return text = `${text.slice(0,8)}\n${text.slice(8)}`
                            }else if(text.length > 16 && text.length <= 24){
                                return text = `${text.slice(0,8)}\n${text.slice(8,16)}\n${text.slice(16)}`
                            }else if(text.length > 24 && text.length <= 30){
                                return text = `${text.slice(0,8)}\n${text.slice(8,16)}\n${text.slice(16,24)}\n${text.slice(24)}`
                            }else if(text.length > 30){
                                return text = `${text.slice(0,8)}\n${text.slice(8,16)}\n${text.slice(16,24)}\n${text.slice(24,30)}\n${text.slice(30)}`
                            }
                        },
						rich: {  
							color1: {  
								color: '#000000',
								fontSize: 14,
								lineHeight:12,
								fontWeight:600
							},  
							color2: {  
								color: '#848484'  ,
								fontSize: 12,
							}  
						},  
                    }, emphasis: {
                        show: !show,
						textStyle: {fontSize: 12,color:'#000000',fontFamily:'Microsoft Yahei'},
                        formatter: function (params) {
                            return params.data.name + ": " + fmoneyf(params.data.value, 0) + "(" + params.percent + "%)";
                        }
                    }
                },

ps:转载的

### 取消ECharts Pie图表的默认样式或行为 为了取消 ECharts Pie 图表的一些默认样式或行为,可以通过配置项中的多个属性来实现。以下是几种常见的修改方式: #### 隐藏图例 如果不需要显示图例,则可以在 `legend` 属性中设置 `{ show: false }` 来隐藏它。 ```javascript option = { legend: { show: false }, // 不展示图例 }; ``` #### 移除数据项之间的间隙 对于环形饼图,默认情况下会有一些内径大小造成视觉上的间隔效果;而对于普通的饼状图来说,可能希望移除扇区间的缝隙。这可以借助于调整 `itemStyle.normal.borderRadius` 或者直接通过设定 `radius` 参数达到目的[^2]。 ```javascript series: [{ type: 'pie', radius: ['50%', '70%'], // 设置内外半径比例从而形成圆环而非实心圆形 }] ``` #### 关闭标签自动旋转功能 有时当切片角度较小时,文字可能会被压缩得难以辨认甚至重叠在一起。此时关闭此特性有助于改善可读性。 ```javascript labelLine: { normal: { smooth: true, length: 10, // 连接线长度 length2: 20, // 延伸线长度 lineStyle: { width: 1, color: '#aaa' } } }, label: { normal: { rotate: false, // 是否启用标签旋转适应 formatter: '{b}: {@value} ({d}%)', // 自定义提示信息格式化器 position: 'outside'// 标签位置 }, } ``` #### 禁用鼠标悬停高亮交互 通常情况下点击某个部分后会有相应的动画反馈以及数值变化指示,但也可以禁掉这些互动特效以便更专注于静态呈现的数据本身。 ```javascript emphasis: { itemStyle: { shadowBlur: 0, // 清楚阴影模糊度 borderColor: null, // 边缘颜色置为空即不绘制边框 borderWidth: 0 // 边界宽度设为零同样起到相同作用 } } ``` 以上就是一些关于如何取消 ECharts Pie 图表默认样式的具体方法[^3]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值