Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isnt `d` defined in d3.behavior.drag()

I am trying to make a circle draggable.

var drag = d3.behavior.drag();
  drag.on("drag", function(d,i) {
      console.log(d);
      d.x += d3.event.dx;
      d.y += d3.event.dy;
      //This will change the center coordinates by the delta
      d3.select(this).attr("x", d.x).attr("y", d.y);
      //This should change the upper left x and y values by the delta
      d3.select(this).attr("transform", function(d,i){
          return "translate(" + [ x,y ] + ")"
      })
  })

Here is the fiddle

It throws errors for every move on the right red circle, but how come it is saying that d is undefined in lines 3, 4, and 5?

like image 869
chris Frisina Avatar asked Jun 21 '13 21:06

chris Frisina


1 Answers

Working fiddle: http://jsfiddle.net/6a6da/33/

The d,i arguments would usually refer to bound data, but you're not binding any data. In your case it's sufficient to work with only the event.

drag.on("drag", function() {
  d3.select(this).attr("cx", +d3.select(this).attr("cx") + d3.event.dx);
  d3.select(this).attr("cy", +d3.select(this).attr("cy") + d3.event.dy);
})l
like image 138
Lars Kotthoff Avatar answered Nov 05 '22 18:11

Lars Kotthoff