在工作中遇到此问题,我这金鱼的记忆实在是不记录不行呀,所以特此记录下
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);