Skip to content

Instantly share code, notes, and snippets.

@saraquigley
Last active July 13, 2017 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saraquigley/fff2204cde6fa769696ef143273560ec to your computer and use it in GitHub Desktop.
Save saraquigley/fff2204cde6fa769696ef143273560ec to your computer and use it in GitHub Desktop.
Bar Chart with update
license: gpl-3.0

Here's a fork of Mike Bostock's iconic bl.ock, with an added change event for Noam.

This simple bar chart is constructed from a TSV file storing the frequency of letters in the English language. The chart employs conventional margins and a number of D3 features:

forked from mbostock's block: Bar Chart

group letter frequency
1 A 0.08167
1 B 0.01492
1 C 0.02782
1 D 0.04253
1 E 0.12702
1 F 0.02288
1 G 0.02015
1 H 0.06094
1 I 0.06966
1 J 0.00153
1 K 0.00772
1 L 0.04025
1 M 0.02406
1 N 0.06749
1 O 0.07507
1 P 0.01929
1 Q 0.00095
1 R 0.05987
1 S 0.06327
1 T 0.09056
1 U 0.02758
1 V 0.00978
1 W 0.02360
1 X 0.00150
1 Y 0.01974
1 Z 0.00074
2 A 0.00074
2 B 0.01974
2 C 0.0015
2 D 0.0236
2 E 0.00978
2 F 0.02758
2 G 0.09056
2 H 0.06327
2 I 0.05987
2 J 0.00095
2 K 0.01929
2 L 0.07507
2 M 0.06749
2 N 0.02406
2 O 0.04025
2 P 0.00772
2 Q 0.00153
2 R 0.06966
2 S 0.06094
2 T 0.02015
2 U 0.02288
2 V 0.12702
2 W 0.04253
2 X 0.02782
2 Y 0.01492
2 Z 0.08167
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.bar {
fill: steelblue;
}
.bar:hover {
fill: brown;
}
.axis--x path {
display: none;
}
svg {
position: absolute;
margin-top: 40px;
}
select {
margin-left: 60px;
}
</style>
<body>
<select id="filter">
<option value="1">1</option>
<option value="2">2</option>
</select>
<svg width="960" height="500"></svg>
</body>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var svg = d3.select("svg"),
margin = {top: 20, right: 20, bottom: 30, left: 40},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom;
var x = d3.scaleBand().rangeRound([0, width]).padding(0.1),
y = d3.scaleLinear().rangeRound([height, 0]);
var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// create a variable that will hold the loaded data
var tsv;
// load the data
d3.tsv("data.tsv", function(d) {
d.frequency = +d.frequency;
return d;
}, function(error, datafile) {
if (error) throw error;
// put the original data in tsv
tsv = datafile;
// filter the data based on the inital value
var data = tsv.filter(function(d) {
var sq = d3.select("#filter").property("value");
return d.group === sq;
});
// set the domains of the axes
x.domain(data.map(function(d) { return d.letter; }));
y.domain([0, d3.max(data, function(d) { return d.frequency; })]);
// add the svg elements
g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
g.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y).ticks(10, "%"))
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.attr("text-anchor", "end")
.text("Frequency");
// create the bars
g.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.letter); })
.attr("y", function(d) { return y(d.frequency); })
.attr("width", x.bandwidth())
.attr("height", function(d) { return height - y(d.frequency); });
// add a change event handler
d3.select("#filter").on("change", function() {
applyFilter(this.value);
});
// call this whenever the filter changes
function applyFilter(value) {
// filter the data
var data = tsv.filter(function(d) {return d.group === value;})
// update the bars
d3.selectAll(".bar")
.data(data)
.transition().duration(1000)
.attr("x", function(d) { return x(d.letter); })
.attr("y", function(d) { return y(d.frequency); })
.attr("height", function(d) { return height - y(d.frequency); });
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment