Built with blockbuilder.org
Created
May 14, 2017 19:58
-
-
Save maelafifi/7a2ae6e84c4d3486d2f8bcdf5cc018e9 to your computer and use it in GitHub Desktop.
boringGroupedChart
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: mit |
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
Occupation | payMen | payTotal | payWomen | |
---|---|---|---|---|
Management/Professional Occupations | 1420 | 1188 | 1027 | |
Service occupations | 595 | 523 | 487 | |
Sales and office occupations | 805 | 698 | 649 | |
Natural Resources/Construction Occupations | 794 | 786 | 580 | |
Production/Transportation Occupations | 704 | 665 | 531 |
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="400"></svg> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script> | |
var svg = d3.select("svg"), | |
margin = {top: 20, right: 20, bottom: 50, 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 z = d3.scaleOrdinal() | |
.range(["blue","grey","pink"]); | |
d3.csv("data.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.Occupation; })); | |
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.Occupation) + ",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)) | |
.selectAll("text") | |
.attr("y", 20) | |
.attr("x", -70) | |
.attr("dy", ".35em") | |
.attr("transform", "rotate(5)") | |
.style("text-anchor", "start");; | |
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("Pay (Weekly)"); | |
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