在带有线条和符号的标准简单折线图中,设置工具提示选项意味着每当您将鼠标悬停在符号上时,它都会显示一个包含线条名称和符号值的框。
我真正想要的是 当我将鼠标悬停在线条本身上时显示此工具提示框(而不仅仅是当我将鼠标悬停在符号上时)。
此外,我希望此工具提示成为自定义工具提示,仅包含行名称,禁用符号。
triggerLineEvent
设置为 true
:
series: [
{
... // your line serie config
triggerLineEvent: true,
},
然后就可以使用Echart的鼠标事件来管理鼠标悬停线条了:
var tooltipDisplay = ""
// Called when your mouse hover an object (params : the object you hover)
myChart.on('mouseover', function(params) {
// Check if it's hovering a line
if(params.componentSubType == "line"){
// get hovered line series name
tooltipDisplay = params.seriesName
}
});
//Called when your mouse leaves an object (params : the object you leave)
myChart.on('mouseout', function(params) {
tooltipDisplay = ''
});
然后,将其显示在工具提示中:
tooltip: [
{
... // your tooltip config
formatter : (params) => {
return tooltipDisplay;
}
},
var myChart = echarts.init(document.getElementById('main'));
var tooltipDisplay = ""
option = {
grid: {
top: '10px',
bottom: '40px',
left: '100px',
right: '100px',
},
tooltip: {
trigger: 'axis',
formatter : (params) => {
return tooltipDisplay;
}
},
xAxis: {
type: 'category',
boundaryGap: false,
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [
{
name: 'Email',
type: 'line',
data: [120, 132, 101, 134, 90, 230, 210],
showSymbol: false,
triggerLineEvent: true,
},
{
name: 'Search Engine',
type: 'line',
data: [820, 932, 901, 934, 1290, 1330, 1320],
showSymbol: false,
triggerLineEvent: true
}
]
};
myChart.on('mouseover', function(params) {
if(params.componentSubType == "line"){
tooltipDisplay = params.seriesName
}
});
myChart.on('mouseout', function(params) {
tooltipDisplay = ''
});
myChart .setOption(option)
<html>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/echarts/5.3.2/echarts.min.js"></script>
<div id="main" style="width: 450px; height:170px;"></div>
</body>
</html>
只需添加以下工具提示配置 那么它应该可以工作
tooltip: {
type: 'item'
}
options: EChartsOption = {
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
},
yAxis: {
type: 'value',
},
series: [
{
data: [820, 932, 901, 934, 1290, 1330, 1320],
type: 'line',
},
],
tooltip: {
type: 'item'
}
};