The way to handle this is to create a page/service/controller (depending on your site architecture) that serves up a JSON string representation of a DataTable object that contains the data from your SQL query. You can then query this service from javascript with AJAX.
The structure of the JSON string is and object with two properties: "cols" and "rows".
The "cols" property is an array of column objects. Each column object has "type" (mandatory), "label", "id", "p", and "role" (all optional) properties. "type" is a string that describes the data type of the column ("string", "number", "date", "datetime", "timeofday", and "boolean" are the valid types). "label" is the label of the column, which is used for table headers and data series labels. "id" is an id to reference the column by, but has practically no use. "p" is an object containing key/value pairs for additional column properties; which properties are valid depends on the type(s) of visualizations drawn. "role" is used to specify a
column role for the column and may be left out if the default column role ("domain" or "data") is appropriate.
The "rows" property is an array of row objects. Each row object contains "c" (mandatory) and "p" (optional) properties. "c" is an array of cell objects. "p" is an object containing key/value pairs for row properties; which properties are valid depends on the type(s) of
visualizations drawn (currently, there are no first-party visualizations that use row properties, so you will probably not need to use this). A cell object contains "v" (mandatory), "f", and "p" (all optional) properties. "v" is the value of the cell; this must be the same data type as the column the cell is in. "f" is the formatted value of the cell, which is the string that gets displayed wherever the value is shown; if left out, the default formatting is used, or a
formatter can be applied to the column to format the data. "p" is an object containing key/value pairs for cell properties; which properties are valid depends on the type(s) of
visualizations drawn (as I recall, the only first-party visualization that uses cell properties is the Table).
Here's an example JSON string:
"{"cols":[{"label":"Name","type":"string"},{"label":"Value","type":"number"}],"rows":[{"c":[{"v":"foo"},{"v":10}]},{"c":[{"v":"bar"},{"v":10}]},{"c":[{"v":"cad"},{"v":10}]},{"c":[{"v":"qud"},{"v":10}]}]}"broken down to make it more readable:
{
"cols":[
{"label":"Name","type":"string"},
{"label":"Value","type":"number"}
],
"rows":[
{"c":[{"v":"foo"},{"v":10}]},
{"c":[{"v":"bar"},{"v":10}]},
{"c":[{"v":"cad"},{"v":10}]},
{"c":[{"v":"qud"},{"v":10}]}
]
}I expect that C# has a method for turning maps, arrays, and objects into JSON without you having to do it manually.
Once you have a service that provides the data in the correct format, you can use an AJAX query to fetch the data dynamically. Here's an example that uses jQuery's AJAX function (though this can be written in other frameworks or even plain javascript):
function drawChart () {
$.ajax({
url: '/path/to/data/service/',
type: 'GET', // or 'POST'
data: {
// map of key/value pairs to pass to the server, eg:
foo: 'bar',
cad: 10,
qud: false,
fiz: [1, 2, 3],
buz: {
piz: 7.89
}
},
dataType: 'json',
success: function (json) {
var data = new google.visualization.DataTable(json);
var chart = new google.visualization.LineChart(document.querySelector('#chart_div'));
chart.draw(data, {
heigh: 400,
width: 600
});
},
error: function (response) {
// handle errors
}
});
}
google.load('visualization', '1', {packages: ['corechart'], callback: drawChart});