Skip to content

Instantly share code, notes, and snippets.

@thulse
Last active November 8, 2017 16:24
Show Gist options
  • Save thulse/51ed310aef4ced2db6895aece2cc2355 to your computer and use it in GitHub Desktop.
Save thulse/51ed310aef4ced2db6895aece2cc2355 to your computer and use it in GitHub Desktop.
ITS_Grouped Bar Chart
license: gpl-3.0

This grouped bar chart is constructed from a CSV file storing the populations of different states by age group. The chart employs conventional margins and a number of D3 features:

forked from mbostock's block: Grouped Bar Chart

State Under 5 Years 5 to 13 Years 14 to 17 Years 18 to 24 Years 25 to 44 Years 45 to 64 Years 65 Years and Over
CA 2704659 4499890 2159981 3853788 10604510 8819342 4114496
TX 2027307 3277946 1420518 2454721 7017731 5656528 2472223
NY 1208495 2141490 1058031 1999120 5355235 5120254 2607672
FL 1140516 1938695 925060 1607297 4782119 4746856 3187797
IL 894368 1558919 725973 1311479 3596343 3239173 1575308
PA 737462 1345341 679201 1203944 3157759 3414001 1910571
<!DOCTYPE html>
<style>
.axis .domain {
display: none;
}
</style>
<svg width="960" height="500"></svg>
<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,
g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var x0 = d3.scaleBand()
.rangeRound([0, width])
.paddingInner(0.1);
var x1 = d3.scaleBand()
.padding(0.05);
var y = d3.scaleLinear()
.rangeRound([height, 0]);
//var color = d3.scale.category10();
var z = d3.scaleOrdinal()
.range(["#0068f9", "#00a810", "#c7091c", "#a865dc", "#d0743c", "#a865dc"]);
d3.csv("itsLearning.csv", function(d, i, columns) {
for (var i = 1, n = columns.length; i < n; ++i) d[columns[i]] = +d[columns[i]];
return d;
}, function(error, data) {
if (error) throw error;
var keys = data.columns.slice(1);
x0.domain(data.map(function(d) { return d.mcasBin; }));
x1.domain(keys).rangeRound([0, x0.bandwidth()]);
y.domain([0, d3.max(data, function(d) { return d3.max(keys, function(key) { return d[key]; }); })]).nice();
g.append("g")
.selectAll("g")
.data(data)
.enter().append("g")
.attr("transform", function(d) { return "translate(" + x0(d.mcasBin) + ",0)"; })
.selectAll("rect")
.data(function(d) { return keys.map(function(key) { return {key: key, value: d[key]}; }); })
.enter().append("rect")
.attr("x", function(d) { return x1(d.key); })
.attr("y", function(d) { return y(d.value); })
.attr("width", x1.bandwidth())
.attr("height", function(d) { return height - y(d.value); })
.attr("fill", function(d) { return z(d.key); });
g.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x0));
g.append("g")
.attr("class", "axis")
.call(d3.axisLeft(y).ticks(null, "s"))
.append("text")
.attr("x", 2)
.attr("y", y(y.ticks().pop()) + 0.5)
.attr("dy", "0.32em")
.attr("fill", "#000")
.attr("font-weight", "bold")
.attr("text-anchor", "start")
;
var legend = g.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("text-anchor", "end")
.selectAll("g")
.data(keys.slice().reverse())
.enter().append("g")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
// var bars = svg.selectAll(".bandwidth")
// .data(data)
// .enter()
// .append("rect")
// .attr("class", "bar")
// .attr("cx", function(d) { return x1(d.learningComponent); })
// .attr("cy", function(d) { return y(d.value); })
// .style("fill", function(d) { return z(d.learningComponent); })
// .attr("data-category", function (d) {
// return d.learningComponent;
// })
// .style("opacity", 0.7);
legend.append("rect")
.attr("x", width - 19)
.attr("width", 19)
.attr("height", 19)
.attr("fill", z)
// .on("click", function (d) {
// bars.filter(function () {
// return this.dataset.learningComponent === d;
// })
// .transition().duration(750)
// .style("opacity", function () {
// console.log(this.style.opacity);
// return (parseInt(this.style.opacity)) ? 0 : 1;
// });
// });
legend.append("text")
.attr("x", width - 24)
.attr("y", 9.5)
.attr("dy", "0.32em")
.text(function(d) { return d; });
});
</script>
mcasBin correct time attempts hints
Advanced 0.600768089 1.357276797 1.288727985 0.22715517
Proficient 0.443741245 1.480581049 1.454132233 0.349795914
Needs Improvement 0.344014923 1.57597224 1.562283694 0.5103102
Warning 0.293510258 2.042130117 1.760844963 0.631580371
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment