如何在渲染图中找到与元素相关的数据

发布于 2025-01-30 05:17:29 字数 829 浏览 3 评论 0原文

我有一张正在尝试进行交互式的地图,因此当用户点击特定地区时,它显示了一些有关该地区的数据(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幸福不弃 2025-02-06 05:17:29

尝试一下

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>
  )
}

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>
  )
}
明月夜 2025-02-06 05:17:29

您还可以在g标记本身上拥有一个单个事件侦听器,然后使用event.target以访问path的详细信息。点击。这称为

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>

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>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文