D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
bricedev
Full window
Github gist
Text tween
Verticale Bar Chart with text tween transition
<!DOCTYPE html> <meta charset="utf-8"> <style> body { font: 10px sans-serif; } .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } .x.axis { display: none; } </style> <body> <script src="https://d3js.org/d3.v3.min.js"></script> <script> var margin = {top: 20, right: 30, bottom: 20, left: 30}, width = 500 - margin.left - margin.right, height = 700 - margin.top - margin.bottom; var formatPercent = d3.format(".3s"); var x = d3.scale.linear() .range([0, width]); var y = d3.scale.ordinal() .rangeRoundBands([height, 0], .3, .3); var xAxis = d3.svg.axis() .scale(x) .orient("bottom") .tickFormat(formatPercent); var yAxis = d3.svg.axis() .scale(y) .tickSize(0) .orient("left"); 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("data.csv", function(error, data) { data.forEach(function(d) { d.frequency = +d.frequency; }); x.domain([0, d3.max(data, function(d) { return d.frequency; })]); y.domain(data.map(function(d) { return d.letter; })); var bar = svg.selectAll(".bar") .data(data) .enter().append("g") .attr("class", "bar"); bar.append("rect") .attr("y", function(d) { return y(d.letter); }) .attr("height", y.rangeBand()) .attr("width", 0) .style("fill","#1a9850"); bar.append("text") .attr("x", 5) .attr("y", function(d) { return y(d.letter) + y.rangeBand()/2 + 3; }) .text(0); bar.selectAll("rect").transition().ease("quad-out").duration(2000).delay(0).attr("width", function(d) { return x(d.frequency); }); bar.selectAll("text").transition().ease("quad-out").duration(2000).delay(0) .attr("x", function(d) { return x(d.frequency) + 3; }) .tween("text", function(d) { var i = d3.interpolate(0, d.frequency); return function(t) { d3.select(this).text(formatPercent(i(t))); }; }); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .call(yAxis) .selectAll("text") .style("font-weight","bold"); d3.select(self.frameElement).style("height", (height + margin.top + margin.bottom) + "px"); }); </script>
https://d3js.org/d3.v3.min.js