A basic bar chart showing the numbers of Members of Parliament per italian regions in D3 with the Camera dei Deputati Open Data.
Last active
August 29, 2015 14:11
-
-
Save andreaderrico2/f43d268335f35e056007 to your computer and use it in GitHub Desktop.
Members of Italian Parliament per region
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
.chart { | |
background-color:white | |
} | |
.bar { | |
fill: brown; | |
} | |
.bar:hover { | |
opacity:0.7; | |
} | |
.axis text { | |
font: 9px sans-serif; | |
} | |
.x.axis text { | |
-webkit-transform: rotate(-45deg); | |
} | |
.axis path, .axis line { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
.x.axis path { | |
display: none; | |
} | |
.d3-tip { | |
line-height: 1; | |
font-weight: bold; | |
padding: 5px; | |
color: black; | |
border-radius: 2px; | |
} | |
/* Creates a small triangle extender for the tooltip */ | |
.d3-tip:after { | |
box-sizing: border-box; | |
display: inline; | |
font-size: 10px; | |
width: 100%; | |
line-height: 1; | |
color: rgba(0, 0, 0, 0.8); | |
content: "\25BC"; | |
position: absolute; | |
text-align: center; | |
} | |
/* Style northward tooltips differently */ | |
.d3-tip.n:after { | |
margin: -1px 0 0 0; | |
top: 100%; | |
left: 0; | |
} |
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>Members of Italian Parliament per region</title> | |
<meta name="description" content="Members of Italian Parliament per region" /> | |
<link type="text/css" href="index.css" rel="stylesheet"/> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script> | |
</head> | |
<body> | |
<svg class='chart'></svg> | |
<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 data = [{name:"VALLE D'AOSTA",value:0}, {name:"PIEMONTE",value:0}, {name:"LOMBARDIA",value:0}, {name:"TRENTINO ALTO-ADIGE",value:0}, {name:"FRIULI-VENEZIA-GIULIA",value:0}, {name:"VENETO",value:0}, {name:"EMILIA-ROMAGNA",value:0}, {name:"LIGURIA",value:0}, {name:"TOSCANA",value:0}, {name:"MARCHE",value:0}, {name:"UMBRIA",value:0}, {name:"LAZIO",value:0}, {name:"CAMPANIA",value:0}, {name:"ABRUZZO",value:0}, {name:"BASILICATA",value:0}, {name:"PUGLIA",value:0}, {name:"MOLISE",value:0}, {name:"CALABRIA",value:0}, {name:"SICILIA",value:0}, {name:"SARDEGNA",value:0}, {name:"FOREIGNED",value:0}]; | |
d3.json("http://wafi.iit.cnr.it/webvis/tmp/dati_camera/basic_anagrafica.json", function(error, json) { | |
if (error) return console.warn(error); | |
json.results.deputati.forEach( function(d){ | |
if (d.regione) { | |
for (var i = 0; i < data.length; i++) { | |
if (data[i].name == d.regione.value) { | |
//console.log((regions[d.regione.value]).VALUE); | |
data[i].value+=1; | |
} | |
} | |
} | |
//console.log(d.nazione); | |
if(d.nazione) { | |
data[data.length-1].value+=1; | |
} | |
//regions[d.regione.value]+=1; | |
}); | |
data = data.sort(function(a, b){ | |
return a.value-b.value; | |
}); | |
console.log(data); | |
var margin = {top: 20, right: 30, bottom: 30, left: 40}, | |
width = 960 - margin.left - margin.right, | |
height = 500 - margin.top - margin.bottom; | |
var x = d3.scale.ordinal() | |
.rangeRoundBands([0, width], 0.2); | |
var y = d3.scale.linear() | |
.range([height, 60]); | |
var xAxis = d3.svg.axis() | |
.scale(x) | |
.orient("bottom"); | |
var yAxis = d3.svg.axis() | |
.scale(y) | |
.orient("left"); | |
var tip = d3.tip() | |
.attr('class', 'd3-tip') | |
.offset([-10, 0]) | |
.html(function(d) { | |
return d.value; | |
}) ; | |
var chart = d3.select(".chart") | |
.attr("width", width + margin.left + margin.right) | |
.attr("height", height + margin.top + margin.bottom) | |
.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
chart.call(tip); | |
x.domain(data.map(function(d) { return d.name.split('-')[0]; })); | |
y.domain([0, d3.max(data, function(d) { return d.value; })]); | |
chart.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + (height-10) + ")") | |
.call(xAxis); | |
chart.append("g") | |
.attr("class", "y axis") | |
.attr("transform", "translate(0,-30)") | |
.call(yAxis); | |
chart.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("Members of Italian Parliament per region"); | |
var bar = chart.selectAll(".bar") | |
.data(data) | |
.enter().append("rect") | |
.attr("class", "bar") | |
.attr("x", function(d) { return x(d.name.split('-')[0]); }) | |
.attr("y", function(d) { return y(d.value)-30; }) | |
.attr("height", function(d) { return height - y(d.value); }) | |
.attr("width", x.rangeBand()) | |
.on('mouseover', tip.show) | |
.on('mouseout', tip.hide); | |
function type(d) { | |
d.value = +d.value; // coerce to number | |
return d; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment