Stay organized with collections
Save and categorize content based on your preferences.
This page describes how to send a query to a data
source that supports the Chart Tools Datasource protocol.
Overview
A Datasource is a web service that supports the Chart Tools Datasource protocol. You can send a SQL query to a Datasource, and in response you will receive a DataTable populated with the appropriate information. Some examples of Datasources include Google Spreadsheets and SalesForce.
Sending a request
To send a request:
Instantiate a Query object with the URL of your Datasource. The URL should indicate what data is being
requested, in a syntax understood by that data source.
Optionally specify request options such as sending method as an optional second parameter in the Query object
constructor (see the Query constructor's opt_options parameter for details):
Optionally add a query
language string to sort or filter the results, and then send the request.
Datasources are not required to support the Chart Tools Datasource query language. If the Datasource does not support the query language, it will ignore the SQL query string, but still return
a DataTable. The query language is a SQL language variant; read the full query language syntax here.
Send the query, specifying a callback handler that will be called when the response is received: see next section for details.
Here's an example of sending a request for data in a Google
Spreadsheet cell range; to learn how to get the URL for a Google Spreadsheet, see here:
functioninitialize(){varopts={sendMethod:'auto'};//ReplacethedatasourceURLonnextlinewithyourdatasourceURL.varquery=newgoogle.visualization.Query('http://spreadsheets.google.com?key=123AB&...',opts);//OptionalrequesttoreturnonlycolumnCandthesumofcolumnB,groupedbyCmembers.query.setQuery('select C, sum(B) group by C');//Sendthequerywithacallbackfunction.query.send(handleQueryResponse);}functionhandleQueryResponse(response){//Calledwhenthequeryresponseisreturned....}
Processing the response
Your response handler function will be called when the request returns.
The parameter passed in to your response handler function is of type google.visualization.QueryResponse.
If the request was successful, the response contains a data table (class google.visualization.DataTable).
If the request failed, the response contains information about the error,
and no DataTable.
Your response handler should do the following:
Check whether the request succeeded or failed by calling response.isError().
You shouldn't need to display any error messages to the user; the Visualization
library will display an error message for you in your container <div>.
However, if you do want to handle errors manually, you can use the goog.visualization.errors class
to display custom messages (see the Query
Wrapper Example for an example of custom
error handling).
If the request succeeded, the response will include a DataTable that you can retrieve by calling getDataTable(). Pass it to your chart.
The following code demonstrates handling the previous request to draw a pie chart:
functionhandleQueryResponse(response){if(response.isError()){alert('Error in query: '+response.getMessage()+' '+response.getDetailedMessage());return;}vardata=response.getDataTable();varchart=newgoogle.visualization.PieChart(document.getElementById('chart_div'));chart.draw(data,{width:400,height:240,is3D:true});}
Reading CSV files
If you want to build a chart out of CSV (comma-separated values)
data, you have two choices:
Convert the data into the Google Charts datatable format
Place the CSV file on the web server serving the chart, and query
it using the technique on this page.
More information
Query Language Syntax - Describes the syntax of the language used to make data
queries.
Query Class - Reference page for the class
that wraps a query.
QueryResponse Class - Reference
page for the class that wraps the response to a query.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["\u003cp\u003eSend a SQL query to a Datasource, which is a web service supporting the Chart Tools Datasource protocol, to receive a DataTable in response.\u003c/p\u003e\n"],["\u003cp\u003eInstantiate a Query object with the Datasource URL, add optional request options and a query language string to filter results, then send the request.\u003c/p\u003e\n"],["\u003cp\u003eHandle the response by checking for errors and if successful, retrieve the DataTable using \u003ccode\u003egetDataTable()\u003c/code\u003e and use it to create your chart.\u003c/p\u003e\n"],["\u003cp\u003eFor CSV data, convert it to Google Charts datatable format or place it on your web server and query it using the described techniques.\u003c/p\u003e\n"],["\u003cp\u003eExplore the Query Language Syntax, Query Class, and QueryResponse Class for more detailed information and advanced functionalities.\u003c/p\u003e\n"]]],[],null,["# Data Queries\n\nThis page describes how to send a query to a data\nsource that supports the Chart Tools Datasource protocol.\n\nOverview\n--------\n\nA Datasource is a web service that supports the Chart Tools Datasource protocol. You can send a SQL query to a Datasource, and in response you will receive a DataTable populated with the appropriate information. Some examples of Datasources include [Google Spreadsheets](/chart/interactive/docs/spreadsheets) and SalesForce.\n\nSending a request\n-----------------\n\n**To send a request:**\n\n1. Instantiate a [Query](/chart/interactive/docs/reference#Query) object with the URL of your Datasource. The URL should indicate what data is being requested, in a syntax understood by that data source.\n2. Optionally specify request options such as sending method as an optional second parameter in the `Query` object constructor (see the Query constructor's [`opt_options`](/chart/interactive/docs/reference#Query) parameter for details):\n3. Optionally add a [query\n language string](/chart/interactive/docs/querylanguage) to sort or filter the results, and then send the request. Datasources are not required to support the Chart Tools Datasource query language. If the Datasource does not support the query language, it will ignore the SQL query string, but still return a `DataTable`. The query language is a SQL language variant; read the full [query language syntax here](/chart/interactive/docs/querylanguage).\n4. Send the query, specifying a callback handler that will be called when the response is received: see next section for details.\n\nHere's an example of sending a request for data in a Google\nSpreadsheet cell range; to learn how to get the URL for a Google Spreadsheet, see [here](/chart/interactive/docs/spreadsheets#Google_Spreadsheets_as_a_Data_Source): \n\n```gdscript\nfunction initialize() {\n var opts = {sendMethod: 'auto'};\n // Replace the data source URL on next line with your data source URL.\n var query = new google.visualization.Query('http://spreadsheets.google.com?key=123AB&...', opts);\n\n // Optional request to return only column C and the sum of column B, grouped by C members.\n query.setQuery('select C, sum(B) group by C');\n\n // Send the query with a callback function.\n query.send(handleQueryResponse);\n}\n\nfunction handleQueryResponse(response) {\n // Called when the query response is returned.\n ...\n}\n```\n\nProcessing the response\n-----------------------\n\n\nYour response handler function will be called when the request returns.\nThe parameter passed in to your response handler function is of type [google.visualization.QueryResponse](/chart/interactive/docs/reference#QueryResponse).\nIf the request was successful, the response contains a data table (class `google.visualization.DataTable`).\nIf the request failed, the response contains information about the error,\nand no `DataTable`.\n\n**Your response handler should do the following:**\n\n1. Check whether the request succeeded or failed by calling `response.isError()`. You shouldn't need to display any error messages to the user; the Visualization library will display an error message for you in your container `\u003cdiv\u003e`. However, if you do want to handle errors manually, you can use the [`goog.visualization.errors`](/chart/interactive/docs/reference#errordisplay) class to display custom messages (see the [Query\n Wrapper Example](/chart/interactive/docs/examples#querywrapper) for an example of custom error handling).\n2. If the request succeeded, the response will include a `DataTable` that you can retrieve by calling `getDataTable()`. Pass it to your chart.\n\nThe following code demonstrates handling the previous request to draw a pie chart: \n\n```gdscript\nfunction handleQueryResponse(response) {\n\n if (response.isError()) {\n alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());\n return;\n }\n\n var data = response.getDataTable();\n var chart = new google.visualization.PieChart(document.getElementById('chart_div'));\n chart.draw(data, {width: 400, height: 240, is3D: true});\n}\n```\n\nReading CSV files\n-----------------\n\nIf you want to build a chart out of CSV (comma-separated values)\ndata, you have two choices:\n\n- Convert the data into the Google Charts datatable format\n- Place the CSV file on the web server serving the chart, and query it using the technique on this page.\n\nMore information\n----------------\n\n- [Query Language Syntax](/chart/interactive/docs/querylanguage) - Describes the syntax of the language used to make data queries.\n- [Query Class](/chart/interactive/docs/reference#Query) - Reference page for the class that wraps a query.\n- [QueryResponse Class](/chart/interactive/docs/reference#QueryResponse) - Reference page for the class that wraps the response to a query."]]