d3.xml(data).then((document) => {
...
node = node.data(nodes).enter().append("circle")
.attr("r", 20)
.attr("fill", nodeColor)
.on("mouseover", function(d) {
div.transition()
.duration(100)
.style("opacity", .9);
div.html( "<h1>Div Title</h1>"... )
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 50) + "px");
})
There are several anonymous functions written in .then(), after the d3.xml promise. This is bad practice for several reasons:
Use named functions that you can reuse. Declare these functions in the destined section of the code, and call them by their name when you need them anywhere else in your code.
d3.xml(data).then((document) => {
...
node = node.data(nodes).enter().append("circle")
.attr("r", 20)
.attr("fill", nodeColor)
.on("mouseover", showDetailsPopup)
}
// Functions declarations
function showDetailsPopup() {
div.transition()
.duration(100)
.style("opacity", .9);
div.html( "<h1>Div Title</h1>"... )
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 50) + "px");
}