I refer this chart code reference to draw this chart.
In this code I wrote click event like svg.append("g").on("click", function(d){console.log(d.id)}. But it is not working.
Click to see the image
function ready(error, guns, us, emp) {
emp.forEach(function (d) {
unemployment.set(d.id, d.rate);
});
if (error) throw error;
// Draw the map
svg.append("g")
.attr("class", "counties")
.selectAll("path")
.data(topojson.feature(us, us.objects.counties).features)
.enter().append("path")
.attr("fill", function (d) { return color(d.rate = unemployment.get(d.id)); })
.attr("d", path)
.style("stroke", "lightgrey")
.append("title")
.text(function (d) {
return `${d.properties.name}: ${d.rate}%`;
})
.on('click', function (d) {
console.log(d.id);
})
The reason you're having issues is because your event listener is linked to the title rather than the county paths themselves.
Here's an observable block that should help; I've annotated the changes to your code below and clarified the naming differences by creating a
$countyPathsvariable:Reading through this link may help explain some of the finer points of selections for d3, but basically, whatever the last appended item is, that's the one that you're going to be referencing in your event listener. Since, in your case, you appended
titleelements, you're adding an event listener to those. Breaking out code the way that I did in the example below allows you to refer to the joined elements themselves, which are, in your case, thepathelements.