Skip to content

Instantly share code, notes, and snippets.

@andy-esch
Last active August 29, 2015 14:15
Show Gist options
  • Save andy-esch/d9b65186664268d6dec4 to your computer and use it in GitHub Desktop.
Save andy-esch/d9b65186664268d6dec4 to your computer and use it in GitHub Desktop.
Use bounding box to calculate values to populate a chart
<!DOCTYPE html>
<html>
<head>
<title>Custom infowindow 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 {
height: 100%;
padding: 0;
margin: 0;
}
#map {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
z-index: 1000;
}
#dashboard {
background: #fff;
border-left: 1px solid #000;
height: 100%;
font-family: 'Open Sans', sans-serif;
overflow-y: scroll;
padding: 10px;
position: absolute;
right: 0;
text-align: left;
top: 0;
z-index: 2001;
opacity: 0.90;
width: 300px;
}
#chart_div {
height: 300px;
width: 300px;
}
</style>
<link rel="stylesheet" href="http://libs.cartodb.com/cartodb.js/v3/3.11/themes/css/cartodb.css" />
</head>
<body>
<div id="map"></div>
<div id="dashboard">
<button id="getTotal">Get total population count</button>
<div id="chart_div">Chart not loaded. Click on a marker or the button above to populate the chart.</div>
<div id="info"></div>
</div>
<script type="infowindow/html" id="infowindow_template">
</script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script src="http://libs.cartodb.com/cartodb.js/v3/3.11/cartodb.js"></script>
<script>
// load visualization library from google
google.load('visualization', '1.0', {'packages':['corechart']});
function draw_chart(data, name) {
var data = google.visualization.arrayToDataTable([
['', 'population'],
['max', data[0]],
['min', data[1]]
]);
var options = {
title: name + ' population',
legend: { position: "none" },
width: 300,
};
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
function main() {
// create map
var map = L.map('map', {
zoomControl: false,
center: [0, 0],
zoom: 3
});
var sql = new cartodb.SQL({ user: 'documentation' });
// add a nice baselayer from Cartodb
L.tileLayer('http://{s}.api.cartocdn.com/base-light/{z}/{x}/{y}.png', {
attribution: 'CartoDB'
}).addTo(map);
cartodb.createLayer(map, 'http://documentation.cartodb.com/api/v2/viz/7eb2096a-51d9-11e3-89a7-5404a6a683d5/viz.json')
.addTo(map)
.on('done', function(layer) {
var sublayer = layer.getSubLayer(0);
sublayer.setInteraction(true);
sublayer.setInteractivity("cartodb_id, name,pop_max,pop_min");
sublayer.on('featureClick',function(e, latlng, pos, data, subLayerIndex) {
draw_chart([data.pop_max, data.pop_min], data.name);
$("#info").html("<h4>Max Population</h4><p>" + data.pop_min + "</p><h4>Min. Population</h4><p>" + data.pop_min + "</p>");
});
}).on('error', function() {
console.log("some error occurred");
});
$('#getTotal').on('click',function() {
var bounds = map.getBounds();
console.log(bounds);
sql.execute("SELECT SUM(pop_min) pmin_total,SUM(pop_max) pmax_total FROM ne_10m_populated_places_simple_1 WHERE ST_Intersects(the_geom,ST_Envelope(ST_GeomFromText('SRID=4326;LINESTRING(" + bounds._southWest.lng + " " + bounds._southWest.lat + ", " + bounds._northEast.lng + " " + bounds._northEast.lat + ")')))")
.done(function(data) {
draw_chart([+data.rows[0].pmax_total, +data.rows[0].pmin_total], "Totals for current view");
$("#info").html("<h4>Max Population</h4><p>" + data.rows[0].pmax_total + "</p><h4>Min. Population</h4><p>" + data.rows[0].pmin_total + "</p>");
console.log(data);
});
});
}
window.onload = main;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment