我如何计算出鼠标在 d3 中的 x 和 y 位置?

thr*_*yan 2 html javascript svg d3.js

我正在使用 JS、D3、鼠标事件和 HTML。

我试图找出鼠标悬停在 SVG 上时的 x 和 y 位置。

我的代码

let svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height)
    .on("mouseover", function(){
        let x = d3.select(this).attr("x")
        console.log(x)
    })
Run Code Online (Sandbox Code Playgroud)

我在网上查了一下,发现有人在谈论使用,d3.mouse(this)但我不断得到 d3.mouse() 不是一个函数。

有人可以帮我吗:) 谢谢

Hug*_*sen 9

在 D3 v6 中,d3.mouse变成d3.pointer

let svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height)
    .on("mouseover", e => console.log(d3.pointer(e)) )
Run Code Online (Sandbox Code Playgroud)