Search code examples
angularjsd3.jsdrag-and-dropangularjs-ng-modelng-bind

D3js / AngularJS - drag and data bind the rect's coordinates


I want to bind the coordinate x ,y of rect.(Recorded as "Coordinate "). And drag the rect, hoping that the record (coordinate x ,y) will synchronous change.

Below are part of the code.(Complete code jsbin)

html

<tr ng-repeat="item in data">
    <td>({{ item.x }}, {{ item.y }})</td>
    <td>{{ item.width }}</td>
    <td>{{ item.height }}</td>
</tr>

JS

$scope.data = [{ 'x': 30, 'y': 50, 'width': 90, 'height': 70 }];

var drag = d3.drag()        
    .on('drag', function (d) {
        d3.select(this)
            .attr('x', d.x = d3.event.x)
            .attr('y', d.y = d3.event.y)
    })
    .on('end', function (d) {
        //update  coordinate x ,y to array
        arrayNum = this.id;   
        $scope.data.splice(arrayNum, 1, { 'x': d.x, 'y': d.y, 'width': d.width, 'height': d.height });
        console.log($scope.data);
    });

I also have $scope.data.splice to update the array. And it really update the $scope.data. But it doesn't work on the browser view. How can I modify? Or what can I refer to? Thank's a lot!


Solution

  • It seems like angular isn't aware that its scope is being updated by a d3 event.

    I added $scope.$apply() to your on end handler and the display updates in the view as intended after each drag event completes.

    var mainApp = angular.module("mainApp", []);
    
    mainApp.controller('Controller', function($scope) {
      $scope.data = [{
        'x': 30,
        'y': 50,
        'width': 90,
        'height': 70
      }];
    
      var drag = d3.drag()
        .on('drag', function(d) {
          d3.select(this)
            .attr('x', d.x = d3.event.x)
            .attr('y', d.y = d3.event.y)
    
        })
        .on('end', function(d) {
          arrayNum = this.id;
    
          // Ensure angular knows about the update to its scope
          $scope.$apply(function() {
            //update  coordinate x ,y to array
            $scope.data.splice(arrayNum, 1, {
              'x': d.x,
              'y': d.y,
              'width': d.width,
              'height': d.height
            });
          });
          console.log($scope.data);
        });
    
      //create SVG
      var svg = d3.select('.Content')
        .append('svg')
        .attr('width', 300)
        .attr('height', 300)
        .style('border', '1px solid #000');
    
      var container = svg.append('g');
    
      container.append('svg:image')
        .attr('xlink:href', 'dog.jpg')
        .attr('x', 0)
        .attr('y', 0);
    
      container.data($scope.data)
        .append('rect')
        .attr('x', function(d) {
          return d.x;
        })
        .attr('y', function(d) {
          return d.y;
        })
        .attr('width', function(d) {
          return d.width;
        })
        .attr('height', function(d) {
          return d.height;
        })
        .attr('id', function(d, i) {
          return i;
        })
        .style('cursor', 'pointer')
        .call(drag);
    });

    Also, take a read of this answer to better understand why you might need to do this when working with d3.