xxxxxxxxxx
<html lang="en">
<head>
<meta charset="utf-8">
<title>Showing the non contributive pensions</title>
<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;
}
p.aclariment{
font-size: 8px;
font-family: Courier, "Lucida Console", monospace;
}
svg {
background-color: white;
}
rect:hover {
fill: orange;
}
/*.descripcio{
font-family: Courier, "Lucida Console", monospace;
font-size: 6px;
}
*/
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 8px;
}
.y.axis text{
font-size: 8px;
}
.y.axis path,
.y.axis line {
opacity: 0;
}
</style>
<script tpye="text/javascript" src="https://d3js.org/d3.v3.js"></script>
</head>
<body>
<h1>Distribució de les pensions no contributives a les comarques de Catalunya</h1>
<p>Proporció de “pensions no contributives” cada mil habitants de més de 65 anys a la comarca.</p>
<p> Font: Elaboració pròpia en base a <a href="https://www.idescat.cat/pub/?id=aec&n=254">Idescat</a> i <a href="https://benestar.gencat.cat/ca/serveis/ajuts_i_prestacions_economiques/pensions_no_contributives/">DOG</a></p>
<script type="text/javascript">
var w = 700;
var h = 600;
var padding = [20, 10, 20, 100];
//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")
.ticks(4);
var yAxis = d3.svg.axis()
.scale(heightScale)
.orient("left");
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
d3.csv("noncontrib2.csv", function(dataset) {
//console.log(data);
//Here we create a new variable within the dataset
dataset.forEach(function(d) {d.noncp65 = d.Njubilacio/(d.Poblacio*d.Poblacio65/100);});
//Here we sort the data
dataset.sort(function(a,b) {return d3.descending(+a.noncp65, +b.noncp65);});
//Here we sort the data with an alternative function
//dataset.sort(function(a,b) {return b.Njubilacio - a.Njubilacio;});
widthScale.domain([0, d3.max(dataset, function(d){
return +d.noncp65*1000;
})]);
//heightScale.domain(d3.range(dataset.length));
heightScale.domain(dataset.map(function (d){
return d.Comarca;
}));
var rects = svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect");
rects.attr("x", padding[3])
.attr("y", function(d,i) {
return heightScale(d.Comarca);
})
.attr("width", function(d) {
//return d.Njubilacio/(d.Poblacio65/100*d.Poblacio)*10000;
return widthScale(d.noncp65*1000);
//return d.Njubilacio/10
})
.attr("height", heightScale.rangeBand())
.attr("fill", function(d) {return "rgb(0, 0, " + (d.Njubilacio) + ")";})
.append("title")
//.style("font-size", "6px")
.text(function(d) {
//return d.Comarca + "'s number of non contrib pensionist is " + d.Njubilacio;
return d.Comarca + "'s ratio of non contrib pensionist per 1000 hab over 65 year is " + Math.round(d.noncp65*10000)/10 + " while the absolute number of non contrib pensionist is " + d.Njubilacio;
});
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>
<p class="aclariment"> Els colors més clars indiquen un número absolut de pensions contributives més alt </p>
</body>
</html>
Modified http://d3js.org/d3.v3.js to a secure url
https://d3js.org/d3.v3.js