d3에서 경로 그리기 애니메이션을 수행하는 한 가지 방법은 대시 배열 및 대시 오프셋 특성을 사용하는 것입니다.
대시 오프셋을 총 경로 길이로 설정 한 다음 시간이 지남에 따라 대시 오프셋이 0이 될 때까지 줄입니다. 이는 그려지는 경로를 시뮬레이션합니다. stroke-dasharray 및 stroke-dashoffset에 대한 SVG 문서를 확인하십시오.
개념적으로, 당신이하고있는 것은 이것이다 :
은 (----) 당신의 라인은 4 대 긴 말. dasharray를 (---- ,,,,) 즉 4 단위 다음 4 칸으로 설정합니다. 대시 오프셋을 4 단위로 설정하면 선이 보이는 공간의 왼쪽에 4 단위 배치됩니다. 그런 다음 dashoffset을 0으로 줄이면 전체 선이 그려 질 때까지 선이 (- ,,,,), (- ,,,)와 같이 보입니다.
var line = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.price); });
var path = svg.append("path")
.attr("d", line(data))
.classed("line", true);
var pathLength= path.node().getTotalLength();
path
.attr("stroke-dasharray", pathLength + " " + pathLength)
.attr("stroke-dashoffset", pathLength)
.transition()
.duration(2000)
.ease("linear")
.attr("stroke-dashoffset", 0);
-Duopixel's에서 배운
는 here을 게시 할 수 있습니다.
참조 http://stackoverflow.com/questions/14275249/how-can-i-animate-a-progressive-drawing-of-svg-path/14282391#14282391 – Phrogz