Javascript D3.具有嵌套数据的拖动行为

Javascript D3.具有嵌套数据的拖动行为,javascript,d3.js,Javascript,D3.js,我以前遇到过这个问题&这是由于我的d3.select与我的attr不匹配。但是,我没有从当前的错误中收到任何错误,我对自己做错的事情感到有些失落: 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("+n

我以前遇到过这个问题&这是由于我的d3.select与我的attr不匹配。但是,我没有从当前的错误中收到任何错误,我对自己做错的事情感到有些失落:

 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();
   });

谢谢。

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

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

看起来你并没有把拖拉行为称为任何地方。你想做什么?嗨@Lars Kotthoff我以为我在调用我的var节点上的drag-.call(drag)。我只是想在页面上拖动我的对象