共计 871 个字符,预计需要花费 3 分钟才能阅读完成。
需求:
在 Echarts 的 tooltips 中添加点击按钮并可以鼠标悬停点击该按钮
功能实现:
- 在 option 中的
tooltip
添加 enterable: true 的属性,表示鼠标可以移入 tooltip 中- 再在
formatter
中添加,此时的
onTooltipsFun
方法需要挂载到 window 上供按钮点击时使用- 除了这两处,还需要添加
position
的属性,该属性默认不设置时位置会跟随鼠标的位置
代码片段:
const onTooltipsFun = () => {
console.log('tooltips 添加按钮点击事件!');
};
window.onTooltipsFun = onTooltipsFun;
tooltip: {
enterable: 'true',
position: (point)=> ([point[0], point[1]]),
trigger: 'axis',
padding: [8, 12.5, 8, 12.5],
backgroundColor: 'rgb(7, 40, 85, 0.7);',
borderColor: '#53B4FF',
textStyle: {
color: "white"
},
axisPointer: {
label: {
show: true,
backgroundColor: '#0b1f56',
color: '#fff',
fontSize: 12,
formatter: (param) => {
return `${xAxisTickData[param.seriesData[0].dataIndex].name}`
},
},
},
formatter: (param) => {
let content = `${xAxisTickData[param[0].dataIndex].name}`;
param.forEach((item) => {
content += `${item.marker + item.seriesName} : ${item.value[1]}%`;
});
return `${content}`;
},
},
原文地址: 在 Echarts 中的 tooltip 上添加点击按钮
正文完