Built with blockbuilder.org
forked from vickygisel's block: Stacked bar chart
license: mit |
Built with blockbuilder.org
forked from vickygisel's block: Stacked bar chart
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
.bar { | |
fill: steelblue; | |
} | |
.axis path { | |
display: none; | |
} | |
</style> | |
<svg width="960" height="500"></svg> | |
<script src="//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 x = d3.scaleBand() | |
.rangeRound([0, width]) | |
.padding(0.1) | |
.align(0.1); | |
var y = d3.scaleLinear() | |
.rangeRound([height, 0]); | |
//var z = d3.scaleOrdinal() | |
// .range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]); | |
var z = d3.scaleOrdinal(d3.schemeCategory20c); | |
var stack = d3.stack(); | |
d3.csv("matricula_x_sala_x_region.csv", type, function(error, data) { | |
if (error) throw error; | |
data.sort(function(a, b) { return b.total - a.total; }); | |
x.domain(data.map(function(d) { return d.region; })); | |
y.domain([0, d3.max(data, function(d) { return d.total; })]).nice(); | |
z.domain(data.columns.slice(1)); | |
g.selectAll(".serie") | |
.data(stack.keys(data.columns.slice(1))(data)) | |
.enter().append("g") | |
.attr("class", "serie") | |
.attr("fill", function(d) { return z(d.key); }) | |
.selectAll("rect") | |
.data(function(d) { return d; }) | |
.enter().append("rect") | |
.attr("x", function(d) { return x(d.data.region); }) | |
.attr("y", function(d) { return y(d[1]); }) | |
.attr("height", function(d) { return y(d[0]) - y(d[1]); }) | |
.attr("width", x.bandwidth()); | |
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, "s")) | |
.append("text") | |
.attr("x", 2) | |
.attr("y", y(y.ticks(10).pop())) | |
.attr("dy", "0.35em") | |
.attr("text-anchor", "start") | |
.attr("fill", "#000") | |
.text("Matrícula"); | |
var legend = g.selectAll(".legend") | |
.data(data.columns.slice(1).reverse()) | |
.enter().append("g") | |
.attr("class", "legend") | |
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; }) | |
.style("font", "10px sans-serif"); | |
legend.append("rect") | |
.attr("x", width - 18) | |
.attr("width", 18) | |
.attr("height", 18) | |
.attr("fill", z); | |
legend.append("text") | |
.attr("x", width - 24) | |
.attr("y", 9) | |
.attr("dy", ".35em") | |
.attr("text-anchor", "end") | |
.text(function(d) { return d; }); | |
}); | |
function type(d, i, columns) { | |
for (i = 1, t = 0; i < columns.length; ++i) t += d[columns[i]] = +d[columns[i]]; | |
d.total = t; | |
return d; | |
} | |
</script> |
region | lactantes | deambuladores | 2 | 1 sección | 2 sección | 3 sección | |
---|---|---|---|---|---|---|---|
1 | 74 | 85 | 840 | 4579 | 5472 | 7321 | |
2 | 0 | 0 | 482 | 4279 | 5541 | 7474 | |
3 | 0 | 0 | 253 | 5107 | 7708 | 10552 | |
4 | 0 | 0 | 409 | 4884 | 7379 | 11500 | |
5 | 0 | 0 | 102 | 4338 | 8118 | 13468 | |
6 | 5 | 12 | 297 | 3706 | 4600 | 5838 | |
7 | 37 | 88 | 714 | 3656 | 4248 | 4599 | |
8 | 0 | 0 | 180 | 4487 | 6824 | 8620 | |
9 | 17 | 25 | 41 | 5406 | 9714 | 12807 | |
10 | 10 | 22 | 584 | 3063 | 4132 | 5516 | |
11 | 3 | 10 | 249 | 3334 | 5332 | 8264 | |
12 | 15 | 27 | 469 | 2679 | 3228 | 3489 | |
13 | 9 | 36 | 832 | 1635 | 1580 | 1986 | |
14 | 0 | 0 | 837 | 2055 | 2061 | 2374 | |
15 | 0 | 0 | 426 | 2066 | 1915 | 2492 | |
16 | 16 | 41 | 495 | 1341 | 1375 | 1419 | |
17 | 0 | 14 | 93 | 810 | 798 | 1077 | |
18 | 0 | 0 | 546 | 1657 | 1723 | 2297 | |
19 | 8 | 13 | 325 | 1761 | 1973 | 3056 | |
20 | 7 | 13 | 405 | 1550 | 1353 | 1909 | |
21 | 0 | 0 | 645 | 1208 | 1185 | 1360 | |
22 | 0 | 0 | 214 | 2613 | 3405 | 3796 | |
23 | 0 | 0 | 882 | 1032 | 624 | 1001 | |
24 | 8 | 29 | 462 | 1559 | 1544 | 1756 | |
25 | 0 | 0 | 197 | 930 | 928 | 976 |