简体   繁体   中英

Why isn't D3 transition working

I'm new to D3 and having trouble smoothly transitioning between two data sets. Here's a super simplified version of what I have currently:

var h = 500;
var w = 500;

var data = [
  10,
  15,
  12,
  9
]

var data_two = [
  6,
  15,
  20,
  19,
  11
]

var vis = d3.select('body')
  .append('svg')
  .attr({
    height: 500,
    width: 500
  })
  .append('g');

function buildChart(data) {
  var square = vis.selectAll('rect')
      .data(data, function(d) { return d }) 

  square
    .transition()
    .duration(1000)
    .attr({
      x : function(d, i) { return 0 }  
    })

  square
    .enter()
    .append('rect')
    .attr({
      height : function(d, i) { return 10 },
      y      : function(d, i) { return i * 15 },
      width  : function(d, i) { return d * 5 },
      x      : function(d, i) { return 0 }
    });

  square
    .exit()
    .remove();
 } 

buildChart(data);

setTimeout(function() {
  buildChart(data_two)
}, 1000)

jsfiddle: http://jsfiddle.net/ncakby1a/

My desired results are to have the bars smoothly animate to the new widths after a one second delay. Instead, they just jump to the new state and I'm not sure why. Thanks in advance!

You're buildChart function is always placing new bars, it's never updating the bars you placed with data according to the data in data_two . To fix this, first append the rectangles using data , then select them & apply the transition:

var h = 500;
var w = 500;

var data = [
  10,
  15,
  12,
  9,
  3
]

var data_two = [
  6,
  15,
  20,
  19,
  11
]

var vis = d3.select('body')
  .append('svg')
  .attr({
    height: 500,
    width: 500
  })
  .append('g');

vis.selectAll('rect')
    .data(data)
    .enter()
    .append('rect')
    .attr({
        x: function(d, i){ return 0;},
        y: function(d, i){ return i * 15;},
        height: function(d){ return 10;},
        width: function(d){ return d * 5;}
    })

vis.selectAll('rect')
  .data(data_two)
  .transition()
  .duration(1000)
  .delay(1000)
  .attr({
     x: function(d, i){ return 0;},
     y: function(d, i){ return i * 15;},
     height: function(d){ return 10;},
     width: function(d){ return d * 5;}
  });

Here's a working JSfiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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