D3.Drag行为与嵌套数据

问题描述:

我有这个问题之前&这是由于我的d3.select不符合我的attr。不过,我不从这个当前的bug收到任何错误,我也多少有些失落在我做了什么错条款:D3.Drag行为与嵌套数据

var drag = d3.behavior.drag() 
    .origin(Object) 
    .on("drag", function(d, i) { 
    d.x = d3.event.x; 
    d.y = d3.event.y; 
    d3.select(this).attr("transform", "translate("+nodes[i].locations[0].x+","+nodes[i].locations[0].y+")"); 

}); 

正如你看到的,我要通过我的索引来查找所有的位置。这会影响拖动功能吗?

var node = svg.selectAll("g.node") 
      .data(nodes) 

    node.enter().append("g") 
     .call(drag) 
     .attr('class', 'node'); 

node.attr("transform", function(d,i) { return"translate("+nodes[i].locations[0].x+","+nodes[i].locations[0].y+")"; }) 

我很困惑,为什么这不会工作

我也试图改变我的ondrag当功能,但是当我试图拖动它只能移动到中心

.on("drag", function(d, i) { 
     nodes[i].locations[0].x = d3.event.x; 
     nodes[i].locations[0].y = d3.event.y; 
     d3.select(this).attr("transform", "translate ("translate("+nodes[i].locations[0].x+","+nodes[i].locations[0].y+")"); 
    // draw(); 
    }); 

感谢节点。

+0

好像你没有在任何地方调用拖动行为。你想做什么? – 2013-04-29 10:30:26

+0

Hi @Lars Kotthoff我以为我在调用var节点上的拖动 - .call(拖动)。我只是试图拖动我的对象在页面周围 – Jose 2013-04-29 14:56:56

我相信我已经找到了解决我的问题:

.on("drag", function(d, i) { 
     nodes[i].locations[0].x += d3.event.dx; 
     nodes[i].locations[0].y += d3.event.dy;