Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

110
Visualizações
How to find the data associated with elements in a rendered map

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>
  )
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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>
  )
}
about 4 years ago · Juan Pablo Isaza Relatório

0

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>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda