Last active
October 28, 2015 11:48
-
-
Save oriolbx/8525229e34263adcaeaf to your computer and use it in GitHub Desktop.
CartoDB classification methods
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> | |
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> | |
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/> | |
<style>html, body, #map { height: 100%; padding: 0; margin: 0 }</style> | |
<link rel="stylesheet" href="http://libs.cartocdn.com/cartodb.js/v3/3.15/themes/css/cartodb.css" /> | |
<!-- include cartodb.js library --> | |
<script src="http://libs.cartocdn.com/cartodb.js/v3/3.15/cartodb.js"></script> | |
<style> | |
html, body, #map { | |
height: 100%; | |
padding: 0; | |
margin: 0; | |
} | |
#selector_menu{ | |
position: absolute; | |
top: 20px; | |
left: 20px; | |
z-index: 9000; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="map"></div> | |
<div id='selector_menu'> | |
<select id='selector'> | |
<option value='no'>Without classification</option> | |
<option value='qua'>Quantile classification</option> | |
<option value='jen'>Jenks classification</option> | |
<option value ='htc'>Heads and Tails classification</option> | |
</select> | |
</div> | |
<script type="text/javascript"> | |
var layer; | |
var vector = []; | |
var legend; | |
function main() { | |
var map = L.map('map', { | |
zoomControl: false, | |
center: [41.390205, 2.154007], | |
zoom: 12 | |
}); | |
// add basemap | |
L.tileLayer('http://{s}.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}.png', {attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, © <a href="http://cartodb.com/attributions">CartoDB</a>'}).addTo(map); | |
cartodb.createLayer(map, { | |
user_name: 'oboix', | |
type: 'cartodb', | |
sublayers: [{ | |
sql: "select * FROM barris_barcelona_test;", | |
cartocss: "#barris_barcelona_test{ polygon-fill: #FF6600;polygon-opacity: 1;line-color: #FFF;line-width: 0.5;line-opacity: 1; }", | |
}] | |
}).addTo(map) | |
.done(function(layer){ | |
// start SQL API | |
var sql = new cartodb.SQL({ user: 'oboix'}); | |
var LayerActions = { | |
no: function(){ | |
sql.execute("SELECT * FROM barris_barcelona_test") | |
.done(function(data){ | |
layer.setCartoCSS("#barris_barcelona_test{ polygon-fill: #FF6600;polygon-opacity: 1;line-color: #FFF;line-width: 0.5;line-opacity: 1; }"); | |
}); | |
}, | |
qua: function(){ | |
sql.execute("SELECT unnest(CDB_QuantileBins(array_agg(homes), 3)) FROM barris_barcelona_test") | |
.done(function(data){ | |
for(i = 0; i < data.total_rows; i++){ // loop that reads each column of the table | |
vector[i] = data.rows[i].unnest; // extract values from the column unnest ans save them in vector array | |
} | |
var cssStyle = [ | |
'#barris_barcelona_test{', | |
'polygon-fill: #FFEDA0;', | |
'polygon-opacity: 0.8;', | |
'line-color: #FFF;', | |
'line-width: 0.5;', | |
'line-opacity: 1;', | |
'}', | |
'#barris_barcelona_test [ homes <= '+ vector[2] + '] {', | |
'polygon-fill: #F03B20;', | |
'}', | |
'#barris_barcelona_test [ homes <= ' + vector[1] + '] {', | |
'polygon-fill: #FEB24C;', | |
'}', | |
'#barris_barcelona_test [ homes <= ' + vector[0] + '] {', | |
'polygon-fill: #FFEDA0;', | |
'}' | |
].join("\n"); | |
layer.setCartoCSS(cssStyle); | |
}); | |
return true; | |
}, | |
jen: function(){ | |
sql.execute("SELECT unnest(CDB_JenksBins(array_agg(homes), 3)) FROM barris_barcelona_test") | |
.done(function(data){ | |
for(i = 0; i < data.total_rows; i++){ // loop that reads each column of the table | |
vector[i] = data.rows[i].unnest; // extract values from the column unnest ans save them in vector array | |
} | |
var cssStyle = [ | |
'#barris_barcelona_test{', | |
'polygon-fill: #FFEDA0;', | |
'polygon-opacity: 0.8;', | |
'line-color: #FFF;', | |
'line-width: 0.5;', | |
'line-opacity: 1;', | |
'}', | |
'#barris_barcelona_test [ homes <= '+ vector[2] + '] {', | |
'polygon-fill: #F03B20;', | |
'}', | |
'#barris_barcelona_test [ homes <= ' + vector[1] + '] {', | |
'polygon-fill: #FEB24C;', | |
'}', | |
'#barris_barcelona_test [ homes <= ' + vector[0] + '] {', | |
'polygon-fill: #FFEDA0;', | |
'}' | |
].join("\n"); | |
layer.setCartoCSS(cssStyle); | |
}); | |
return true; | |
}, | |
htc: function(){ | |
sql.execute("SELECT unnest(CDB_HeadsTailsBins(array_agg(homes), 3)) FROM barris_barcelona_test") | |
.done(function(data){ | |
for(i = 0; i < data.total_rows; i++){ // loop that reads each column of the table | |
vector[i] = data.rows[i].unnest; // extract values from the column unnest ans save them in vector array | |
} | |
var cssStyle = [ | |
'#barris_barcelona_test{', | |
'polygon-fill: #FFEDA0;', | |
'polygon-opacity: 0.8;', | |
'line-color: #FFF;', | |
'line-width: 0.5;', | |
'line-opacity: 1;', | |
'}', | |
'#barris_barcelona_test [ homes <= '+ vector[2] + '] {', | |
'polygon-fill: #F03B20;', | |
'}', | |
'#barris_barcelona_test [ homes <= ' + vector[1] + '] {', | |
'polygon-fill: #FEB24C;', | |
'}', | |
'#barris_barcelona_test [ homes <= ' + vector[0] + '] {', | |
'polygon-fill: #FFEDA0;', | |
'}' | |
].join("\n"); | |
layer.setCartoCSS(cssStyle); | |
}); | |
return true; | |
} | |
} | |
$('#selector').change(function() { | |
LayerActions[$(this).val()](); | |
}); | |
console.log(layer); | |
}); | |
} | |
window.onload = main; | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment