A multi-line chart showing the behaviour of the distribution of the genders of the members of the italian Parliament per legislature using d3.js. Three additional lines show the average values per all legislatures. Chart made thanks to Camera dei Deputati Open Data.
Last active
August 29, 2015 14:11
-
-
Save andreaderrico2/f71438e207b85a427019 to your computer and use it in GitHub Desktop.
Italian Parliament Gender Distribution per legislature
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
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; | |
} |
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> | |
<head> | |
<meta charset="utf-8"> | |
<title>Italian Parliament Gender Distribution per legislature</title> | |
<meta name="description" content="Italian Parliament Gender Distribution 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> |
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
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(["#1f77b4", "#e377c2", "#2ca02c"]); | |
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.number); }); | |
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_male = 0; | |
total_female = 0; | |
total_total = 0; | |
total_average = [total_male,total_female,total_total]; | |
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){ | |
male = 0; | |
female = 0; | |
total = 0; | |
d.deputati.forEach(function(d){ | |
if (d.genere.value == 'male'){ | |
male += 1; | |
total_average[0] += 1; | |
} else { | |
female += 1; | |
total_average[1] += 1; | |
} | |
total +=1; | |
total_average[2] +=1; | |
}); | |
obj = {"male" : male, "female" : female, "total" : total, "date" : [{"date":d.startDate.substring(0,4),"legislature":d.repubblica}]}; | |
data.push(obj); | |
}); | |
color.domain(["male","female","total"]); | |
var genders = color.domain().map(function(name) { | |
return { | |
name: name, | |
values: data.map(function(d) { | |
return {date: d.date[0].date, number: +d[name]}; | |
}) | |
}; | |
}); | |
var genders_total = color.domain().map(function(name,i) { | |
return { | |
name: name, | |
values: data.map(function(d) { | |
return {date: d.date[0].date, number: total_average[i]/17}; | |
}) | |
}; | |
}); | |
x.domain(d3.extent(data, function(d) { return d.date[0].date; })); | |
y.domain([ | |
d3.min(genders, function(c) { return d3.min(c.values, function(v) { return v.number; }); }), | |
d3.max(genders, function(c) { return d3.max(c.values, function(v) { return v.number; }); }) | |
]); | |
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 Gender Distribution per legislature"); | |
var gender = svg.selectAll(".gender") | |
.data(genders) | |
.enter().append("g") | |
.attr("class", "gender"); | |
gender.append("path") | |
.attr("class", "line") | |
.attr("d", function(d) { return line(d.values); }) | |
.style("stroke", function(d) { return color(d.name); }); | |
var gender_total = svg.selectAll(".gender_total") | |
.data(genders_total) | |
.enter().append("g") | |
.attr("class", "gender_total"); | |
gender_total.append("path") | |
.attr("class", "line") | |
.attr("d", function(d) { return line(d.values); }) | |
.style("stroke", function(d) { return color(d.name); }); | |
gender.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.number)+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(male)) | |
.attr("cx", function(d,i) { return x(d.date[0].date); }) | |
.attr("cy", function(d,i) { return y(d.male); }) | |
.attr("class", "male_dot") | |
.on("mouseover", function(d) { | |
div.transition() | |
.duration(200) | |
.style("opacity", 0.9); | |
div.html('<b>Male:</b> ' + d.male + ' <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); | |
}); | |
svg.selectAll(".female_dot") | |
.data(data) | |
.enter().append("circle") | |
.attr("r", 5) | |
.attr("stroke", color(female)) | |
.attr("cx", function(d,i) { return x(d.date[0].date); }) | |
.attr("cy", function(d,i) { return y(d.female); }) | |
.attr("class", "female_dot") | |
.on("mouseover", function(d) { | |
div.transition() | |
.duration(200) | |
.style("opacity", 0.9); | |
div.html('<b>Female:</b> ' + d.female + ' <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); | |
}); | |
svg.selectAll(".total_dot") | |
.data(data) | |
.enter().append("circle") | |
.attr("r", 5) | |
.attr("stroke", color(total)) | |
.attr("cx", function(d,i) { return x(d.date[0].date); }) | |
.attr("cy", function(d,i) { return y(d.total); }) | |
.attr("class", "total_dot") | |
.on("mouseover", function(d) { | |
div.transition() | |
.duration(200) | |
.style("opacity", 0.9); | |
div.html('<b>Total:</b> ' + d.total + ' <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