Skip to content

Instantly share code, notes, and snippets.

@cavedave
Last active June 28, 2016 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cavedave/f043996ec398267c7a558a95c4dc2601 to your computer and use it in GitHub Desktop.
Save cavedave/f043996ec398267c7a558a95c4dc2601 to your computer and use it in GitHub Desktop.
Hexbin Map of Irish Election Constituencies
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>svg { position: relative; left: 315px; }</style>
</head>
<body>
<div id="frame"></div>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="uk.electionmap.d3.min.js"></script>
<script src="results.js"></script>
<script>
var UK_VOTE_NUM_COLOURS = {
'Independant': '#DDDDDD'
, 'Fine Gael': '#6699FF'
,'Fianna Fáil': '#66BB66'
, 'Sinn Féin': '#008800'
, 'Labour Party': '#CC0000'
};
var svg = d3.select('#frame').append('svg')
.attr('width', 330)
.attr('height', 430);
var map = UK.ElectionMap()
.fill(function (constituency) { return UK_VOTE_NUM_COLOURS[UK_REFERENDUM_PETITION[constituency]] || 'white'; })
.origin({x: 55, y: 395});
map(svg);
</script>
</body>
</html>
//code from http://bl.ocks.org/cavedave/d6712b0b6127d8cba77e3afee5db7d06
//data from https://petition.parliament.uk/petitions/131215.json
var UK_REFERENDUM_PETITION = {"Kerry1": "Independant",
"Kerry2": "Independant",
"Kerry3": "Fine Gael",
"Kerry4": "Fianna Fáil",
"Kerry5": "Sinn Féin",
"Cork South-West1":"Fianna Fáil",
"Cork South-West2":"Fine Gael",
"Cork South-West3":"Independant",
"Cork North-West1":"Fianna Fáil",
"Cork North-West2":"Fianna Fáil",
"Cork North-West3":"Fine Gael",
"Cork East1":"Fine Gael",
"Cork East2":"Fianna Fáil",
"Cork East3":"Sinn Féin",
"Cork East4":"Fine Gael",
};
if("undefined"===typeof UK)var UK={};
UK.ElectionMap=function(){function b(a){var b=Math.sin(angle)*d,h=Math.cos(angle)*d;"undefined"===typeof origin&&(origin={x:4*d+Math.ceil(5*h),y:a.attr("height")-Math.ceil(2*b)});return a.selectAll(".constituency").data(UK.ElectionMap.CONSTITUENCIES).enter().append("path").attr("class","constituency").attr("stroke",e).attr("fill",fill).attr("d",function(a){var f=origin.x+a[0]*(d+h),c=origin.y-a[1]*b,g=f+d,e=c+b,k=e+b;a=[f,c];f=[[g,c],[g+h,e],[g,k],[f,k],[f-h,e]];c=[];c.push("M"+a[0]+" "+a[1]);for(g=
0;a=f[g];g++)c.push("L"+a[0]+" "+a[1]);c.push("z");return c.join("")})}var d=15,e="#AAAAAA";fill="none";origin=void 0;angle=60*(Math.PI/180);b.edgeLength=function(a){if(!arguments.length)return d;d=a;return b};b.origin=function(a){if(!arguments.length)return origin;origin=a;return b};b.stroke=function(a){if(!arguments.length)return e;e=a;return b};b.fill=function(a){fill=function(b){return a(b[2])};return b};return b};
UK.ElectionMap.CONSTITUENCIES=[
[0,0,"Kerry1"],
[1,1,"Kerry3"],
[1,5,"Kerry3"],
[1,3,"Kerry4"],
[0,4,"Kerry2"],
[2,0,"Cork South-West1"],
[3,1,"Cork South-West2"],
[4,0,"Cork South-West3"],
[2,2,"Cork North-West1"],
[3,3,"Cork North-West2"],
[2,4,"Cork North-West3"],
[4,4,"Cork East1"],
[5,3,"Cork East2"],
[4,2,"Cork East3"],
[5,1,"Cork East3"],
]
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment