Trying to link object and text when dragging in v4

泄露秘密 提交于 2020-01-13 19:18:25

问题


I have been trying to learn enough about d3.drag to integrate it into a sankey diagram for v4 similar to this example.

The start point was Mikes circle dragging example here which I tried to integrate with the object+text drag implementation here.

Tragically I can't quite work out how to grab the 'g' element using d3.drag.

There have been several iterations over the past few days, but I think the closest I have come is the example below which will only grab a specific rectangle for dragging (I suspect I may have butchered the use of 'this').

var margin = {top: 10, right: 10, bottom: 30, left: 10},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var rectangles = d3.range(20).map(function() {
  return {
    x: Math.round(Math.random() * (width)),
    y: Math.round(Math.random() * (height))
  };
});

var color = d3.scaleOrdinal(d3.schemeCategory20);

var svg = d3.select("body").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom);

var group = svg.append("g")
  .data(rectangles)
  .attr("transform", 
     "translate(" + margin.left + "," + margin.top + ")")
      .call(d3.drag()
        .on("start", dragstarted)
        .on("drag", dragged)
        .on("end", dragended));

group.selectAll("rect")
  .data(rectangles)
  .enter().append("rect")
    .attr("x", function(d) { return d.x; })
    .attr("y", function(d) { return d.y; })
    .attr("height", 60)
    .attr("width", 30)
    .style("fill", function(d, i) { return color(i); });

group.selectAll("text")
  .data(rectangles)
  .enter().append("text")
    .attr("x", function(d) { return d.x; })
    .attr("y", function(d) { return d.y; })
      .attr("text-anchor", "start")
      .style("fill", "steelblue")
      .text("Close");

function dragstarted(d) {
  d3.select(this).raise().classed("active", true);
}

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

function dragended(d) {
  d3.select(this).classed("active", false);
}
.active {
  stroke: #000;
  stroke-width: 2px;
}

.rect {
  pointer-events: all;
  stroke: none;
  stroke-width: 40px;
}
<script src="//d3js.org/d3.v4.min.js"></script>

I tried to implement the code per the answer here which is a close analog, but the changes involved with moving from the force diagram defeated me as well.

Any guidance appreciated. I feel like I'm missing something fundamental.


回答1:


I tweaked your code a little. I grouped both rectangles and text and added the drag attribute to the group. So, when we drag, both text and rectangle move in sink.

	var margin = {top: 10, right: 10, bottom: 30, left: 10},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var rectangles = d3.range(10).map(function() {
  return {
    x: Math.round(Math.random() * (width)),
    y: Math.round(Math.random() * (height))
  };
});

var color = d3.scaleOrdinal(d3.schemeCategory20);

var svg = d3.select("body").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom);

var group = svg.selectAll('g')
	.data(rectangles).enter().append("g").attr("transform", 
     "translate(" + margin.left + "," + margin.top + ")")
      .call(d3.drag()
        .on("start", dragstarted)
        .on("drag", dragged)
        .on("end", dragended));

group.append("rect")
    .attr("x", function(d) { return d.x; })
    .attr("y", function(d) { return d.y; })
    .attr("height", 60)
    .attr("width", 30)
    .style("fill", function(d, i) { return color(i); });
	
group.append("text")
    .attr("x", function(d) { return d.x; })
    .attr("y", function(d) { return d.y; })
      .attr("text-anchor", "start")
      .style("fill", "steelblue")
      .text("Close");	
		
function dragstarted(d) {
  d3.select(this).raise().classed("active", true);
}

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

function dragended(d) {
  d3.select(this).classed("active", false);
}
.active {
  stroke: #000;
  stroke-width: 2px;
}

.rect {
  pointer-events: all;
  stroke: none;
  stroke-width: 40px;
}
<script src="//d3js.org/d3.v4.min.js"></script>

Hope this helps.




回答2:


You can also try this alternative way of grouping and moving groups.

var margin = {top: 10, right: 10, bottom: 30, left: 10},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var rectangles = d3.range(10).map(function() {
    return {
        x: Math.round(Math.random() * (width)),
        y: Math.round(Math.random() * (height))
    };
});

var color = d3.scaleOrdinal(d3.schemeCategory20);

var svg = d3.select("body").append("svg")
            .attr("width", width + margin.left + margin.right)
            .attr("height", height + margin.top + margin.bottom);

var group = svg.selectAll('g')
               .data(rectangles)
               .enter().append("g")
               .attr("transform",function(d) {
                      return "translate(" + (margin.left + d.x) + "," + (margin.top + d.y) + ")"
                })
               .call(d3.drag()
                       .on("start", dragstarted)
                       .on("drag", dragged)
                       .on("end", dragended));

group.append("rect")
     .attr("height", 60)
     .attr("width", 30)
     .style("fill", function(d, i) { return color(i); });

group.append("text")
     .attr("text-anchor", "start")
     .style("fill", "steelblue")
     .text("Close");

function dragstarted(d) {
    d._drag = {
        distance:0,
        threshold:2,
        initiated: false
    };
}

function dragged(d) {
    if (!d._drag.initiated) {
        d._drag.initiated = true;
        d3.select(this).raise().classed("active", true);
    }
    d._drag.distance += d3.event.dx * d3.event.dx + d3.event.dy * d3.event.dy;
    d.x = d3.event.x;
    d.y = d3.event.y;
    d3.select(this).attr('transform', 'translate('+[d.x,d.y]+')');
}

function dragended(d) {
    if (d._drag.distance < d._drag.threshold) {
        d3.select(window).on('click.drag', null);
        return;
    }
    d3.select(this).classed("active", false);
}
.active {
  stroke: #000;
  stroke-width: 2px;
}

.rect {
  pointer-events: all;
  stroke: none;
  stroke-width: 40px;
}
<script src="//d3js.org/d3.v4.min.js"></script>


来源:https://stackoverflow.com/questions/38928869/trying-to-link-object-and-text-when-dragging-in-v4

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!