misoproject / dataset

JavaScript library that makes managing the data behind client-side visualisations easy
http://misoproject.com
GNU General Public License v2.0
1.18k stars 99 forks source link

countBy does not group correctly on date type columns #94

Closed iros closed 12 years ago

iros commented 12 years ago

Try this code:

var GH = {};
GH.CommitsParser = function(options) {
  options = options || {}; 
}

_.extend(GH.CommitsParser.prototype, Miso.Parsers.prototype, {
  parse : function(data) {
    // we really only want to grab a few data points from the entire
    // api call result.
    var columns     = ['sha', 'date', 'committer'],
        dataColumns = {};

    _.each(data, function(c) {

      dataColumns.sha       = dataColumns.sha || [];
      dataColumns.date      = dataColumns.date || [];
      dataColumns.committer = dataColumns.committer || [];

      dataColumns.sha.push(c.sha);
      dataColumns.date.push(c.commit.committer.date);
      dataColumns.committer.push(c.committer.login)
    });

    return {
      columns : columns,
      data    : dataColumns
    };
  }
});

var ds = new Miso.Dataset({
  url : "https://api.github.com/repos/iros/deck.js-codemirror/commits",
  parser : GH.CommitsParser,
  columns : [
    { 
      name : 'date', 
      type : 'time', 
      format : 'YYYY-MM-DD', 
      before : function(date) {
        // let's roll the dates back to the beginning of the week, since we
        // just want the count of commits per week.
        var incomingFormat = 'YYYY-MM-DDThh:mm:ssZZ';
        var d = moment(date, incomingFormat);

        // take the day we have, and subtract from the number of days
        // into the week that it is. So if Saturday is the 6th day, it will
        // roll the date back 6 days.
        return d.subtract('days', d.day()).format(this.format);
      } 
    }
  ]
});

ds.fetch({ success : function() {
  var commitsByDay = this.countBy("date");
  console.log(commitsByDay);
}});