block by michalskop 9598334

SK: Presidential elections 2014 - 1st round Fico vs. Kiska

Full Screen

index.html

<!DOCTYPE html>
<html>
  <head>
    <title>Slovak presidential election 2014 - 1st round</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>
	  // see //leafletjs.com/reference.html
	  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" />
	 <!--[if lte IE 8]>
		 <link rel="stylesheet" href="//cdn.leafletjs.com/leaflet-0.6.4/leaflet.ie.css" />
	 <![endif]-->
    <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;
        }

    </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="#">Slovak presidential election 2014 - 1st round</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 winner and <strong>opacity</strong> margin of victory. <strong><span style="color:#f00">Fico</span></strong> vs. <strong><span style="color:#B700FF">Kiska</span></strong></div>
    </div>

    <div id="map" style="margin-top:40px;"></div>
    <script type="text/javascript">


		// Create the map
		var map = L.map('map',{zoomControl: false}).setView([48.695493,19.467773], 8);
		map.addControl( L.control.zoom({position: 'topright'}) );
		
		// add an OpenStreetMap tile layer
		// also see //wiki.openstreetmap.org/wiki/Tiles
		//L.tileLayer('//{s}.tile.osm.org/{z}/{x}/{y}.png', {
		L.tileLayer('//{s}.www.toolserver.org/tiles/bw-mapnik/{z}/{x}/{y}.png', {
			attribution: '&copy; <a href="//osm.org/copyright">OpenStreetMap</a> contributors'
		}).addTo(map);
		
		//add circles
		$.getJSON( "sk_president_2014_1_fico_kiska.json", function (data) {
		    $.each(data.votes, function (index, value) {
		        
		        description = "<strong>" + value.town + "</strong><br>";
		        
		        tuples = [];
		        for (var key in value.votes) {
		          tuples.push([key,parseInt(value.votes[key])]);
		        }
		        
		        tuples.sort(function(a, b) {
                    a = a[1];
                    b = b[1];
                    return a < b ? 1 : (a > b ? -1 : 0);
                });
                for (i = 0; i <= 1; i++) {
                  //tmp = Math.round(parseInt(tuples[i][1]) / parseInt(value.population) * 100);
                  description += data.people[tuples[i][0]] + ": " +  tuples[i][1]  +"<br>";
                }
		    
		    
				circle = L.circle([value.lat, value.lng], Math.sqrt(parseInt(value.population)*2000), {
					color: data.colors[value.winner_class],
					fillColor: data.colors[value.winner_class],
					fillOpacity: 
					  4*(Math.max(parseInt(value.votes[0]),parseInt(value.votes[1])) / value.population - 0.5),
					weight: 0.1,
				}).addTo(map);
				

				circle.bindPopup(description);
		  });
		});

	</script>
    <script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

      ga('create', 'UA-8592359-13', 'ocks.org');
      ga('send', 'pageview');

    </script> 
  </body>
</html>

data2json2.py

# -*- coding: utf-8 -*-

# Fico, Kiska
import csv
import json
import unicodedata

i = 0
people = []
votes = []
winners = {}
double = [3,7]
with open('obce3.1.csv','r') as f:
  csvreader = csv.reader(f,delimiter=",")
  for row in csvreader:
    if i == 0:
      for j in double:
        people.append(row[j+6])
        winners[row[j+6]] = 0
    else:
      r = {}
      r['id'] = row[0]
      r['town'] = row[3]
      r['lat'] = row[4]
      r['lng'] = row[5]
      r['votes'] = []
      maxi = 0
      sumi = 0
      for j in double:
        r['votes'].append(row[j+6])
        sumi = sumi + int(row[j+6])
        if (int(row[j+6])) > maxi:
          if (j == double[0]):
            winner = people[0]
          else:
            winner = people[1]
        maxi = int(row[j+6])
      r['winner'] = winner
      r['winner_class'] = unicodedata.normalize('NFKD', winner.decode('utf8')).encode('ASCII', 'ignore').lower()
      r['population'] = sumi
      winners[winner] = winners[winner] + 1  
      votes.append(r)
#      print winners

#      print people
#      print r
#      raise(Exception,'dipy')  

    i = i + 1
#Bárdos	Behýl	Čarnogurský	Fico	Fischer	Hrušovský	Jurišta	Kiska	Kňažko	Martinčko	Melník	Mezenská	Procházka	Šimko
colors = {'fico':'#f00','kiska':'#B700FF'}
print winners #F39C2D
data = {'people':people,'votes':votes,'colors':colors}
with open('sk_president_2014_1_fico_kiska.json', 'w') as outfile:
  json.dump(data, outfile)
outfile.close()