javascript - d3 updates new elements transition in -
i have barchart updating fine this:
var bar=chart.selectall(".bar") .data(data); bar.enter().append("rect") .attr("class", "bar") .attr("x", function(d) { if(scale(d.values.total)<0){return width/2+widthdatelabel;}else{return width/2-scale(d.values.total)-widthdatelabel;}}) .attr("width", function(d) { return math.abs(scale(d.values.total)); }) .attr("height", barheight - 1) .attr("fill", function(d) { if(scale(d.values.total)<0){ return "deeppink"}else{return "mediumseagreen"}}) .attr("transform", function(d, i) { return "translate(0," + * barheight + ")"; }); bar.exit() .transition().duration(750) .attr("transform", function(d, i) { return "translate(0," + (i + 1) * barheight + ")"; }) .style("opacity", 0) .remove(); bar .transition().duration(750) .attr("height", barheight - 1) .attr("x", function(d) { if(scale(d.values.total)<0){return width/2+widthdatelabel;}else{return width/2-scale(d.values.total)-widthdatelabel;}}) .attr("width", function(d) { return math.abs(scale(d.values.total)); }) .attr("fill", function(d) { if(scale(d.values.total)<0){ return "deeppink"}else{return "mediumseagreen"}}) .attr("transform", function(d, i) { return "translate(0," + * barheight + ")"; });
update works fine. transition also. when there new bars added appear in place should appear , not have transition (e.g., opacity change, or delay before appears). there way can differentiate between new data transition , updating data transition? looks pretty ugly , cannot believe how d3 things. there must workaround this. in advance!
thanks @thisoneguy gave hint opacity. started understanding "setup" means inital before transition, affects newly added elements in update. here solution (i went opacity change + new elements coming bottom):
var bar=chart.selectall(".bar") .data(data); bar.enter().append("rect") .attr("class", "bar") .attr("x", function(d) { if(scale(d.values.total)<0){return width/2+widthdatelabel;}else{return width/2-scale(d.values.total)-widthdatelabel;}}) .attr("width", function(d) { return math.abs(scale(d.values.total)); }) .attr("height", barheight - 1) .attr("opacity", 0) .attr("fill", function(d) { if(scale(d.values.total)<0){ return "deeppink"}else{return "mediumseagreen"}}) .attr("transform", function(d, i) { return "translate(0," + (i+math.abs(monthlengthdiff)) * (barheight) + ")"; }); bar.exit() .transition().duration(750) .attr("transform", function(d, i) { return "translate(0," + (i + 1) * barheight + ")"; }) .style("opacity", 0) .remove(); bar .transition().duration(750) .attr("height", barheight - 1) .attr("x", function(d) { if(scale(d.values.total)<0){return width/2+widthdatelabel;}else{return width/2-scale(d.values.total)-widthdatelabel;}}) .attr("width", function(d) { return math.abs(scale(d.values.total)); }) .attr("fill", function(d) { if(scale(d.values.total)<0){ return "deeppink"}else{return "mediumseagreen"}}) .attr("transform", function(d, i) { return "translate(0," + * barheight + ")"; }) .attr("opacity", 1)
Comments
Post a Comment