block by michalskop 9873150

SK: Presidential elections 2014 - 2nd round

Full Screen

index.html

<!DOCTYPE html>
<html>
  <head>
    <title>Slovak presidential election 2014 - 2nd 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 - 2nd 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:#00f">Kiska</span></strong> vs. <strong><span style="color:#f00">Fico</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_2_fico_kiska_short.json", function (data) {
		    $.each(data.votes, function (index, value) {
		    
		        if (value.winner_class == 'kiska') color = "#00f";
		        else color = "#f00";
		        
		        value.population = 0;
		        for (k=0;k<value.votes.length;k++)
		          value.population = value.population+parseFloat(value.votes[k]);
		        
		        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]] + ": " +  tmp + "% (" + tuples[i][1].toLocaleString()  +")<br>";
                }
		    
		        //weight
		        if (value.population > 20000) weight = 2;
		        else if (value.population > 3000) weight = 1;
		        else weight = 0.5;
				circle = L.circle([value.lat, value.lng], Math.sqrt(parseInt(value.population)*2000), {
					color: color,
					fillColor: color,
					fillOpacity: 
					  2.86*(Math.max(parseInt(value.votes[0]),parseInt(value.votes[1])) / value.population - 0.5),
					weight: weight,
				}).addTo(map);
				

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

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

index6.html

index6ba.html


    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

	<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="//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>

  
    <div id="map"></div>
    <script type="text/javascript">

        //colors:
        var amax = Array(255,0,0);
		var bmax = Array(0,0,255);
		
		// Create the map
		var map = L.map('map',{zoomControl: false}).setView([48.123018,17.065659], 11);
		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_2_fico_kiska_ba_short.json", function (data) {
		    $.each(data.votes, function (index, value) {
		        if (value.winner_class == 'kiska') color = "#00f";
		        else color = "#f00";
		        
		        value.population = 0;
		        for (k=0;k<value.votes.length;k++)
		          value.population = value.population+parseFloat(value.votes[k]);
		          
		        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]] + ": " +  tmp + "% (" + tuples[i][1].toLocaleString()  +")<br>";
                }
                
		        //weight
		        if (value.population > 2000) weight = 2;
		        else if (value.population > 300) weight = 1;
		        else weight = 0.5;
		    
				circle = L.circle([value.lat, value.lng], Math.sqrt(parseInt(value.population)*150), {
					color: data.colors[value.winner_class],
					fillColor: values2color(parseInt(value.votes[0]),parseInt(value.votes[1])),
					fillOpacity: 0.8,
					weight: weight,
				}).addTo(map);
				

				circle.bindPopup(description);
		  });
		});
		
		function values2color(a,b) {
		  //amax = Array(127,0,0);
		  //bmax = Array(0,127,0);
		  color = Array();
		  if (a >= b)
		    return val2col(1-(2*b/(a+b)),amax);
		  else
		    return val2col(1-(2*a/(a+b)),bmax);
		}
		
		// color scale over white
		//r - rate [0,1], c ~ max. color Array(127,0,0)
		//0 ~ white, 1 ~ full color
		function val2col(r,c) {
		  white = [255,255,255];
		  col = Array();
		  for (i=0;i<=2;i++) {
		     col[i] = Math.round(white[i] + r * (c[i] - white[i]));
		  }
		  return "rgb(" + col[0] + "," + col[1] + "," + col[2] + ")";
		}		

	</script>

index6fico.html

index6ke.html

index6kiska.html

index7.html

index8.html

index9.html

sk_president_2014_2_fico_kiska_ba_short.json

sk_president_2014_2_fico_kiska_ke_short.json