Can I access the data portion of the SQL queries used in the cartodb.createLayer function without the use of special event handlers?
I am making a choropleth map, and categorizing the data with Jenks natural breaks. So, in order to determine the breaks needed to set the right values in CartoCSS, I need to access all values in the data, run the Jenks process, and set the proper CartoCSS. Currently I do this via an independent SQL query prior to running createLayer:
var sql = new cartodb.SQL({ user: 'user' });
var sqlQuery = "SELECT * FROM table;"
sql.execute(sqlQuery)
This works fine, but I would rather not run the independent SQL query, if I can somehow extract that information from the createLayer function.
I set up the layer source with four sublayers:
var layerSource = {
user_name: 'user',
type: 'cartodb',
sublayers: [
{
sql: q1,
interactivity: 'cartodb_id',
cartocss: s1
},
{
sql: q2,
interactivity: 'cartodb_id',
cartocss: s2
},
{
sql: q3,
interactivity: 'cartodb_id',
cartocss: s3
},
{
sql: q4,
interactivity: 'cartodb_id',
cartocss: s4
}
]
}
Then I create the layers:
cartodb.createLayer(map, layerSource, {cartodb_logo: false})
.addTo(map)
.done(function(layer) {
for (var i = 0; i < layer.getSubLayerCount(); i++) {
subLayers[i] = layer.getSubLayer(i);
}
etc...
I know of no way to extract the attribute values from the four SQL queries that are called to the make the layer - from neither the returned 'layer' nor any of the elements in subLayers.
I can access the 'data' from a featureOver or featureClick event handler on a sublayer, e.g.
subLayer[0].on('featureOver', function(e,pos,latlng,data){
$('.leaflet-container').css('cursor','pointer');
if (data.cartodb_id != highlight.cartodb_id) {
highlightArea(data);
}
but I need the data before that - to set up CartoCSS and create a proper legend.
Any ideas?