Skip to content

Instantly share code, notes, and snippets.

@murtra murtra/autonomies.csv
Last active Dec 19, 2015

Embed
What would you like to do?
module1project: Bar chart
autonomy projects
Andalucía 78
Aragón 20
Canarias 27
Cantabria 9
Castilla - La Mancha 14
Castilla y León 18
Cataluña 149
Comunidad Foral de Navarra 8
Comunidad de Madrid 162
Extremadura 7
Galicia 16
Islas Baleares 15
La Rioja 1
País Vasco 15
Principado de Asturias 10
Región de Murcia 15
Valencia 65
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Alternative pedagogy schools in Spain</title>
<script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
<style type="text/css">
body {
margin: 0;
background-color: lightGray;
font-family: Helvetica, Arial, sans-serif;
}
#container {
width: 700px;
margin-left: auto;
margin-right: auto;
margin-top: 50px;
padding: 50px;
background-color: white;
box-shadow: 3px 3px 5px 6px #ccc;
}
h1 {
font-size: 24px;
margin: 0;
}
p {
font-size: 14px;
margin: 15px 0 10px 0;
}
a:link {
text-decoration: none;
color: gray;
}
a:hover {
text-decoration: underline;
}
a:visited {
color: gray;
}
a:active {
color: steelBlue;
}
svg {
background-color: white;
}
g.bar text {
fill: #333;
font-size: 12px;
font-weight: bold;
text-anchor: end;
opacity: 0;
}
g.bar rect {
opacity: 0.8;
}
g.bar:hover rect {
opacity: 1;
}
g.bar:hover text {
opacity: 1;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 13px;
}
.y.axis path,
.y.axis line {
opacity: 0;
}
</style>
</head>
<body>
<div id="container">
<h1>Madrid and Catalonia, leading Spanish autonomies in educational innovation</h1>
<p>The number of alternative pedagogy schools in Spain has grown exponentially in the last few years, due the large failure rates. <strong>Source:</strong> <a href="http://ludus.org.es/es/stats">Ludus</a>, 2015</p>
</div>
<script type="text/javascript">
var colors = ['#93BBF4', '#C4159F', '#E57518', '#A1BB06', '#C35D07'];
var w = 700;
var h = 600;
var padding = [ 20, 10, 30, 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");
//Now SVG goes into #container instead of body
var svg = d3.select("#container")
.append("svg")
.attr("width", w)
.attr("height", h);
//SHADOW https://github.com/wbzyl/d3-notes/blob/master/hello-drop-shadow.html
var defs = svg.append("defs");
// black drop shadow
var filter = defs.append("filter")
.attr("id", "drop-shadow")
filter.append("feGaussianBlur")
.attr("in", "SourceAlpha")
.attr("stdDeviation", 1)
.attr("result", "blur");
filter.append("feOffset")
.attr("in", "blur")
.attr("dx", 1)
.attr("dy", 1)
.attr("result", "offsetBlur");
var feMerge = filter.append("feMerge");
feMerge.append("feMergeNode")
.attr("in", "offsetBlur")
feMerge.append("feMergeNode")
.attr("in", "SourceGraphic");
d3.csv("autonomies.csv", function(data) {
data.sort(function(a, b) {
return d3.descending(+a.projects, +b.projects);
});
widthScale.domain([ 0, d3.max(data, function(d) {
return +d.projects;
}) ]);
heightScale.domain(data.map(function(d) { return d.autonomy; } ));
//Bind data to groups (not bars directly)
var groups = svg.selectAll("g")
.data(data)
.enter()
.append("g")
.attr("class", "bar");
//Add a rect to each group
var rects = groups.append("rect")
.attr("x", padding[3])
.attr("y", function(d) {
return heightScale(d.autonomy);
})
.attr("width", 0)
.attr("height", heightScale.rangeBand()-3) // -3 adds a little vertical margin
.style("filter", "url(#drop-shadow)")
.attr("stroke", "white")
.attr("stroke-width", 1)
.attr("fill", function(d, i) {
return colors[i % colors.length];
});
//Add a text element to each group
groups.append("text")
.attr("x", function(d) {
return padding[3] + widthScale(d.projects) - 3;
})
.attr("y", function(d) {
return heightScale(d.autonomy) + 18;
})
.text(function(d) {
return d.projects;
});
rects.transition()
.ease("elastic")
.delay(function(d, i) {
return i * 50;
})
.duration(1500)
.attr("width", function(d) {
return widthScale(d.projects);
});
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.