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

268
Views
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 answers
Answer question

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 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!