Skip to content

Instantly share code, notes, and snippets.

@andreaderrico2
Last active August 29, 2015 14:11
Show Gist options
  • Save andreaderrico2/a5eb804eba5f4b8743e2 to your computer and use it in GitHub Desktop.
Save andreaderrico2/a5eb804eba5f4b8743e2 to your computer and use it in GitHub Desktop.
Italian Parliament Average Age per legislature

A line chart showing the behaviour of the average age of the members of the italian Parliament per legislature using d3.js. An additional line shows the total average age per all legislatures. Chart made thanks to Camera dei Deputati Open Data.

body {
background-color:white;
font-family: Helvetica;
font-weight: lighter;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
circle {
fill: white;
stroke-width: 3;
}
.tooltip {
background: white;
border: 1px solid black;
border-radius: 8px;
color: black;
font: 12px sans-serif;
padding: 2px;
pointer-events: none;
position: absolute;
text-align: center;
vertical-align: middle;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Italian Parliament Average Age per legislature</title>
<meta name="description" content="Italian Parliament Average Age per legislature" />
<link type="text/css" href="index.css" rel="stylesheet"/>
<script src="http://d3js.org/d3.v3.min.js"></script>
</head>
<body>
<script src="index.js"></script>
</body>
</html>
var margin = {top: 20, right: 80, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var parseDate = d3.time.format("%Y").parse;
var x = d3.time.scale()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var color = d3.scale.ordinal()
.range(["#e377c2", "#2ca02c", "#1f77b4"]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickFormat(d3.format(".0f"));
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var line = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.age); });
var div = d3.select('body').append("div") // declare the properties for the div used for the tooltips
.attr("class", "tooltip") // apply the 'tooltip' class
.style("opacity", 0);
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
data = [];
total_deputati = 0;
total_age = 0;
d3.json("http://wafi.iit.cnr.it/webvis/tmp/dati_camera/members_for_republic.json", function(error, json) {
if (error) return console.warn(error);
json.results.bindings.forEach(function(d){
year_legislature = +d.startDate.substring(0,4);
//console.log(year_legislature);
deputati = d.deputati.length;
total_deputati += deputati;
//console.log(total_deputati);
age = 0;
d.deputati.forEach(function(f){
if (f.dataNascita){
data_nascita = f.dataNascita.value.substring(0,4);
single_age = year_legislature-data_nascita;
age += single_age;
}
});
total_age += age/deputati;
obj = {"age" : age/deputati, "date" : [{"date":d.startDate.substring(0,4),"legislature":d.repubblica}]};
data.push(obj);
});
color.domain(["age"]);
var ages = color.domain().map(function(name) {
return {
name: name,
values: data.map(function(d) {
return {date: d.date[0].date, age: +d[name]};
})
};
});
var ages_total = color.domain().map(function(name) {
return {
name: name,
values: data.map(function(d) {
return {date: d.date[0].date, age: total_age/17};
})
};
});
x.domain(d3.extent(data, function(d) { return d.date[0].date; }));
y.domain([
d3.min(ages, function(c) { return d3.min(c.values, function(v) { return v.age; }); }),
d3.max(ages, function(c) { return d3.max(c.values, function(v) { return v.age; }); })
]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("N°");
svg.append("text")
.attr("x", (width / 2))
.attr("y", 0 - (margin.top / 4))
.attr("text-anchor", "middle")
.style("font-size", "16px")
.style("text-decoration", "underline")
.text("Italian Parliament Average Age per legislature");
var age = svg.selectAll(".gender")
.data(ages)
.enter().append("g")
.attr("class", "gender");
age.append("path")
.attr("class", "line")
.attr("d", function(d) { return line(d.values); })
.style("stroke", function(d) { return color(age); });
var age_total = svg.selectAll(".gender_total")
.data(ages_total)
.enter().append("g")
.attr("class", "gender_total");
age_total.append("path")
.attr("class", "line")
.attr("d", function(d) { return line(d.values); })
.style("stroke", function(d) { return color(age); });
age.append("text")
.datum(function(d) { return {name: d.name, value: d.values[d.values.length - 1]}; })
.attr("transform", function(d) { return "translate(" + x(d.value.date) + "," + (y(d.value.age)+10) + ")"; })
.attr("x", 3)
.attr("dy", ".35em")
.text(function(d) { return d.name; });
svg.selectAll(".male_dot")
.data(data)
.enter().append("circle")
.attr("r", 5)
.attr("stroke", color(age))
.attr("cx", function(d,i) { return x(d.date[0].date); })
.attr("cy", function(d,i) { return y(d.age); })
.attr("class", "male_dot")
.on("mouseover", function(d) {
div.transition()
.duration(200)
.style("opacity", 0.9);
div.html('<b>Average Age:</b> ' + d3.format(".2f")(d.age) + ' <br><b>Year:</b> ' + d.date[0].date + ' <br><b>Legislature:</b> ' + d.date[0].legislature)
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY + 5) + "px");
})
.on("mouseout", function(d) {
div.transition()
.duration(500)
.style("opacity", 0);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment