如何在渲染图中找到与元素相关的数据
我有一张正在尝试进行交互式的地图,因此当用户点击特定地区时,它显示了一些有关该地区的数据(X/Y/Z类别中的人口的百分比,人口规模等) 。数据集包括所有数据以及地理数据集。但是,在渲染地图后,我找不到事件对象中区的数据。以下是映射结构化的方式(在React组件中):
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>
)
}
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>
)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试一下
try it
您还可以在
g
标记本身上拥有一个单个事件侦听器,然后使用event.target
以访问path
的详细信息。点击。这称为You can also have a single event listener on the
g
tag itself and use theevent.target
to get access to the specifics topath
that was clicked. This is called event delegation and results in great performance.