Y轴的标签名文字太长怎么设置超出省略,并且鼠标移上去后显示全名称?

在工作中遇到此问题,我这金鱼的记忆实在是不记录不行呀,所以特此记录下
1.超出省略:在yAxis中设置axisLabel为:

axisLabel: {
				show: true,
				formatter: function(value) {
					if(value.length > 5) {
						return `${value.slice(0,4)}...`;
					}
					return value;
				}
			},

2.显示全名:
经过查看官方配置知道有个属性triggerEvent,它是指坐标轴的标签是否响应和触发鼠标事件,默认是不响应的。
现在思路就是书写一个触发鼠标的事件,在触发Y轴标签的时候。在此事件中,我们可以创建一个div盒子来装完整的标签内容。
步骤:
首先给yAxis加上triggerEvent: true;
其次在option外面定义方法:

	function tips(terminalServices) {              
        //判断是否创建过div框,如果创建过将不再创建
		var already = document.getElementById("extension");
		if(!already) {
			var div = "<div id = 'extension' sytle=\"display:none\"></div>"
			$('html').append(div);
		}
		terminalServices.on('mouseover', function(params) {
			if(params.componentType == "yAxis") {
				$('#extension').css({
					"position": "absolute",
					"color": "#fff",
					"font-size": "12px",
					"padding": "10px",
					"display": "inline",
					"z-index":'3000',
					"background-color":'rgba(47,79,79, 0.8)',
					"border-radius":'5px',
				}).text(params.value);
				$("html").mousemove(function(event) {
					var x = event.pageX - 30;
					var y = event.pageY + 20;
					$('#extension').css('top', y).css('left', x);
				});
			}
		});
		terminalServices.on('mouseout', function(params) {
			if(params.componentType == "yAxis") {
				$('#extension').css('display', 'none');
			}
		});

	};

最后调用方法:
tips(terminalServices);
源码展示:

var terminalServices = echarts.init(document.getElementById('terminalServices'));
	var option = {
		tooltip: {
			trigger: 'axis',
			axisPointer: {
				type: 'none'
			}
		},
		grid: {
			top: '2%',
			right: '12%',
			left: '20%',
			bottom: '1%'
		},
		xAxis: [{
			type: 'value',
			show: false,
		}],
		yAxis: [{
			type: 'category',
			triggerEvent: true,
			data: [],
			inverse: true,
			axisTick: {
				show: false,
			},
			axisLabel: {
				show: true,
				formatter: function(value) {
					if(value.length > 5) {
						return `${value.slice(0,4)}...`;
					}
					return value;
				}
			},
			axisLine: {
				show: false,
				lineStyle: {
					color: '#cecece',
					fontSize: 5
				}
			},
			splitLine: {
				show: false
			}
		}, {
			type: 'category',
			inverse: true,
			axisTick: 'none',
			axisLine: 'none',
			show: true,
			axisLabel: {
				textStyle: {
					color: '#ffffff',
					fontSize: '12'
				},
				formatter: function(param) {
					return(param).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
				}
			},
			data: []
		}],
		series: [{
			type: 'bar',
			barWidth: '6px',
			barCategoryGap: '10',
			data: [],
			itemStyle: {
				normal: {
					color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [{
						offset: 0,
						color: 'rgba(57,93,252,1)'
					}, {
						offset: 1,
						color: 'rgba(47,198,209)'
					}], false),
					barBorderRadius: [50, 50, 50, 50],
					shadowColor: 'rgba(0,160,221,1)',
					shadowBlur: 2,
				}
			}
		}]
	};
	tips(terminalServices);
	function tips(terminalServices) {              
        //判断是否创建过div框,如果创建过将不再创建
		var already = document.getElementById("extension");
		if(!already) {
			var div = "<div id = 'extension' sytle=\"display:none\"></div>"
			$('html').append(div);
		}
		terminalServices.on('mouseover', function(params) {
			if(params.componentType == "yAxis") {
				$('#extension').css({
					"position": "absolute",
					"color": "#fff",
					"font-size": "12px",
					"padding": "10px",
					"display": "inline",
					"z-index":'3000',
					"background-color":'rgba(47,79,79, 0.8)',
					"border-radius":'5px',
				}).text(params.value);
				$("html").mousemove(function(event) {
					var x = event.pageX - 30;
					var y = event.pageY + 20;
					$('#extension').css('top', y).css('left', x);
				});
			}
		});
		terminalServices.on('mouseout', function(params) {
			if(params.componentType == "yAxis") {
				$('#extension').css('display', 'none');
			}
		});

	};
			var data = {
				name: [‘文字过长的内容’,‘文字过长的内容’,‘文字过长的内容’,‘文字过长的内容’,],
				tnum: [‘1512’,‘1512’,‘1512’,‘1512’,],
			}
			option.yAxis[0].data = data.name;
			option.series[0].data = data.tnum;
			option.yAxis[1].data = data.tnum;
			terminalServices.setOption(option);
		
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值