Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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>
  )
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading