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

269
Visualizações
d3 js drag event listener isnt getting the event - (d3, react)

I'm trying to add drag event to my d3 force layout, but for some reason, the event attr isn't defined so nothing happens. I know there were changes in the new doc of d3-drag and I followed these instructions.

heres my code:

    const node = svg
      .selectAll("circle")
      .data(nodes)
      .enter()
      .append("circle")
      .attr("r", 35) // radius field, so width and height 80X80
      .attr("stroke", "lightgray")
      .attr("stroke-width", 0.5)
      .style("fill", "lightgray")
      .call(handler
      .on('start', dragstarted)
      .on('drag', dragged)
      .on('end', dragended)

the event functions:

    const dragstarted = (event: any, d: any) => {
      console.log(' dragged d', d);
      console.log('event', event);

      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
      // @ts-ignore
      if (!event.active) {
        simulation.alphaTarget(0.3).restart();
      }
      d.fx = d.x;
      d.fy = d.y;
    }

    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore
    const dragged = (event: any, d: any) => {
      console.log(' dragged d', d);
      console.log('event', event);

      d.fx = event.x;
      d.fy = event.y;
    }

    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore
    const dragended = (event: any, d: any) => {
      console.log(' dragended  d', d);
      console.log('event', event);

      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
      // @ts-ignore
      if (event.active) {
        simulation.alphaTarget(0);
      }

      d.fx = null;
      d.fy = null;
    }

the example from the docs :

  d3.drag()
       .on("start", (event, d) => circle.filter(p => p === d).raise().attr("stroke", "black"))
       .on("drag", (event, d) => (d.x = event.x, d.y = event.y))
       .on("end", (event, d) => circle.filter(p => p === d).attr("stroke", null))

and this is what i get :

Uncaught TypeError: Cannot read properties of undefined (reading 'document')
    at nodrag.js:5

I added screenshots for the place its failing to get the event

the event that isn't there

the error

I don't get what I do wrong here, thanks for the help.

EDIT full example with the code : https://stackblitz.com/edit/react-saqrv9?file=src%2Fgraph.tsx

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

notice that the new API isn't using d3.event but the event passed to the event listener.

updated snippet for example for anyone who is interested, I know there's not many examples out there, so have fun

https://stackblitz.com/edit/react-saqrv9?file=src%2Fgraph.tsx

about 4 years ago · Santiago Gelvez 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