javascript - d3 Line Chart animations from left to right -
i interested create line chart -- draws left right -- connecting dots in x-axis.
http://bl.ocks.org/markmarkoh/8700606 http://bl.ocks.org/duopixel/4063326
this jsfiddle i've made start creating chart. http://jsfiddle.net/nyeax/1512/
//__invoke line $('[data-role="line"]').each(function(index) { createline(this); }); function createline(el){ var w = $(el).data("width"); var h = $(el).data("height"); var svg = d3.select($(el)[0]) .append("svg") .attr("width", w) .attr("height", h); var data = d3.range(11).map(function(){return math.random()*10}) var x = d3.scale.linear().domain([0, 10]).range([0, 700]); var y = d3.scale.linear().domain([0, 10]).range([10, 290]); var line = d3.svg.line() .interpolate("cardinal") .x(function(d,i) {return x(i);}) .y(function(d) {return y(d);}) var path = svg.append("path") .attr("d", line(data)) .attr("stroke", "steelblue") .attr("stroke-width", "2") .attr("fill", "none"); var totallength = path.node().gettotallength(); path .attr("stroke-dasharray", totallength + " " + totallength) .attr("stroke-dashoffset", totallength) .transition() .duration(2000) .ease("linear") .attr("stroke-dashoffset", 0); svg.on("click", function(){ path .transition() .duration(2000) .ease("linear") .attr("stroke-dashoffset", totallength); }); /* plot axis */ var padding = 100; // space around chart, not including labels // define x axis var xaxis = d3.svg.axis() .orient("bottom") .scale(x); //.tickformat(date_format); // draw x axis labels , move bottom of chart area svg.append("g") .attr("class", "xaxis axis") // 2 classes, 1 css formatting, 1 selection below .attr("transform", "translate(0," + (h - padding) + ")") .call(xaxis); /* plot axis */ }
one solution can append svg circles correspond path before drawing path. example fiddle: http://jsfiddle.net/stancheta/ystymlm4/6/
var circles = svg.selectall("dot") .data(data) .enter().append("svg:circle") .attr('class', 'circ') .attr("r", 3) .attr("cx", function(d, i) { return x(i); }) .attr("cy", function(d, i) { return y(d); }) .style('fill', 'lightsteelblue');
Comments
Post a Comment