Verticale Bar Chart with text tween transition
Last active
March 24, 2016 16:09
-
-
Save bricedev/a0c5ef180272fac3aea6 to your computer and use it in GitHub Desktop.
Text tween
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
letter | frequency | |
---|---|---|
A | .08167 | |
B | .01492 | |
C | .02780 | |
D | .04253 | |
E | .12702 | |
F | .02288 | |
G | .02022 | |
H | .06094 | |
I | .06973 | |
J | .00153 | |
K | .00747 | |
L | .04025 | |
M | .02517 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment