echarts图表tooltip内部实现事件传参效果

图片效果:tooltip内容可点击并且可实现动态传参,常见场景为散点图x与y轴值相同导致点重叠引发的问题:

newInnerText方法在全局没有,所以一定需要将newInnerText方法注入全局才行,(重要)
constructor(props) {
   super(props);
   window.newInnerText = this.newInnerText.bind(this)  //非常重要,该代码代表此方法已注入
   this.state = {
            
   }
}



newInnerText = (item) => {
    console.log(item)
}



tooltip: {
                show: true,
                trigger: 'axis',
                textStyle: {
                    color: 'rgba(0,0,0,0.65)',
                    fontSize: 12,
                    fontWeight: 'normal'
                },
                extraCssText: 'max-height:150px;min-width:150px;overflow-y:auto',
                enterable: true,  //鼠标可移入
                // position: function (point, params, dom, rect, size) {
                //     return [point[0], point[1]]
                // },
                triggerOn: 'click',
                // axisPointer: {
                //     type: 'cross',
                //     snap: true,
                //     axis: 'x', // y轴选项
                //     label: {
                //         // show: false,
                //         color: '#fff',
                //         backgroundColor: 'rgba(0, 0, 0, .55)'
                //     }
                // },
                formatter: function (param) {
                    if (!currentData) return;
                    var newText = ''
                    for (var i = 0; i < param.length; i++) {
                        if (param[i].data[0] != undefined && param[i].data[1] != undefined) {
                            if (currentData[0] === param[i].data[0] && currentData[1] === param[i].data[1]) {
                                newText += `<div style="cursor: pointer;" onclick="newInnerText('${param[i].data[3]}')">${param[i].marker} ${param[i].data[3]}<br/> 均次费用比: ${param[i].data[1]} <br/> 均次住院日比: ${param[i].data[0]}</div>`;
                            }
                        }
                    }
                    return newText
                }
            },

//以下为获取图表代码:

    var caseSurplusPic = echarts.init(document.getElementById('caseSurplusPic'));
        caseSurplusPic.off('click')  //清除点击事件,防止点击一下触发两次点击事件
        caseSurplusPic.clear()  //清除图表缓存
        caseSurplusPic.setOption(option);  //生成图表
        window.addEventListener("resize", function () {
            caseSurplusPic.resize(); //监听浏览器窗口图表自适应刷新
        });

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值