index.html
<!DOCTYPE html>
<html>
<head>
<title>Local elections in Czech Republic 2014</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<script src="//code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="d3.tip.js"></script>
<script>
L_PREFER_CANVAS = true;
</script>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css">
<link rel="stylesheet" href="//cdn.leafletjs.com/leaflet-0.6.4/leaflet.css" />
<script src="//cdn.leafletjs.com/leaflet-0.6.4/leaflet.js"></script>
<style type="text/css">
html, body, #map {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
.leaflet-tile-pane {
opacity: .3
}
.leaflet-container {
background-color: #fff;
}
.d3-tip {
line-height: 1;
font-weight: bold;
padding: 12px;
background: rgba(0, 0, 0, 0.8);
color: #fff;
border-radius: 2px;
}
.d3-tip.n:after {
margin: -1px 0 0 0;
top: 100%;
left: 0;
}
.stronger {
color: yellow;
}
.navbar {
margin-bottom: 0;
}
</style>
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">Local elections in Czech Republic 2014 - First 3 parties</a>
</div>
</div>
</div>
<div style="position:fixed;top:50px;z-index:1000;">
<div class="alert alert-info" >The <strong>size</strong> of bubbles represents number of voters, the <strong>color</strong> represents the 1st, 2nd and 3rd party.</div>
</div> <div style="position:fixed;top:125px;z-index:1000;">
<div class="alert alert-info" style="float:left;">
<svg height="20" width="20"><circle cx="10" cy="10" r="10" fill="#009ee0"></svg> ANO 2011<br/>
<svg height="20" width="20"><circle cx="10" cy="10" r="10" fill="#f54200"></svg> ČSSD<br/>
<svg height="20" width="20"><circle cx="10" cy="10" r="10" fill="#808"></svg> STAN / TOP 09<br/>
<svg height="20" width="20"><circle cx="10" cy="10" r="10" fill="#880000"></svg> KSČM<br/>
<svg height="20" width="20"><circle cx="10" cy="10" r="10" fill="#fedc35"></svg> KDU-ČSL<br>
<svg height="20" width="20"><circle cx="10" cy="10" r="10" fill="#008"></svg> ODS
</div>
</div>
<div id="map" style="margin-top:40px;"></div>
<script type="text/javascript">
var map = L.map('map',{zoomControl: false}).setView([50,15], 7);
map.addControl( L.control.zoom({position: 'topright'}) );
L.tileLayer('//{s}.www.toolserver.org/tiles/bw-mapnik/{z}/{x}/{y}.png', {
attribution: 'CC-BY Michal Škop <a href="//kohovolit.eu">KohoVolit.eu</a> | © <a href="//osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
j = 0;
$.getJSON( "cz2014towns.json", function (data) {
$.each(data, function (index, value) {
description = "<strong>" + value.name + "</strong> (" + value.population + ")<br>";
mx = Math.min(value['votes'].length, 9)
for (i = 0; i < mx; i++) {
description += value['votes'][i]['name'] + ": " + value['votes'][i]['result'] +"%<br>";
}
j++;
positionparam = 0.000000025;
sizeparam = 1000;
r = 0;
population = parseInt(value.population);
if (typeof(value['votes'][0]) !== 'undefined') {
result0 = value['votes'][0]['result']/100;
R0 = 2*Math.sqrt(result0*population*positionparam/3.14);
}
for (i = 2; i >= 0; i--) {
if (typeof(value['votes'][i]) !== 'undefined') {
color = name2color(value['votes'][i]['name']);
result = value['votes'][i]['result']/100;
R = Math.sqrt(result*population*positionparam/3.14);
dx = Math.sin(i*3.14*2/3)*(2*R0-R);
dy = Math.cos(i*3.14*2/3)*(2*R0-R);
size = Math.sqrt(result*sizeparam*population);
circle = L.circle([parseFloat(value.lat)+dy, parseFloat(value.lng)-dx], size, {
color: color,
fillColor: color,
fillOpacity: 0.85,
weight: 1,
}).addTo(map);
circle.bindPopup(description);
}
r = r + result*3.14*2;
}
});
});
function convertToSlug(Text)
{
return Text
.toLowerCase()
.replace(/[^\w ]+/g,'')
.replace(/ +/g,'-')
;
}
function name2color(n) {
if ((n == 'ANO 2011') || (n == 'Politické hnutí ANO 2011')) return '#009ee0';
if ((n == 'TOP 09') || (n == 'STAROSTOVÉ A NEZÁVISLÍ') || (n == 'Starostové pro Liberecký kraj')) return '#808';
if (n == 'Česká strana sociálně demokratická') return '#f54200';
if (n == 'Komunistická strana Čech a Moravy') return '#880000';
if (n == 'Křesťanská a demokratická unie - Československá strana lidová') return '#fedc35';
if ((n == 'PRO Chomutov') || (n == 'Změna pro Liberec') || (n == 'PRO! Ústí') || (n == 'VOLBA PRO! TEPLICE') || (n == 'Strana zelených')) return '#0b0';
if (n == 'Občanská demokratická strana') return '#008';
if (n == 'Česká strana sociálně demokratická') return '#f54200';
if (n == 'Česká pirátská strana') return '#000';
return '#888';
}
</script>
<script>
</script>
</body>
</html>