Skip to content

Instantly share code, notes, and snippets.

@gsarfaty
Created May 8, 2015 16:05
Show Gist options
  • Save gsarfaty/cf66a8e7241ad7bdfc52 to your computer and use it in GitHub Desktop.
Save gsarfaty/cf66a8e7241ad7bdfc52 to your computer and use it in GitHub Desktop.
Child Marriage - bar w/axes
<!DOCTYPE html>
<html lang="en">
<head>
<script src="//use.edgefonts.net/cabin.js"></script>
<meta charset="utf-8">
<title>Child Marriage</title>
<script type="text/javascript" src="http://d3js.org/d3.v3.js"></script>
<style type="text/css">
body {
background-color: white;
}
h1 {
font-family: cabin, sans-serif;
font-style: normal;
font-weight: 400;
}
p {
font-family: cabin, sans-serif;
font-size: 14px;
margin: 10px 0 0 0;
}
svg {
background-color: white;
}
rect:hover {
fill: #FFCC00;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 11px;
}
.y.axis path,
.y.axis line {
opacity: 0;
}
</style>
</head>
<body>
<h1>Child Marriage</h1>
<p>Prevalence of child marriage by country. Source: <a href="http://www.dhsprogram.com/">Demographic and Health Surveys</a>. ICF International </p>
<script type="text/javascript">
var w = 700;
var h = 800;
var padding = [ 20, 10, 50, 150 ]; //Top, right, bottom, left
var widthScale = d3.scale.linear()
.range([ 0, w - padding[1] - padding[3] ]);
var heightScale = d3.scale.ordinal()
.rangeRoundBands([ padding[0], h - padding[2] ], 0.1);
var xAxis = d3.svg.axis()
.scale(widthScale)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(heightScale)
.orient("left");
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
d3.csv("marriage.csv", function(data) {
data.sort(function(a, b) {
return d3.descending(+a.childmarriage, +b.childmarriage);
});
widthScale.domain([ 0, d3.max(data, function(d) {
return +d.childmarriage;
}) ]);
heightScale.domain(data.map(function(d) { return d.Country; } ));
var rects = svg.selectAll("rect")
.data(data)
.enter()
.append("rect");
rects.attr("x", padding[3])
.attr("y", function(d) {
return heightScale(d.Country);
})
.attr("width", function(d) {
return widthScale(d.childmarriage);
})
.attr("height", heightScale.rangeBand())
.attr("fill", "#C9CACC")
.append("title")
.text(function(d) {
return d.childmarriage + " percent of women in " + d.Country + " are married by age 18";
});
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(" + padding[3] + "," + (h - padding[2]) + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + padding[3] + ",0)")
.call(yAxis);
});
</script>
</body>
</html>
Country Survey childmarriage
Albania 2008-09 DHS 9.6
Armenia 2010 DHS 7.2
Azerbaijan 2006 DHS 12.2
Bangladesh 2011 DHS 64.9
Benin 2011-12 DHS 31.9
Bolivia 2008 DHS 21.7
Burkina Faso 2010 DHS 51.6
Burundi 2010 DHS 20.4
Cambodia 2010 DHS 18.4
Cameroon 2011 DHS 38.4
Chad 2004 DHS 71.5
Colombia 2010 DHS 23
Comoros 2012 DHS 31.6
Congo (Brazzaville) 2011-12 DHS 32.6
DRC 2013-14 DHS 37.3
Cote d'Ivoire 2011-12 DHS 33.2
Dominican Republic 2013 DHS 36.5
Egypt 2008 DHS 16.6
Egypt 2005 DHS 16.6
Ethiopia 2011 DHS 41.2
Gabon 2012 DHS 21.9
Ghana 2008 DHS 24.6
Guinea 2012 DHS 51.7
Guyana 2009 DHS 23
Haiti 2012 DHS 17.5
Honduras 2011-12 DHS 33.6
India 2005-06 DHS 44.5
Indonesia 2012 DHS 17
Jordan 2012 DHS 8.4
Jordan 2007 DHS 9.6
Kenya 2008-09 DHS 26.4
Kyrgyz Republic 2012 DHS 7.8
Lesotho 2009 DHS 18.8
Liberia 2013 DHS 35.9
Madagascar 2008-09 DHS 48.2
Malawi 2010 DHS 49.6
Malawi 2004 DHS 48.9
Maldives 2009 DHS 3.9
Mali 2012-13 DHS 59.6
Moldova 2005 DHS 18.9
Morocco 2003-04 DHS 15.9
Mozambique 2011 DHS 48.2
Namibia 2006-07 DHS 8.5
Nepal 2011 DHS 40.7
Niger 2012 DHS 76.3
Nigeria 2013 DHS 42.8
Pakistan 2012-13 DHS 21
Peru 2012 DHS 19.1
Philippines 2013 DHS 15
Rwanda 2010 DHS 8.1
Sao Tome and Principe 2008-09 DHS 34.4
Senegal 2010-11 DHS 32.9
Sierra Leone 2013 DHS 38.9
Swaziland 2006-07 DHS 5
Tajikistan 2012 DHS 11.6
Tanzania 2010 DHS 36.9
Timor-Leste 2009-10 DHS 18.9
Uganda 2011 DHS 39.7
Ukraine 2007 DHS 9.9
Zambia 2007 DHS 41.6
Zimbabwe 2010-11 DHS 30.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment