d3.js - Using D3, how can I transition a line one point at time? -
i'm working on visualization project in 1 component line chart overlayed on bar graph. delayed bar transitions @ time. line transition each point on line remains "attached" bar.
here's code line:
var line = d3.svg.line() .x(function(d, i) { return xscale(i) + 20; }) .y(function(d) { return h - yscale(parsefloat(d.performance)); }); svg1.append("svg:path").attr("d", line(dataset[0].months)); and here's transition it:
svg1.select("path") .transition() .ease("linear") .duration(1000) .attr("d", line(dataset[count].months)); i've seen other questions addressing d3 line transitions none seem address issue, hope i'm not repeater. in advance!
note: did try putting delay() function in after transition didn't work. i'm assuming because line single <path> instead of multiple <rect> elements...
so fell off radar while, had time other day , figured out 1 approach doing delayed transition...
here pen wrote. generated random data , created simple line chart showing stock prices play around with. trick here instead of iterating through selection of elements using transition, iterate through dataset updating point point , transitioning line go:
dataset.foreach(function(item, index) { let set = dataset.slice(); // update current point in copy set[index].price = newprice(); stock_line.transition() .delay(index * 500) .attr('d', line_generator(set)); }); admittedly bit hacky , possibly overkill, update whole line @ once. @lars mentioned possibility of using stroke-dashoffset trick accomplish well. have played around method animate drawing line i'm not sure how i'd use accomplish same delayed animation shown in pen. if there less hacky implementation please let me know , i'll update answer.
Comments
Post a Comment