D3 / JavaScript从鼠标位置查找最近的元素

当您使用D3 / Javascript将鼠标悬停在路径元素(示例中为粉红色)上时,我试图显示最近的数据点及其文本。

JS小提琴:https://jsfiddle.net/qkcsrt7p/

我尝试搜索示例,但没有任何东西可以给我最接近鼠标位置的数据点。有人尝试过类似的功能吗?

到目前为止的代码:

       d3.selectAll("g text.label")
        .on('mouseover',function (d) {
            tooltip.transition().duration(200)
                .style('opacity',.9)
            var toolTipText = "<span>" + this.textContent + "</span>";
            tooltip.html(toolTipText)
                .style('left',(d3.event.pageX - 35) + 'px')
                .style('top',(d3.event.pageY - 30) + 'px');
        })
        .on('mouseout',0);
            tooltip.html("");
        })

        d3.selectAll("path")
        .on('mouseover',function (d) {
            var elements = document.elementsFromPoint(d3.event.pageX,d3.event.pageY);
            console.log(elements);
        })
        .on('mouseout',function (d) {

        })
wb1317 回答:D3 / JavaScript从鼠标位置查找最近的元素

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3164657.html

大家都在问