Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

104
Views
Cómo encontrar los datos asociados con elementos en un mapa renderizado

Tengo un mapa que estoy tratando de hacer interactivo, de modo que cuando el usuario haga clic en un distrito en particular, muestre algunos datos sobre ese distrito (% de la población en categorías x/y/z, tamaño de la población, etc.) . El conjunto de datos incluye todos esos datos, así como los geodatos. Sin embargo, después de representar el mapa, no puedo encontrar los datos del distrito en el objeto de evento. A continuación se muestra cómo está estructurado el mapa (está en un componente 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> ) }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

intentalo

 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 Report

0

También puede tener un solo detector de eventos en la etiqueta g y usar event.target para obtener acceso a los detalles de la path en la que se hizo clic. Esto se llama delegación de eventos y da como resultado un gran rendimiento.

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!