Aggregate data before charting

9,071 views
Skip to first unread message

A Eady

unread,
Oct 1, 2013, 2:35:41 PM10/1/13
to google-visua...@googlegroups.com
Hi there, 

I'm pretty new to the data visualization game. I'm wondering if the following is possible with dataTables/dataViews or if I should do it in PHP. 

I am building a charting application that charts a dataset using a Line Chart at 5 min intervals, however I would like to be able to show a monthly overview of sums and averages in a Column Chart form. 
Can I use my existing dataTable (5min intervals) to build a dataTable that aggregates the data into a daily average or sum?

IE. table 1 showing 5 min interval raw data
NB - times are really dateobjects

Timetemperaturerainfall
5251
10222
15250
20250
25260
30271
etcetcetc

table 2 showing aggregate data based on table 1

Daytemperature (avg)rainfall (sum)
Mon2510
Tues242
Wed220
Thurs260
Fri260

I think I know how to do it in PHP, so I could build a new dataTable of this info that way, but I feel like is would be more efficient to use the dataTable I already have. 
I would appreciate any advice you might have. 

Thomas Rybka

unread,
Oct 1, 2013, 3:48:42 PM10/1/13
to google-visua...@googlegroups.com
You can totally do this with the group method.

Here's a jsfiddle: http://jsfiddle.net/kTWBe/2/

You want to group the data, and you'll want a function to effectively "floor" the datetimes to the nearest day (or whatever).

Assuming your dataTable looks something like this:
    var data = new google.visualization.DataTable();
    data.addColumn('datetime', 'Time');
    data.addColumn('number', 'Temperature');
    data.addColumn('number', 'Rainfall');
    data.addRows([
        [new Date(2013, 1, 2, 10, 0, 0), 25, 1],
        [new Date(2013, 1, 2, 10, 5, 0), 25, 2],
        [new Date(2013, 1, 2, 10, 10, 0), 27, 3],
        [new Date(2013, 1, 2, 10, 15, 0), 27, 4],
        ...
    ]);

   function floorDate(datetime) {
     var newDate = new Date(datetime);
     newDate.setHours(0);
     newDate.setMinutes(0);
     newDate.setSeconds(0);
     return newDate;
   }

   var newData = google.visualization.data.group(data, [{
        column: 0,
        modifier: floorDate,
        type: 'date'
    }], [{
        column: 1,
        label: 'Avg Temp',
        aggregation: google.visualization.data.avg,
        type: 'number'
    }, {
        column: 2,
        label: 'Rainfall',
        aggregation: google.visualization.data.sum,
        type: 'number'
    }]);

-Tom


--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-visualizati...@googlegroups.com.
To post to this group, send email to google-visua...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-visualization-api.
For more options, visit https://groups.google.com/groups/opt_out.

Thomas Rybka

unread,
Oct 1, 2013, 3:49:25 PM10/1/13
to google-visua...@googlegroups.com

A Eady

unread,
Oct 1, 2013, 11:04:38 PM10/1/13
to google-visua...@googlegroups.com
That's fantastic! So much easier than what I had in mind. 

Thank you!
To unsubscribe from this group and stop receiving emails from it, send an email to google-visualization-api+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages