Is it possible to group by multiple dimensions in crossfilter?

后端 未结 3 942
囚心锁ツ
囚心锁ツ 2021-02-02 03:36

For Example If we have data for books, authors and date information. Can we build a crossfilter for how many books are present for author per month?

3条回答
  •  误落风尘
    2021-02-02 03:46

    I want to update an old answer with a new work around described in: https://github.com/dc-js/dc.js/pull/91

    This performance hasn't been tested on large data-sets

      var cf = crossfilter([
      { date:"1 jan 2014", author: "Mr X", book: "Book 1" },
      { date:"2 jan 2014", author: "Mr X", book: "Book 2" },
      { date:"3 feb 2014", author: "Mr X", book: "Book 3" },
      { date:"1 mar 2014", author: "Mr X", book: "Book 4" },
      { date:"2 apr 2014", author: "Mr X", book: "Book 5" },
      { date:"3 apr 2014", author: "Mr X", book: "Book 6"},
      { date:"1 jan 2014", author: "Ms Y", book: "Book 7" },
      { date:"2 jan 2014", author: "Ms Y", book: "Book 8" },
      { date:"3 jan 2014", author: "Ms Y", book: "Book 9" },
      { date:"1 mar 2014", author: "Ms Y", book: "Book 10" },
      { date:"2 mar 2014", author: "Ms Y", book: "Book 11" },
      { date:"3 mar 2014", author: "Ms Y", book: "Book 12" },
      { date:"4 apr 2014", author: "Ms Y", book: "Book 13" }
      ]);
    
      var dimensionMonthAuthor = cf.dimension(function (d) {
        var thisDate = new Date(d.date);
        //stringify() and later, parse() to get keyed objects
        return JSON.stringify ( { date: thisDate.getMonth() , author: d.author } ) ;
      });
    
      group = dimensionMonthAuthor.group();
      //this forEach method could be very expensive on write.
      group.all().forEach(function(d) {
        //parse the json string created above
        d.key = JSON.parse(d.key);
      });
    
      return group.all()
    

    Results in:

    [ { key: { date: 0, author: 'Mr X' },
        value: 2 },
      { key: { date: 0, author: 'Ms Y' },
        value: 3 },
      { key: { date: 1, author: 'Mr X' },
        value: 1 },
      { key: { date: 2, author: 'Mr X' },
        value: 1 },
      { key: { date: 2, author: 'Ms Y' },
        value: 3 },
      { key: { date: 3, author: 'Mr X' },
        value: 2 },
      { key: { date: 3, author: 'Ms Y' },
        value: 1 } ]
    

提交回复
热议问题