admin管理员组文章数量:1335172
This seems to be something very simple but I have searched and not found a solution. I am trying to create a chart on my site based on the data in a google spreadsheet. I have been able to get it working using hard coded values but not sure how to fill from a spreadsheet.
My current script looks like this:
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Year', 'Sales', 'Expenses'],
['2004', 1000, 400],
['2005', 1170, 460],
['2006', 660, 1120],
['2007', 1030, 540]
]);
var options = {
title: 'Company Performance'
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
My data on my spread sheet is simlilar to what is hard coded but I want to give it the spreadsheet url and have it fill it from that (url like this: )
This seems to be something very simple but I have searched and not found a solution. I am trying to create a chart on my site based on the data in a google spreadsheet. I have been able to get it working using hard coded values but not sure how to fill from a spreadsheet.
My current script looks like this:
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Year', 'Sales', 'Expenses'],
['2004', 1000, 400],
['2005', 1170, 460],
['2006', 660, 1120],
['2007', 1030, 540]
]);
var options = {
title: 'Company Performance'
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
My data on my spread sheet is simlilar to what is hard coded but I want to give it the spreadsheet url and have it fill it from that (url like this: https://docs.google./spreadsheet/ccc?key=0AnD0SFr9ooPgdG83Wm)
Share Improve this question asked Feb 3, 2014 at 20:02 BluGeniBluGeni 3,4649 gold badges37 silver badges64 bronze badges2 Answers
Reset to default 4there is a tutorial with an example here:
https://developers.google./chart/interactive/docs/spreadsheets
Basically, you code will look like: (I'm not sure your URL is correct. Also, set the end of the url to use the right range of data):
google.load("visualization", "1", {packages:["corechart"]});
function initialize() {
var opts = {sendMethod: 'auto'};
// Replace the data source URL on next line with your data source URL.
var query = new google.visualization.Query('http://spreadsheets.google./tq?key=0AnD0SFr9ooPgdG83Wm&transpose=0&headers=1&merge=COLS&range=A1%3AA5%2CB1%3AC5&gid=0&pub=1', opts);
-
// Optional request to return only column C and the sum of column B, grouped by C members.
//query.setQuery('select C, sum(B) group by C');
// Send the query with a callback function.
query.send(handleQueryResponse);
}
function handleQueryResponse(response) {
if (response.isError()) {
alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
var data = response.getDataTable();
var options = {
title: 'Company Performance'
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
google.setOnLoadCallback(initialize);
You can also use this way
/* Google sheet dynamic data */
function drowChartUsingSheet(){
var sheetUrl = 'your google sheet link here';
var opts = {sendMethod: 'auto'};
var query = new google.visualization.Query(sheetUrl,opts);
query.send(handleQueryResponse);
}
/* handler function & drow chart */
function handleQueryResponse(response) {
if (response.isError()) {
alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
var data = response.getDataTable();
var chart = new google.visualization.PieChart(document.getElementById('pie_googlechart'));
chart.draw(data,options);
}
本文标签: javascriptFill a google chart with data from a google spreadsheetStack Overflow
版权声明:本文标题:javascript - Fill a google chart with data from a google spreadsheet - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742342356a2456857.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论