D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
quizzicol
Full window
Github gist
animated_rank
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <style> body { font: 10px sans-serif; } .axis path, .axis line { fill: none; stroke: #151516; /*shape-rendering: crispEdges;*/ } /*.x.axis path { display: none; }*/ .line { fill: none; /*stroke: steelblue;*/ stroke-width: 1.5px; } </style> <body> <script src="//d3js.org/d3.v3.min.js"></script> <script> var margin = {top: 20, right: 80, bottom: 30, left: 50}, width = 900 - margin.left - margin.right, height = 400 - margin.top - margin.bottom; // var parseDate = d3.time.format("%Y%m%d").parse; var x = d3.scale.linear() .range([0, width]); var y = d3.scale.linear() .range([height, 0]); var color = d3.scale.ordinal() .domain(['Redbacks','Pirates','Dragons','SDK']) .range(["#c52035", '#000000','#007f00', '#FFA85A']); var xAxis = d3.svg.axis() .scale(x) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .orient("left"); var line = d3.svg.line() .interpolate("basis") .x(function(d) { return x(d.week); }) .y(function(d) { return y(d.rank); }); var svg = d3.select("body").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); d3.csv("rank.csv", function(error, data) { if (error) throw error; var teams = color.domain().map(function(name) { return { name: name, values: data.map(function(d) { return {week: d.week, rank: +d[name]}; }) }; }); x.domain(d3.extent(data, function(d) {return +d.week; })); y.domain([ d3.max(teams, function(c) { return d3.max(c.values, function(v) { return v.rank; }); }), d3.min(teams, function(c) { return d3.min(c.values, function(v) { return v.rank; }); }) ]); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .call(yAxis) .append("text") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end") .text("Rank"); var team = svg.selectAll(".team") .data(teams) .enter().append("g") .attr("class", "team") team.append("path") .attr("class", "line") .attr("d", function(d) { return line(d.values); }) .style("stroke", function(d) { return color(d.name); }); team.append("text") .datum(function(d) { return {name: d.name, value: d.values[d.values.length - 1]}; }) .attr("transform", function(d) { return "translate(" + x(d.value.week) + "," + y(d.value.rank) + ")"; }) .attr("x", 3) .attr("dy", ".35em") .text(function(d) { return d.name; }); team.append("image") .datum(function(d) { return {name: d.name, value: d.values[d.values.length - 1]}; }) .attr("transform", function(d) { return "translate(" + x(d.value.week) + "," + y(d.value.rank) + ")"; }) .attr("x", 3) .attr("dy", ".85em") .attr("width", "24px") .attr("height", "24px") .attr("xlink:href", function(d) { return d.name + ".png"; }); /*============================================================================== ANIMATION FROM HERE ON */ var curtain = svg.append('rect') .attr('x', -1 * width) .attr('y', -1 * height) .attr('height', height) .attr('width', width) .attr('class', 'curtain') .attr('transform', 'rotate(180)') .style('fill', '#ffffff') /* Optionally add a guideline */ var guideline = svg.append('line') .attr('stroke', '#333') .attr('stroke-width', 0) .attr('class', 'guide') .attr('x1', 1) .attr('y1', 1) .attr('x2', 1) .attr('y2', height) var t = svg.transition() .delay(750) .duration(6000) .ease('linear') .each('end', function() { d3.select('line.guide') .transition() .style('opacity', 0) .remove() }); t.select('rect.curtain') .attr('width', 0); t.select('line.guide') .attr('transform', 'translate(' + width + ', 0)') d3.select("#show_guideline").on("change", function(e) { guideline.attr('stroke-width', this.checked ? 1 : 0); curtain.attr("opacity", this.checked ? 0.75 : 1); }) }); </script>
https://d3js.org/d3.v3.min.js