block by michalskop 8278143

CZ president. elections '13: Each Polling Station (color saturation)

Full Screen

index.html

<!DOCTYPE html>
<html>
  <head>
    <title>Czech presidential elections 2013, 2nd round: Zeman vs. Schwarzenberg</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>
    <!--<script type="text/javascript" src="//mbostock.github.com/d3/d3.js?1.29.1"></script>-->
    <style type="text/css">

		html, body, #map {
		  width: 100%;
		  height: 100%;
		  margin: 0;
		  padding: 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="#">Czech presidential elections 2013, 2nd round: Zeman vs. Schwarzenberg</a>
        </div>
      </div>
    </div>
    
    <div style="position:fixed;top:50px;z-index:1000;">
      <div class="alert alert-info" >
      Look inside cities and towns! Each bubble represents one polling station.<br/>
      The <strong>size</strong> of bubbles represents number of voters, the <strong>color</strong> represents the winner and the <strong>color saturation</strong> the margin of victory. <span style="color:#b00">Zeman</span> vs. <span style="color:#00b">Schwarzenberg</span><br/>
      <strong><em>There are still some missing data that are shown only aggregated, notably Ústí n.L., part of Brno, Praha 1 and 3, Plzeň 1, Opava.</em></strong>
      </div>

    </div>
    <div id="map" style="margin-top:40px;"></div>
    <script type="text/javascript">
    
        //colors:
        var amax = Array(127,0,0);
		var bmax = Array(0,0,127);


		// Create the map
		var map = L.map('map',{zoomControl: false}).setView([50,15], 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( "cz_president_2013_both_2_ring.json", function (data) {
		$.getJSON( "cz_president_2013_okrsky_2_ring.json", function (data) {
		    $.each(data, function (index, value) {
				circle = L.circle([value.coordinates[1], value.coordinates[0]], Math.sqrt((parseInt(value.population.p6)+parseInt(value.population.p9))*125), {
					color: values2color(parseInt(value.population.p6),parseInt(value.population.p9)),
					fillColor: values2color(parseInt(value.population.p6),parseInt(value.population.p9)),
					//fillOpacity: 2*(Math.max(parseInt(value.population.p6),parseInt(value.population.p9)) / (parseInt(value.population.p6)+parseInt(value.population.p9))-0.5),
					fillOpacity: .8,
					weight: 0.1,
					//className: value.classname,	//this does not seem to work, so hacked by class2color
				}).addTo(map);
				
				perc1 = Math.round(Math.max(parseInt(value.population.p6),parseInt(value.population.p9)) / (parseInt(value.population.p6)+parseInt(value.population.p9)) * 100);
				perc2 = 100 - perc1;
				
				circle.bindPopup(value.name + "<br>" + value.winner + " vyhrál " + perc1 + " % vs. " + perc2 + " % <br>(" +Math.max(value.population.p6,value.population.p9) + " : " + Math.min(value.population.p6,value.population.p9) + " hlasů)");
		  });
		});
		
		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) {
		  maxover = 255 - Math.max.apply(Math, c);
		  maxcol = Array();
		  col = Array();
		  for (i=0;i<=2;i++) {
		     maxcol[i] = 255 + c[i]
		  }
		  for (i=0;i<=2;i++) {
		    col[i] = Math.round(Math.min(255,maxcol[i] * (1 - r)))
		  }
		  return "rgb(" + col[0] + "," + col[1] + "," + col[2] + ")";
		}		
	</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>