This stacked 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:
-
-
Save yoon-gu/b18fdcd336ee9a358248200fabed965c to your computer and use it in GitHub Desktop.
2016 감성고기 총매출 Stacked Bar Chart
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
license: gpl-3.0 |
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
month | 양재점 | 바스킷점 | 온라인 | |
---|---|---|---|---|
1월 | 109578890 | 42938884 | 16102110.0 | |
2월 | 124268751 | 34667326 | 10939580.0 | |
3월 | 67872227 | 34215476 | 7575400.0 | |
4월 | 73959820 | 37759278 | 6081770.0 | |
5월 | 76332199 | 34330530 | 7052160.0 | |
6월 | 69057732 | 35751100 | 5357600.0 | |
7월 | 74981136 | 30696232 | 6912963.0 | |
8월 | 53890525 | 34078964 | 6361627.0 | |
9월 | 130643986 | 78149546 | 14685629.0 | |
10월 | 57735372 | 35570988 | 6412914.0 | |
11월 | 60942085 | 42575613 | 4265066.0 | |
12월 | 74199450 | 63609123 | 15220649.0 |
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> | |
<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 x = d3.scaleBand() | |
.rangeRound([0, width]) | |
.paddingInner(0.05) | |
.align(0.1); | |
var y = d3.scaleLinear() | |
.rangeRound([height, 0]); | |
var z = d3.scaleOrdinal() | |
.range(["#8a89a6", "#6b486b", "#d0743c"]); | |
d3.csv("data.csv", function(d, i, columns) { | |
for (i = 1, t = 0; i < columns.length; ++i) t += d[columns[i]] = +d[columns[i]]; | |
d.total = t; | |
return d; | |
}, function(error, data) { | |
if (error) throw error; | |
var keys = data.columns.slice(1); | |
// data.sort(function(a, b) { return b.total - a.total; }); | |
x.domain(data.map(function(d) { return d.month; })); | |
y.domain([0, d3.max(data, function(d) { return d.total; })]).nice(); | |
z.domain(keys); | |
g.append("g") | |
.selectAll("g") | |
.data(d3.stack().keys(keys)(data)) | |
.enter().append("g") | |
.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.month); }) | |
.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") | |
.attr("transform", "translate(0," + height + ")") | |
.call(d3.axisBottom(x)); | |
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") | |
.text("Revenue"); | |
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 + ")"; }); | |
legend.append("rect") | |
.attr("x", width - 19) | |
.attr("width", 19) | |
.attr("height", 19) | |
.attr("fill", z); | |
legend.append("text") | |
.attr("x", width - 24) | |
.attr("y", 9.5) | |
.attr("dy", "0.32em") | |
.text(function(d) { return d; }); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment