Aftere reading all the links I gave (several times), I finally worked this out. It confused me why so many tables seemed to be made. Here's the sequence of events:
2) Create a new table view with the grouping data and averages . As all the data is being used, the grouping data is all the same (in this case an empty string). Then use then aggregation to calculate the averages.
3) Create the averages variables from the above table view - they are all going to be the same.
4) Make a new view from the original datatable and add the the calcuated averages to new columns.
What I did was start from scratch to make things clearer:
<script>
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var queryOptions = {
// Define the CSV data columns
csvColumns: ['date', 'number', 'number', 'number', 'number'],
// This should be false if your CSV file doesn't have a header
csvHasHeader: true
}
// Create the query giving the path and name of the CSV file
var query = new google.visualization.Query('jose.csv', queryOptions);
query.send(handleQueryResponse);
}
function handleQueryResponse(response) {
if (response.isError()) {
alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
var dataTable = response.getDataTable();dataTable
//Create the averages using aggregation
var averages = google.visualization.data.group(
dataTable,
// The first column is what to group on, in this case an empty string
[{column: 0, type: 'string', modifier: function () {return '';}}],
[
{column: 1, type: 'number', aggregation: google.visualization.data.avg},
{column: 2, type: 'number', aggregation: google.visualization.data.avg},
{column: 3, type: 'number', aggregation: google.visualization.data.avg},
{column: 4, type: 'number', aggregation: google.visualization.data.avg}
]
);
// Put the calculated averages into variables
var diastolAvg = averages.getValue(0, 1);
var sistolAvg = averages.getValue(0, 2);
var pulsaAvg = averages.getValue(0, 3);
var saturaAvg = averages.getValue(0, 4);
// Create a new view from the table and add the averages
var dataView = new google.visualization.DataView(dataTable);
dataView.setColumns([0, 1, 2, 3, 4, {
label: 'diastolAvg',
type: 'number',
calc: function (dt, row) {
return diastolAvg;
}
}, {
label: 'sistolAvg',
type: 'number',
calc: function (dt, row) {
return sistolAvg;
}
}, {
label: 'pulsaAvg',
type: 'number',
calc: function (dt, row) {
return pulsaAvg;
}
}, {
label: 'saturaAvg',
type: 'number',
calc: function (dt, row) {
return saturaAvg;
}
}]);
var options = {
series: {
0:{type: 'area'},
1:{type: 'area'},
2:{type: 'area'},
3:{type: 'area'},
4:{type: 'line'},
5:{type: 'line'},
6:{type: 'line'},
7:{type: 'line'}
}
};
var joseChart = new google.visualization.ComboChart(document.getElementById('joseChart-div'));
joseChart.draw(dataView, options);
}
</script>
<div id="joseChart-div" style="width:100%; height:900px;"></div>
Like most things, it's easy once you understand what's going on.