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

131
Visualizações
D3.JS Click Event problem, using a svg map

Hi I'm fairly new to the D3 library, and I cannot figure out how to get a click event to work when I'm clicking on the country, I understand the 'd' variable contains the country information but how do I get it to console log on click. I've tried a few methods but cant figure it out. any help appreciated

   const svg = d3.select("svg"),
           width = +svg.attr("width"),
           height = +svg.attr("height");

   // Map and projection
   const projection = d3.geoNaturalEarth1()
           .scale(width / 1.5 ) // Lower the num closer the zoom
           .translate([200, 700])  // (Horizontal, Vertical)

   // Load external data and boot
   d3.json("https://raw.githubusercontent.com/holtzy/D3-graph-gallery/master/DATA/world.geojson").then( function(data) {

     // Draw the map
     svg.append("g")
             .selectAll("path")
             .data(data.features)
             .join("path")
             .attr("fill", "#348C31") // Color Of Country
             .attr("d", d3.geoPath().projection(projection))
             .style("stroke", "white")// Border Lines
             .append("title")
                .text(d => console.log(d))
       //Want to run on click of country,
   })

   const $countyPaths = svg.append("g")
   .selectAll("path")
   .data(data.features)
   .join("path")

   $countyPaths
       .attr("fill", d => color(data.get(d.id)))
       .attr("d", path)
       .append("title")
   
   $countyPaths.on('click',d=>console.log(d.id))

   function buttonClick() {
       window.alert("Boom");
   }



 </script>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

d3 provides the .on(event, fx(event, d)) method in order to handle events. To log the country, you could do then the following:

svg.append("g")
         .selectAll("path")
         .data(data.features)
         .join("path")
         .attr("fill", "#348C31") // Color Of Country
         .attr("d", d3.geoPath().projection(projection))
         .style("stroke", "white")// Border Lines
         .on("click", (_, d) => console.log(d))

Note that the first thing that the function from the event listener will receive is the event info, and the second one the data

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