Skip to content

Instantly share code, notes, and snippets.

@Sesim
Last active August 29, 2015 14:13
Show Gist options
  • Save Sesim/38973387ffa727746ac0 to your computer and use it in GitHub Desktop.
Save Sesim/38973387ffa727746ac0 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.2.js"></script>
<script type="text/javascript">
// Load the Visualization API and the core charts package.
google.load('visualization', '1.0', {
'packages': ['corechart']
});
// Set a callback to run when the Google Visualization API is fully loaded.
google.setOnLoadCallback(getData);
function getData() {
// this is the key for the spreadhseet with your data in it, you can copy this from the URL for the sheet
var key = "15drTdWfw6P6C6ZqhMTNdSBvOF4JtCqRRQWafusiBFsc";
// this is the range in the sheet to find the data
var range = "A1:B1366";
// get the date from the range specified
var query = new google.visualization.Query('https://docs.google.com/spreadsheets/d/15drTdWfw6P6C6ZqhMTNdSBvOF4JtCqRRQWafusiBFsc/edit#gid=0' + key + '&usp=sharing&range=' + range);
// Send the query with a callback function, to be executed when the data is reeady
query.send(drawChart);
}
// Callback that creates and populates a data table,
// instantiates the line chart, passes in the data and
// draws it.
function drawChart(response) {
// standard bit of code in case we are offline or you deleted the google sheet or something like that
if (response.isError()) {
console.log('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
// dig out the data from the response to the query
var data = response.getDataTable();
// there are squillions of cool options for your charts, shove them into this JSON object
// read more https://developers.google.com/chart/interactive/docs/gallery/linechart
var options = {
'title': 'Peak Times',
'legend': { position: 'out'}
};
// Instantiate and draw our line chart, passing in the options JSON object.
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
// make the graph responsive.
$(window).resize(function(){
getData();
});
</script>
</head>
<body>
<!--Div that will hold the pie chart-->
<div id="chart_div"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment