I have a map that I'm trying to make interactive, so that when the user clicks on a particular district, it shows some data about that district(% of the population in x/y/z categories, population size, etc.). The dataset includes all of that data, as well as the geodata. However, after the map is rendered, I can't find the data for the district in the event object. Below is how the map is structured (it's in a React component):
function Map({ data, selectedDistricts }) {
.
.
.
const districtPathGenerator = d3.geoPath(projection)
function handleClick(e) {
console.log(e) // <-- how to display the data associated with the clicked-on district?
}
return (
<div ref={ref}>
<svg width="500px" height="450px">
<g>
{data.map((district) => {
return (
<path
d={districtPathGenerator(district)}
onClick={(e) => handleClick(e)}
>
</path>
)
})}
</g>
</svg>
</div>
)
}
CodePudding user response:
try it
function Map({ data, selectedDistricts }) {
.
.
.
const districtPathGenerator = d3.geoPath(projection)
function handleClick(e, district) {
console.log(e, district) // <-- how to display the data associated with the clicked-on district?
}
return (
<div ref={ref}>
<svg width="500px" height="450px">
<g>
{data.map((district) => {
return (
<path
d={districtPathGenerator(district)}
onClick={(e) => handleClick(e, district)}
>
</path>
)
})}
</g>
</svg>
</div>
)
}
CodePudding user response:
You can also have a single event listener on the g
tag itself and use the event.target
to get access to the specifics to path
that was clicked. This is called event delegation and results in great performance.
const g = document.querySelector('svg g')
g.addEventListener("click", event => {
console.log(event.target.getAttribute("key"))
})
const data = {
a: "M50 0 L10 10",
b: "M100 10 L20 20",
c: "M200 20 L30 30",
}
g.innerHTML = Object.keys(data).map(key => `
<path
key="${key}"
stroke="black"
stroke-width="5"
d="${data[key]}"
>
</path>
`).join("")
<svg viewBox="0 0 200 200">
<g></g>
</svg>