Skip to content

Instantly share code, notes, and snippets.

@thulse
Last active November 4, 2017 22:03
Show Gist options
  • Save thulse/3380919c738180a189bf76f7e9a20697 to your computer and use it in GitHub Desktop.
Save thulse/3380919c738180a189bf76f7e9a20697 to your computer and use it in GitHub Desktop.
[Unlisted]ITS_Grouped Bar Chart
license: mit

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

forked from ninjaPixel'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>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar {
fill: steelblue;
}
.x.axis path {
display: none;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x0 = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var x1 = d3.scale.ordinal();
var y = d3.scale.linear()
.range([height, 0]);
var color = d3.scale.ordinal()
.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);
var xAxis = d3.svg.axis()
.scale(x0)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(d3.format(".2s"));
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("itsLearning.csv", function(error, data) {
if (error) throw error;
var learningComponent = d3.keys(data[0]).filter(function(key) { return key !== "mcasBin"; });
console.log(data);
console.log(learningComponent);
data.forEach(function(d) {
d.learningComponent = learningComponent.map(function(name) { return {name: name, value: +d[name]}; });
});
console.log(data);
x0.domain(data.map(function(d) { return d.mcasBin; }));
x1.domain(learningComponent).rangeRoundBands([0, x0.rangeBand()]);
y.domain([0, d3.max(data, function(d) { return d3.max(d.learningComponent, function(d) { return d.value; }); })]);
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("Population")
;
var mcasBin = svg.selectAll(".mcasBin")
.data(data)
.enter().append("g")
.attr("class", "g")
.attr("transform", function(d) { return "translate(" + x0(d.mcasBin) + ",0)"; });
mcasBin.selectAll("rect")
.data(function(d) { return d.value; })
.enter().append("rect")
.attr("width", x1.rangeBand())
.attr("x", function(d) { return x1(d.name); })
.attr("y", function(d) { return y(d.value); })
.attr("height", function(d) { return height - y(d.value); })
.style("fill", function(d) { return color(d.name); });
var legend = svg.selectAll(".legend")
.data(learningComponent.slice().reverse())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
});
</script>
</body>
learningComponent value mcasBin
correct 0.600768089 Advanced
correct 0.443741245 Proficient
correct 0.344014923 Needs Improvement
correct 0.293510258 Warning
mcas 47.50980392 Advanced
mcas 38.30714286 Proficient
mcas 27.19723183 Needs Improvement
mcas 14.32044199 Warning
time 1.357276797 Advanced
time 1.480581049 Proficient
time 1.57597224 Needs Improvement
time 2.042130117 Warning
attempts 1.288727985 Advanced
attempts 1.454132233 Proficient
attempts 1.562283694 Needs Improvement
attempts 1.760844963 Warning
hints 0.22715517 Advanced
hints 0.349795914 Proficient
hints 0.5103102 Needs Improvement
hints 0.631580371 Warning
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment