Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Chart.js using SQL API
<!doctype html>
<title>Tornadoes total versus max damage</title>
<script src=""></script>
<meta name = "viewport" content = "initial-scale = 1, user-scalable = no">
<script src=""></script>
<canvas id="canvas" height="450" width="600"></canvas>
var sql = cartodb.SQL({ user: 'andrew' });
sql.execute("SELECT date_part('Month', as month, count(*) total, sum(damage) damage FROM tornados t GROUP BY date_part('Month', ORDER BY date_part('Month', ASC").done(function(data) {
var total = [];
var damage = [];
for (i in data.rows){
var lineChartData = {
labels : ["January","February","March","April","May","June","July", "August", "September", "October", "November", "December"],
datasets : [
fillColor : "rgba(220,220,120,0.5)",
strokeColor : "rgba(220,220,120,1)",
pointColor : "rgba(220,220,120,1)",
pointStrokeColor : "#fff",
data : damage
fillColor : "rgba(151,187,205,0.5)",
strokeColor : "rgba(151,187,205,1)",
pointColor : "rgba(151,187,205,1)",
pointStrokeColor : "#fff",
data : total
var myLine = new Chart(document.getElementById("canvas").getContext("2d")).Line(lineChartData);

This comment has been minimized.

Copy link

hackerjj commented Apr 25, 2014

Hi Andrewxhill,

Thank you for your script, can you tell me where do i can connect to my sql?

Best regard!


This comment has been minimized.

Copy link

klukiyan commented Feb 1, 2018

this is a dangerous approach as you are disclosing your sql credentials to whoever visits the page and clicks "view page source".
The SQL is better to be processed in php (or any other server side programming language)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.