title: Trying to link object and text when dragging in v4
body: <p>I have been trying to learn enough about <a href="https://github.com/d3/d3-drag" rel="nofollow noreferrer">d3.drag</a> to integrate it into a sankey diagram for v4 similar to <a href="http://bl.ocks.org/xaranke/9ada4c74a87b57ae7308" rel="nofollow noreferrer">this example</a>.</p>
  
  <p>The start point was Mikes circle dragging example <a href="https://bl.ocks.org/mbostock/22994cc97fefaeede0d861e6815a847e" rel="nofollow noreferrer">here</a> which I tried to integrate with the object+text drag implementation <a href="http://bl.ocks.org/rauldiazpoblete/92f68eac8a957d316e9783ce7521b5f4" rel="nofollow noreferrer">here</a>. </p>
  
  <p>Tragically I can't quite work out how to grab the 'g' element using d3.drag.</p>
  
  <p>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').</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>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);
  }</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>.active {
    stroke: #000;
    stroke-width: 2px;
  }
  
  .rect {
    pointer-events: all;
    stroke: none;
    stroke-width: 40px;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="//d3js.org/d3.v4.min.js"&gt;&lt;/script&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>I tried to implement the code per the answer <a href="https://stackoverflow.com/questions/38596775/labels-wont-show-up-on-d3-force-diagram">here</a> which is a close analog, but the changes involved with moving from the force diagram defeated me as well.</p>
  
  <p>Any guidance appreciated. I feel like I'm missing something fundamental.</p>