Note that there are some explanatory texts on larger screens.

plurals
  1. POD3.js enter transition for path not working
    primarykey
    data
    text
    <p>I am using d3 to create a line graph visualization for a bunch of data. The data for the line comes as an array of objects, and I use this function to translate that data into an svg path that can be displayed:</p> <pre><code>this.line = d3.svg.line() .interpolate('linear') // .x(function(d,i) {return x(d['date']);}) .x(function (d,i) {return x(i);}) .y(function (d,i) {return y(d[this.key]) - this.yOffset;}); </code></pre> <p>However, if I leave all my data as a single array of objects (and set that as the only data point for the path), then I will not be able to use enter and exit transitions to add/remove only some segments composing the line if my data set changes size. So, to solve this issue, I use this function to split my array of points into a 2D array: an array of arrays of length 2 that each contain the start and end point for one particular segment of my line, and then I use that array as the data for my path, with each segment on the screen bound separately to its own 2-element array.</p> <p>When I call setData and enter, the path draws exactly as it should. However, when I update the data with the update function (in this case I am transitioning to a longer data set), the path goes through the appropriate update and exit transitions, which shows that my key function that I supplied when binding the data is working as expected. But then when I call enter again, it doesn't just draw the one segment that needs to be added -- instead, it erases the existing path, and redraws the <strong>whole</strong> line, this time including the new segment.</p> <p>Is there something I'm missing here? How do I get it to only animate the drawing of the new segment, and not erase the existing line segments?</p> <p>All relevant code is below -- feel free to comment if I need to clarify more:</p> <p>Thanks in advance!</p> <pre><code>this.line = d3.svg.line() .interpolate('linear') .x(function (d,i) {return x(d['date']);}) .y(function (d,i) {return y(d[this.key]) - this.yOffset;}); this.pathEnterData = function(selection) { this.path = selection.enter().append('path') .attr('class','segment') .attr('d',function(d, i) {return mThis.line(d);}) .style('stroke', mThis.pathColor) .style('stroke-width',2) .style('fill','none'); }; this.pathEnterTransition = function(selection) { return selection .attr("stroke-dasharray", function(d) { var totLength = d3.select(this).node().getTotalLength(); return totLength + " " + totLength; }) .attr("stroke-dashoffset", function(d) { var totLength = d3.select(this).node().getTotalLength(); return totLength; }) .transition() .delay(function(d,i) { return segmentDuration*i; }) .duration(segmentDuration) .ease("linear") .attr('stroke-dashoffset',0); }; this.enterData = function() { this.path.call(this.pathEnterData); }; this.enterTransition = function() { this.path.call(this.pathEnterTransition); }; this.enter = function() { this.enterData(); this.enterTransition(); }; this.exitTransition = function() { this.path.exit().transition() .duration(exitTransitionDuration) .style("stroke-opacity", 0.0) .style("fill-opacity", 0.0) .remove(); }; this.update = function(newData) { this.setData(newData); this.exitTransition(); this.updateTransition(); setTimeout(function() {mThis.enter();},updateTransitionDuration); }; this.updateTransition = function() { this.bubbleCircles.transition() .duration(updateTransitionDuration) .attr("cx", function (d, i) { return x(d['date']); }) .call(mThis.setCircleY); this.bubbleText.transition() .duration(updateTransitionDuration) .attr("x", function (d, i) { return x(d['date']) - (13.0/16)*bubbleNumberSize; }) .call(mThis.setTextY); }; this.setData = function(points) { var splitPoints = new Array(points.length-1); for(var i=0; i&lt;points.length-1; i++) { splitPoints[i] = [points[i],points[i+1]];//{'start':points[i],'end':points[i+1]}; } this.path = this.path.data(splitPoints,function(d,i){return d[1]['date']}); }; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload