How can I group dates along the h-axis into weeks, months or quarters

5,767 views
Skip to first unread message

Bernardo Alves

unread,
Feb 22, 2013, 6:01:38 AM2/22/13
to google-visua...@googlegroups.com
Hi,

I have a chart with continuous data (date type) along the h-axis. What I would like to do is group the underlying variable, let's say "Sales", with a function (SUM, for example) by a specific aggregation of dates, such as Week, Month or Quarter.

I am able to do this with Microsoft's Datavisualization.Charting charts for .net. Here is a line of code to do this:

Chart1.DataManipulator.Group("SUM, X:CENTER", 1, IntervalType.Weeks, 1, DateTimeIntervalType.Days, ChSeries)

Could I do something similar with Google Charts?

Thanks for any insights you could share on the subject.

Cheers,
Bernardo

asgallant

unread,
Feb 22, 2013, 11:06:00 AM2/22/13
to google-visua...@googlegroups.com
Yes, you can group data, using the google.visualization.data.group method.  This takes a DataTable and performs a group on it to create a new DataTable, which you can then use to draw your chart.

Bernardo Alves

unread,
Feb 22, 2013, 11:29:32 AM2/22/13
to google-visua...@googlegroups.com
Hi,

Thank you for your input. I know about the grouping capabilities of the DataTable. My question is, would the result of this grouping still be considered as a continuous?

Here is the problem: my current data is already aggregated by week with an integer representing the year and the week (yyyyww). The problem happens when I jump from 201252 to 201301: since I need this to be a continuous variable in order to use a ChartRangeFilter, a huge gap is generated between these two numbers. That's why I need the underlying data to be of type Date and the grouping done on the chart, as the example I posted about Microsoft's chart...

Any thoughts?

Cheers,
Bernardo.

asgallant

unread,
Feb 22, 2013, 11:40:16 AM2/22/13
to google-visua...@googlegroups.com
You can make Date objects out of that, which will fix your continuous data problem.  Create Date objects in the form:

new Date(year, 0, week * 7)

You can then format your data to display year and week of year.

When you group on the date axis, you can group by months or quarters, however you like, and format the display as appropriate.

Bernardo Alves

unread,
Feb 22, 2013, 11:50:01 AM2/22/13
to google-visua...@googlegroups.com
Thank you. I'll give this a try!

Cheers,
Bernardo
Reply all
Reply to author
Forward
0 new messages