- you can either extract the data you use in the Sheet or do it in the code when drawing the chart.
I used your Sheet and used a query in the chart code to extract the data. The only problem was that the data table had to be transposed to get it to work properly. Luckily, there's a function to do that.
<script type="text/ecmascript">
google.charts.load("current", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function transposeDataTable(dataTable) {
//step 1: let us get what the columns would be
var rows = [];//the row tip becomes the column header and the rest become
for (var rowIdx=0; rowIdx < dataTable.getNumberOfRows(); rowIdx++) {
var rowData = [];
for( var colIdx = 0; colIdx < dataTable.getNumberOfColumns(); colIdx++) {
rowData.push(dataTable.getValue(rowIdx, colIdx));
}
rows.push( rowData);
}
var newTB = new google.visualization.DataTable();
newTB.addColumn('string', dataTable.getColumnLabel(0));
newTB.addRows(dataTable.getNumberOfColumns()-1);
var colIdx = 1;
for(var idx=0; idx < (dataTable.getNumberOfColumns() -1);idx++) {
var colLabel = dataTable.getColumnLabel(colIdx);
newTB.setValue(idx, 0, colLabel);
colIdx++;
}
for (var i=0; i< rows.length; i++) {
var rowData = rows[i];
console.log(rowData[0]);
newTB.addColumn('number',rowData[0]); //assuming the first one is always a header
var localRowIdx = 0;
for(var j=1; j< rowData.length; j++) {
newTB.setValue(localRowIdx, (i+1), rowData[j]);
localRowIdx++;
}
}
return newTB;
}
function drawChart() {
var queryString = encodeURIComponent('SELECT J, L LIMIT 6 OFFSET 1 ');
query.send(handleQueryResponse);
}
function handleQueryResponse(response) {
if (response.isError()) {
alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
var Options = {
isStacked: 'percent',
};
var data = response.getDataTable();
var transposedData = transposeDataTable(data);
var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
chart.draw(transposedData, Options);
}
</script>