Search code examples
d3.jsrickshaw

How do I get rickshaw to aggregate data into weeks instead of days


My x,y values consist of unix epoch times (over about a year) and an integer. I graph the x axis using this:

        x_axis = new Rickshaw.Graph.Axis.Time
          graph: graph

The result looks good, but the tick marks are day based (I assume) and I get dips on the weekends when nothing is happening. I would like to aggregate based on weeks. Is it possible for rickshaw to combine the y values together for all times that fall in the same week?

Here's a working jsfiddle: http://jsfiddle.net/jTmWC/


Solution

  • You can aggregate the data per month using this code:

    var aggregated = d3.nest()
                   .key(function(d) { return (new Date(+d.x * 1000)).getMonth(); })
                   .rollup(function(d) { return d3.sum(d, function(e) { return +e.y; }); })
                   .entries(data)
                   .map(function(d) { return {x: +d.key, y: d.values}; });
    

    Updated jsfiddle here. Note that I also changed the time unit for the axis -- you might need to play around with it a bit to make it show the labels you want.