svg.selectAll("rect")
.data(data)
.enter()
.append("rect")
.attr("x", (d, i) => xScale(i))
.attr("y", d => yScale(d))
.attr("width", xScale.bandwidth())
.attr("height", d => height - yScale(d))
.attr("fill", "steelblue")
.on("mouseover", function () {
d3.select(this).attr("fill", "orange");
})
.on("mouseout", function () {
d3.select(this).attr("fill", "steelblue");
});
Now, when a user hovers over a bar, it changes color to orange, and reverts back when the mouse is moved away.