block by michalskop a0af3e7d548592335c77

CZ: Local elections 2014, 3 parties

Full Screen

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>
	  // 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;
        }
        
        /* D3 tips */  
    .d3-tip {
      line-height: 1;
      font-weight: bold;
      padding: 12px;
      background: rgba(0, 0, 0, 0.8);
      color: #fff;
      border-radius: 2px;
    }
    /* Creates a small triangle extender for the tooltip */
    /*.d3-tip:after {
      box-sizing: border-box;
      display: inline;
      font-size: 10px;
      width: 100%;
      line-height: 1;
      color: rgba(0, 0, 0, 0.8);
      content: "\25BC";
      position: absolute;
      text-align: center;
    }*/
    /* Style northward tooltips differently */
    .d3-tip.n:after {
      margin: -1px 0 0 0;
      top: 100%;
      left: 0;
    }
    .stronger {
      color: yellow;
    }
    /*overwrite bootstrap*/
        .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">


		// Create the map
		var map = L.map('map',{zoomControl: false}).setView([50,15], 7);
		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: 'CC-BY Michal Škop <a href="//kohovolit.eu">KohoVolit.eu</a> | &copy; <a href="//osm.org/copyright">OpenStreetMap</a> contributors'
		}).addTo(map);
		
		//add circles
		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>";
                }
				/*sum = 0;
				$.each(value.description, function (i, v) {
				  tmp = Math.round(parseInt(v.value) / parseInt(value.population) * 1000) / 10;
				  description += v.name + ': ' +  tmp + '% (' + v.value + ' hlasů)<br>';
				  sum += parseInt(v.value)
				});
				tmp = Math.round((parseInt(value.population) - sum) / parseInt(value.population) * 1000) / 10;
				tmp2 = value.population - sum;
				description += "Ostatní: " + tmp + '% (' + tmp2 + ' hlasů)';*/
		        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;
		        }

		  });
		  //alert(j);
		});
		
		
    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>
     /* (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>