Last active
April 29, 2016 15:55
-
-
Save ramiroaznar/af14dfec00984feb001a325b4a18708d to your computer and use it in GitHub Desktop.
How to make a choropleth map with CartoDB.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>How to make a choropleth map with CartoDB.js</title> | |
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> | |
<meta http-equiv="content-type" content="text/html; charset=UTF-8" /> | |
<link rel="shortcut icon" href="http://cartodb.com/assets/favicon.ico" /> | |
<link rel="stylesheet" href="http://libs.cartocdn.com/cartodb.js/v3/3.15/themes/css/cartodb.css" /> | |
<script src="http://libs.cartocdn.com/cartodb.js/v3/3.15/cartodb.js"></script> | |
<style> | |
html, body,#map { | |
width:100%; | |
height:100%; | |
padding: 0; | |
margin: 0; | |
} | |
</style> | |
</head> | |
<body> | |
<div id='map'></div> | |
<script type="cartocss/text" id="choropleth"> | |
/** choropleth visualization */ | |
#world_borders{ | |
polygon-fill: #FFFFB2; | |
polygon-opacity: 0.8; | |
line-color: #FFF; | |
line-width: 0.5; | |
line-opacity: 1; | |
} | |
#world_borders [ pop2005 <= 1312978855] { | |
polygon-fill: #B10026; | |
} | |
#world_borders [ pop2005 <= 32270507] { | |
polygon-fill: #E31A1C; | |
} | |
#world_borders [ pop2005 <= 10528226] { | |
polygon-fill: #FC4E2A; | |
} | |
#world_borders [ pop2005 <= 5416945] { | |
polygon-fill: #FD8D3C; | |
} | |
#world_borders [ pop2005 <= 2253501] { | |
polygon-fill: #FEB24C; | |
} | |
#world_borders [ pop2005 <= 456613] { | |
polygon-fill: #FED976; | |
} | |
#world_borders [ pop2005 <= 67827] { | |
polygon-fill: #FFFFB2; | |
} | |
</script> | |
<script type="text/javascript"> | |
function main() { | |
var style = $('#choropleth').text(); // get style | |
console.log(style); | |
var map = new L.Map('map', { | |
zoomControl: false, | |
center: [43, -30], | |
zoom: 3 | |
}); | |
cartodb.createLayer(map, { | |
user_name: 'ramirocartodb', | |
type: 'cartodb', | |
sublayers: [ | |
{ | |
type: "http", // add basemap without labels | |
urlTemplate: "http://{s}.basemaps.cartocdn.com/dark_nolabels/{z}/{x}/{y}.png", | |
subdomains: [ "a", "b", "c" ] | |
}, | |
{ | |
type: "mapnik", // add layer | |
sql: "SELECT * FROM world_borders", | |
cartocss: style | |
} | |
] | |
}) | |
.addTo(map); | |
} | |
window.onload = main; | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment