简体   繁体   English

如何使用d3.js拖动行为拖动svg组?

[英]How to drag an svg group using d3.js drag behavior?

I am using D3js drag . 我正在使用D3js拖动 single element is get dragged perfectly fine. 单个元素被拖得很好。 but i want to drag a group of elements.How it can be done. 但我想拖动一组元素。如何做到这一点。 Here is what is on my Js Fiddle link : 这是我的Js Fiddle链接

  function onDragDrop(dragHandler, dropHandler) {
        var drag = d3.behavior.drag();

    drag.on("drag", dragHandler)
    .on("dragend", dropHandler);
    return drag;
    }

    var g = d3.select("body").select("svg").append("g")
    .data([{ x: 50, y: 50 }]);

    g.append("rect")
    .attr("width", 40)
    .attr("height", 40)
    .attr("stroke", "red")
    .attr("fill","transparent")
    .attr("x", function (d) { return d.x; })
    .attr("y", function (d) { return d.y; })
    .call(onDragDrop(dragmove, dropHandler));

    g.append("text")
    .text("Any Text")
    .attr("x", function (d) { return d.x; })
    .attr("y", function (d) { return d.y; })
    .call(onDragDrop(dragmove, dropHandler));

    function dropHandler(d) {
       // alert('dropped');
    }

    function dragmove(d) {
        d3.select(this)
      .attr("x", d.x = d3.event.x)
      .attr("y", d.y = d3.event.y);
    }

I want to drag both rect and text simultaneously. 我想同时拖动rect和text。 Here is what I tried , but no luck. 这是我尝试过的 ,但没有运气。 I think am missing something simple. 我想我错过了一些简单的事情。

First off, the <g> element doesn't care about x and y attributes (as in: they're just ignored). 首先,<g>元素不关心xy属性(如:它们只是被忽略)。 You can use transform="translate(x,y)" instead. 您可以使用transform="translate(x,y)"代替。

Second, you will need to check that the element you get in the dragmove handler actually is the <g> element, and not a child element of it. 其次,您需要检查您在dragmove处理程序中获得的元素是否实际上是<g>元素,而不是它的子元素。 This is because <g> elements have no actual hit area themselves. 这是因为<g>元素本身没有实际的命中区域。 Their children do though, and the mouse events first go to the children then bubble up to the parent. 他们的孩子会这样做,鼠标事件首先会传给孩子,然后冒泡到父母那里。 You can inspect evt.target and evt.currentTarget to see this in action. 您可以检查evt.targetevt.currentTarget以查看此操作。 target is the element that was hit originally, currentTarget is the event target that is currently handling the event (eg the <g> element if the event bubbled up). target是最初命中的元素, currentTarget是当前正在处理事件的事件目标(例如,如果事件冒泡,则为<g>元素)。

For d3 v4: 对于d3 v4:

var drag_this = d3.drag().subject(this)
    .on('start',function (d) {
        if (d.x1){
            d.x1 =  d3.event.x - d.xt;
            d.y1 =  d3.event.y - d.yt;
        }else{
            d.x1 = d3.event.x;
            d.y1 = d3.event.y;
        }
    })
    .on('drag',function(d){
        d3.select(this)
        .attr("transform", "translate(" + (d3.event.x - d.x1)  + "," + (d3.event.y - d.y1) + ")");

        d.xt = d3.event.x - d.x1;
        d.yt = d3.event.y - d.y1;
    });

my_group.call(drag_this);

This assumes that you have data bound to the group. 这假设您有绑定到组的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM