Created
November 15, 2015 16:56
-
-
Save adrianblanco/ae4600180c737fe8cd94 to your computer and use it in GitHub Desktop.
Population Distribution ethnicity
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> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Stack layout, stacked bar chart, but flipped</title> | |
<script type="text/javascript" src="//d3js.org/d3.v3.min.js"></script> | |
<style type="text/css"> | |
body { | |
background-color: white; | |
font-family: Helvetica, Arial, sans-serif; | |
} | |
h1 { | |
font-size: 24px; | |
margin: 0; | |
} | |
p { | |
font-size: 14px; | |
margin: 10px 0 0 0; | |
} | |
svg { | |
background-color: white; | |
} | |
rect:hover { | |
fill: grey; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Population Distribution by Race/Ethnicity</h1> | |
<p>Population distribution by race in the United States. Source: <a href="http://kff.org/other/state-indicator/distribution-by-raceethnicity/">KFF</a>, 2014</p> | |
<p><FONT COLOR="#936abb">█</FONT> White <FONT COLOR="#d42a2f">█</FONT> Black <FONT COLOR="#339f34">█</FONT> Hispanic <FONT COLOR="#fd7f28">█</FONT> Asian <FONT COLOR="#2678b2">█</FONT> Other</p> | |
<br> | |
<script type="text/javascript"> | |
//Width and height | |
var w = 640; | |
var h = 400; | |
var padding = [ 0, 0, 0, 0 ]; //Top, right, bottom, left | |
//Original data | |
var dataset = [ | |
[ | |
{ x: 0, y: 2, z: "2 % of the population in the US is from another ethnicity" }, | |
], | |
[ | |
{ x: 0, y: 6, z: "6 % of the population in the US is Asian" }, | |
], | |
[ | |
{ x: 0, y: 12, z: "12 % of the population in the US is Black" }, | |
], | |
[ | |
{ x: 0, y: 18, z: "18 % of the population in the US is Hispanic" }, | |
], | |
[ | |
{ x: 0, y: 62, z: "62 % of the population in the US is White" }, | |
], | |
]; | |
//Set up stack method | |
var stack = d3.layout.stack() | |
.order("reverse"); //Flip the order | |
//Data, stacked | |
stack(dataset); | |
//Set up scales | |
var xScale = d3.scale.ordinal() | |
.domain(d3.range(dataset[0].length)) | |
.rangeRoundBands([0, w], 0.05); | |
var yScale = d3.scale.linear() | |
.domain([0, | |
d3.max(dataset, function(d) { | |
return d3.max(d, function(d) { | |
return d.y0 + d.y; | |
}); | |
}) | |
]) | |
.range([0, h]); | |
//Easy colors accessible via a 10-step ordinal scale | |
var colors = d3.scale.category10() | |
//Create SVG element | |
var svg = d3.select("body") | |
.append("svg") | |
.attr("width", w) | |
.attr("height", h); | |
// Add a group for each row of data | |
var groups = svg.selectAll("g") | |
.data(dataset) | |
.enter() | |
.append("g") | |
.style("fill", function(d, i) { | |
return colors(i); | |
//if(d.value > x) return 'steelblue' | |
//else if(d.value > y) | |
}); | |
// Add a rect for each data value | |
var rects = groups.selectAll("rect") | |
.data(function(d) { return d; }) | |
.enter() | |
.append("rect") | |
.attr("x", function(d, i) { | |
return xScale(i); | |
}) | |
.attr("width", xScale.rangeBand()) | |
.attr("y", function(d) { | |
return h - yScale(d.y0) - yScale(d.y); //Flip the math! | |
}) | |
.attr("height", function(d) { | |
return yScale(d.y); | |
}) | |
.append("title") | |
.text(function(d) { | |
return d.z ; | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment