Skip to content

Instantly share code, notes, and snippets.

@iriberri
Created October 16, 2015 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iriberri/1da211dd0a195958a53a to your computer and use it in GitHub Desktop.
Save iriberri/1da211dd0a195958a53a to your computer and use it in GitHub Desktop.
Basic filtering through SQL with createVis
<!DOCTYPE html>
<html>
<head>
<title>Basemap example | 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" />
<style>
html, body, #map {
height: 90%;
padding: 0;
margin: 0;
}
</style>
<link rel="stylesheet" href="http://libs.cartocdn.com/cartodb.js/v3/3.15/themes/css/cartodb.css" />
</head>
<body>
<div id="map"></div>
<button type="button" id="button-basemap1" onclick="setButton1();"> Button 1 (Spain)</button>
<button type="button" id="button-basemap2" onclick="setButton2();"> Button 2 (Area > 5000)</button>
<!-- include cartodb.js library -->
<script src="http://libs.cartocdn.com/cartodb.js/v3/3.15/cartodb.js"></script>
<script>
var dataLayer;
function setButton1(){
//filtering by country name
dataLayer.setSQL("select * from european_countries_e where name = 'Spain'");
//changing the css too
dataLayer.setCartoCSS("#european_countries_e{ polygon-fill: #CCC; polygon-opacity: 0.8; line-color: #FFF; line-width: 1; line-opacity: 0.5; } ");
}
function setButton2(){
//filtering by area
dataLayer.setSQL("select * from european_countries_e where area > 5000");
//changing the css, not mandatory!
dataLayer.setCartoCSS("#european_countries_e{ polygon-fill: #FFFFB2; polygon-opacity: 0.8; line-color: #FFF; line-width: 1; line-opacity: 0.5; } #european_countries_e [ area <= 1638094] { polygon-fill: #B10026; } #european_countries_e [ area <= 55010] { polygon-fill: #E31A1C; } #european_countries_e [ area <= 34895] { polygon-fill: #FC4E2A; } #european_countries_e [ area <= 12890] { polygon-fill: #FD8D3C; } #european_countries_e [ area <= 10025] { polygon-fill: #FEB24C; } #european_countries_e [ area <= 9150] { polygon-fill: #FED976; } #european_countries_e [ area <= 5592] { polygon-fill: #FFFFB2; } ");
}
function main() {
// Change your viz.json below
cartodb.createVis('map', 'http://documentation.cartodb.com/api/v2/viz/2b13c956-e7c1-11e2-806b-5404a6a683d5/viz.json')
.done(function(vis, layers) {
//getting the data layer
dataLayer = layers[1].getSubLayer(0);
//Apply a query into that layer
dataLayer.setSQL("select * from european_countries_e limit 5")
//Apply a CartoCSS for that layer
dataLayer.setCartoCSS("#european_countries_e{ polygon-fill: #000; polygon-opacity: 0.8; line-color: #FFF; line-width: 1; line-opacity: 0.5; } ");
})
.error(function(err) {
console.log(err);
});
}
window.onload = main;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment