function choroplether(data){ // get the currently selected style cartodb.createVis('map', 'http://team.cartodb.com/api/v2/viz/d4c88194-7bc7-11e4-ab0a-0e853d047bba/viz.json') .done(function(vis, layers) { var subLayer = layers[1].getSubLayer(0); var sql, style, arr = [], min, max, step; for (i in data){ if(data[i].trim().length>0){ var d = data[i].split(','); arr.push( "('"+d[0]+"',"+d[1]+")"); var k = parseFloat(d[1]); if(kmax || max==null){max = k} } // console.log(sql) } step = (max-min)/7; sql = "WITH c AS (SELECT * FROM (VALUES "+arr.join(',')+") n (iso, val)) " + "SELECT c.val, c.iso, the_geom, the_geom_webmercator, cartodb_id FROM " + "choroplether t, c WHERE t.iso2 = c.iso"; style = "#choroplether{ polygon-fill: #0C2C84; polygon-opacity: 0.6; line-color: #0C2C84; line-width: 0.5; line-opacity: 1; } " + "#choroplether [ val <= "+(max-step)+"] { polygon-fill: #225EA8; } " + "#choroplether [ val <= "+(max-2*step)+"] { polygon-fill: #1D91C0; } " + "#choroplether [ val <= "+(max-3*step)+"] { polygon-fill: #41B6C4; } " + "#choroplether [ val <= "+(max-4*step)+"] { polygon-fill: #7FCDBB; } " + "#choroplether [ val <= "+(max-5*step)+"] { polygon-fill: #C7E9B4; } " + "#choroplether [ val <= "+(max-6*step)+"] { polygon-fill: #FFFFCC; } " subLayer.set({sql: sql, cartocss: style}); console.log(style) }) .error(function(err) { console.log(err); }); }