Skip to content

Instantly share code, notes, and snippets.

@rekaj3773
Last active April 5, 2019 15:45
Show Gist options
  • Save rekaj3773/54b6cf7a7f8db4e6d77ecc434584ced2 to your computer and use it in GitHub Desktop.
Save rekaj3773/54b6cf7a7f8db4e6d77ecc434584ced2 to your computer and use it in GitHub Desktop.
Turkish Local Election Results 2019
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
.tooltip {
position: absolute;
text-align: center;
width: 300px;
height: 40px;
padding: 2px;
font: 12px sans-serif;
background: lightsteelblue;
border: 0px;
border-radius: 8px;
pointer-events: none;
opacity:0;
}
</style>
</head>
<body>
<script>
// Feel free to change or delete any of the code you see in this editor!
var svg = d3.select("body").append("svg")
.attr("width", 960)
.attr("height", 500)
svg.append("text")
.text("Turkish General Election Results 2018")
.attr("y", 83)
.attr("x", 120)
.attr("font-size", 36)
.attr("font-family", "monospace")
var g = svg.append('g');
var tooltipdiv = d3.select("body").append("div")
.attr("class", "tooltip");
var mercProjection = d3.geoMercator()
.scale(2000)
.rotate([-1, 0])
.center([36.487, 38.961])
;
var geoPath = d3.geoPath().projection(mercProjection);
var featureCount =0;
d3.json("turkeyProvinces_simplified.json",function(error,mapData){
d3.json("turkishGeneralElection2019.json",function(error,electionData){
var provinces = g.selectAll('path')
.data(mapData.features)
.enter()
.append('path')
.attr('fill', function(d) {return electionColor(findMatch(d,electionData))})
.attr('stroke','black')
.attr('d', geoPath)
.on("mouseover", function(d) {
//console.log(electionData[d.properties.NAME_1]);
console.log(findMatch(d, electionData));
tooltipdiv.transition()
.duration(200)
.style("opacity", .9)
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px")
.text(d.properties.NAME_1+" - Winning Party: "+findMatch(d,electionData))
;
//need to find out how to index //tooltipdiv.html(mapData.features[featureCount].properties.NAME_1);
})
.on("mouseout", function(d) {
tooltipdiv.transition()
.duration(500)
.style("opacity", 0);
});
});
});
function findMatch(d,obj)
{
for (var i = 0; i < obj.length; i++){
// look for the entry with a matching `code` value
if (obj[i].HA_SC == d.properties.HASC_1){
return obj[i].WinningParty;
// obj[i].name is the matched result
}
}
}
function electionColor(party){
switch(party) {
case "AKP":
return "yellow";
break;
case "CHP":
return "red";
break;
case "MHP":
return "firebrick";
break;
case "HDP":
return "purple";
break;
case "AKP-CHP-tie":
return "orange";
break;
case "AKP-HDP-tie":
return "brown";
break;
case "AKP-MHP-tie":
return "fuchsia";
break;
case "three-way-tie":
return "gray";
break;
default:
return "steelblue";
}
}
</script>
</body>
[
{"Name": "Adana","WinningParty": "AKP", "HA_SC":"TR.AA"},
{"Name": "Adıyaman","WinningParty": "AKP", "HA_SC":"TR.AD"},
{"Name": "Afyonkarahisar","WinningParty": "AKP", "HA_SC":"TR.AF"},
{"Name": "Ağrı","WinningParty": "HDP", "HA_SC":"TR.AG"},
{"Name": "Aksaray","WinningParty": "AKP", "HA_SC":"TR.AK"},
{"Name": "Amasya","WinningParty": "AKP", "HA_SC":"TR.AM"},
{"Name": "Ankara","WinningParty": "AKP", "HA_SC":"TR.AN"},
{"Name": "Antalya","WinningParty": "AKP", "HA_SC":"TR.AL"},
{"Name": "Ardahan","WinningParty": "AKP", "HA_SC":"TR.AR"},
{"Name": "Artvin","WinningParty": "AKP", "HA_SC":"TR.AV"},
{"Name": "Aydın","WinningParty": "CHP", "HA_SC":"TR.AY"},
{"Name": "Balıkesir","WinningParty": "AKP", "HA_SC":"TR.BK"},
{"Name": "Bartın","WinningParty": "AKP", "HA_SC":"TR.BR"},
{"Name": "Batman","WinningParty": "HDP", "HA_SC":"TR.BM"},
{"Name": "Bayburt","WinningParty": "AKP", "HA_SC":"TR.BB"},
{"Name": "Bilecik","WinningParty": "AKP", "HA_SC":"TR.BC"},
{"Name": "Bingöl","WinningParty": "AKP", "HA_SC":"TR.BG"},
{"Name": "Bitlis","WinningParty": "AKP", "HA_SC":"TR.BT"},
{"Name": "Bolu","WinningParty": "AKP", "HA_SC":"TR.BL"},
{"Name": "Burdur","WinningParty": "AKP", "HA_SC":"TR.BD"},
{"Name": "Bursa","WinningParty": "AKP", "HA_SC":"TR.BU"},
{"Name": "Çanakkale","WinningParty": "AKP", "HA_SC":"TR.CK"},
{"Name": "Çankırı","WinningParty": "AKP", "HA_SC":"TR.CI"},
{"Name": "Çorum","WinningParty": "AKP", "HA_SC":"TR.CM"},
{"Name": "Denizli","WinningParty": "AKP", "HA_SC":"TR.DN"},
{"Name": "Diyarbakır","WinningParty": "HDP", "HA_SC":"TR.DY"},
{"Name": "Düzce","WinningParty": "AKP", "HA_SC":"TR.DU"},
{"Name": "Edirne","WinningParty": "CHP", "HA_SC":"TR.ED"},
{"Name": "Elazığ","WinningParty": "AKP", "HA_SC":"TR.EG"},
{"Name": "Erzincan","WinningParty": "AKP", "HA_SC":"TR.EN"},
{"Name": "Erzurum","WinningParty": "AKP", "HA_SC":"TR.EM"},
{"Name": "Eskişehir","WinningParty": "AKP", "HA_SC":"TR.ES"},
{"Name": "Gaziantep","WinningParty": "AKP", "HA_SC":"TR.GA"},
{"Name": "Giresun","WinningParty": "AKP", "HA_SC":"TR.GI"},
{"Name": "Gümüşhane","WinningParty": "AKP", "HA_SC":"TR.GU"},
{"Name": "Hakkâri","WinningParty": "HDP", "HA_SC":"TR.HK"},
{"Name": "Hatay","WinningParty": "AKP", "HA_SC":"TR.HT"},
{"Name": "Iğdır","WinningParty": "HDP", "HA_SC":"TR.IG"},
{"Name": "Isparta","WinningParty": "AKP", "HA_SC":"TR.IP"},
{"Name": "İstanbul","WinningParty": "AKP", "HA_SC":"TR.IB"},
{"Name": "İzmir","WinningParty": "CHP", "HA_SC":"TR.IZ"},
{"Name": "Kahramanmaraş","WinningParty": "AKP", "HA_SC":"TR.KM"},
{"Name": "Kars","WinningParty": "AKP", "HA_SC":"TR.KA"},
{"Name": "Karabük","WinningParty": "AKP", "HA_SC":"TR.KB"},
{"Name": "Karaman","WinningParty": "AKP", "HA_SC":"TR.KR"},
{"Name": "Kastamonu","WinningParty": "AKP", "HA_SC":"TR.KS"},
{"Name": "Kayseri","WinningParty": "AKP", "HA_SC":"TR.KY"},
{"Name": "Kilis","WinningParty": "AKP", "HA_SC":"TR.KI"},
{"Name": "Kırıkkale","WinningParty": "AKP", "HA_SC":"TR.KK"},
{"Name": "Kırklareli","WinningParty": "CHP", "HA_SC":"TR.KL"},
{"Name": "Kırşehir","WinningParty": "AKP", "HA_SC":"TR.KH"},
{"Name": "Kocaeli","WinningParty": "AKP", "HA_SC":"TR.KC"},
{"Name": "Konya","WinningParty": "AKP", "HA_SC":"TR.KO"},
{"Name": "Kütahya","WinningParty": "AKP", "HA_SC":"TR.KU"},
{"Name": "Malatya","WinningParty": "AKP", "HA_SC":"TR.ML"},
{"Name": "Manisa","WinningParty": "AKP", "HA_SC":"TR.MN"},
{"Name": "Mardin","WinningParty": "HDP", "HA_SC":"TR.MR"},
{"Name": "Mersin","WinningParty": "AKP", "HA_SC":"TR.IC"},
{"Name": "Muğla","WinningParty": "CHP", "HA_SC":"TR.MG"},
{"Name": "Muş","WinningParty": "HDP", "HA_SC":"TR.MS"},
{"Name": "Nevşehir","WinningParty": "AKP", "HA_SC":"TR.NV"},
{"Name": "Niğde","WinningParty": "AKP", "HA_SC":"TR.NG"},
{"Name": "Ordu","WinningParty": "AKP", "HA_SC":"TR.OR"},
{"Name": "Osmaniye","WinningParty": "AKP", "HA_SC":"TR.OS"},
{"Name": "Rize","WinningParty": "AKP", "HA_SC":"TR.RI"},
{"Name": "Sakarya","WinningParty": "AKP", "HA_SC":"TR.SK"},
{"Name": "Samsun","WinningParty": "AKP", "HA_SC":"TR.SS"},
{"Name": "Siirt","WinningParty": "HDP", "HA_SC":"TR.SI"},
{"Name": "Sinop","WinningParty": "AKP", "HA_SC":"TR.SP"},
{"Name": "Sivas","WinningParty": "AKP", "HA_SC":"TR.SV"},
{"Name": "Şanlıurfa","WinningParty": "AKP", "HA_SC":"TR.SU"},
{"Name": "Şırnak","WinningParty": "HDP", "HA_SC":"TR.SR"},
{"Name": "Tekirdağ","WinningParty": "CHP", "HA_SC":"TR.TG"},
{"Name": "Tokat","WinningParty": "AKP", "HA_SC":"TR.TT"},
{"Name": "Trabzon","WinningParty": "AKP", "HA_SC":"TR.TB"},
{"Name": "Tunceli","WinningParty": "HDP", "HA_SC":"TR.TC"},
{"Name": "Uşak","WinningParty": "AKP", "HA_SC":"TR.US"},
{"Name": "Van","WinningParty": "HDP", "HA_SC":"TR.VA"},
{"Name": "Yalova","WinningParty": "AKP", "HA_SC":"TR.YL"},
{"Name": "Yozgat","WinningParty": "AKP", "HA_SC":"TR.YZ"},
{"Name": "Zonguldak","WinningParty": "AKP", "HA_SC":"TR.ZO"}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment