Stan Sokolov
Stan Sokolov

Reputation: 2270

d3.mouse is not a function

Upgraded d3 library and now getting an error for executing the following code

console.log(`\n${JSON.stringify(d3.mouse)}\n`);
const mouse = d3.mouse(this);

Getting

undefined

d3.mouse is not a function

And it is not defined anywhere. I went trough d3 exports and this function is not really exported from anywhere.

Upvotes: 20

Views: 16004

Answers (2)

Achraf Farouky
Achraf Farouky

Reputation: 1280

d3.mouse was removed in d3v6, you should use d3.pointer(event).

Upvotes: 38

mahyar sadeghi
mahyar sadeghi

Reputation: 87

An example of using event is represented below:

let mousemove = function (event, d) {
        tooltip
            .html("Year: " + d.Year + " Sales: " + d.value + " M$")
            .style("left", (event.pageX + 30) + "px") 
            .style("top", (event.pageY) + "px")
    }

If each data point has the values of year and value, you can demonstrate them using the code above. Event has two attributes: event.pageX and evev.pageY. Try to sum the returned values, for example event.pageX +30 , so they are not going to be in the exact position that the mouse is on.

Upvotes: 1

Related Questions