Is it possible to group by multiple dimensions in crossfilter?

后端 未结 3 940
囚心锁ツ
囚心锁ツ 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条回答
  •  萌比男神i
    2021-02-02 03:48

    I didn't find the accepted answer all that helpful.

    I used the following instead.

    I first made a keyed group (in your case month)

       var authors = cf.dimension(function (d) {
         return +d['month'];
       })
    

    Next, I used a map reduce method on the keyed dataset to compute the averages

    The grouping helper function:

    var monthsAvg = authors.group().reduce(reduceAddbooks, reduceRemovebooks, reduceInitialbooks).all();
    

    The map-reduce functions:

    function reduceAddbooks(p, v) {
        p.author = v['author'];
        p.books = +v['books'];
        return p;
    }
    
    function reduceRemovebooks(p, v) {
        p.author = v['author'];
        p.books = +v['books'];
        return p;
    }
    
    function reduceInitialbooks() {
        return {
            author:0,
            books:0
        };
    }
    

提交回复
热议问题