是否可以在crossfilter中按多个维度进行分组? [英] Is it possible to group by multiple dimensions in crossfilter?

查看:102
本文介绍了是否可以在crossfilter中按多个维度进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如如果我们有书籍,作者和日期信息的数据。我们可以为每月为作者提供多少本书构建一个交叉过滤器吗?

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?

推荐答案

在伪sql术语中,你在尝试什么要做的是:

In pseudo sql terms, what you are trying to do is:

SELECT COUNT(book)
GROUP BY author, month

我处理此类问题的方法是将字段组合为一个维度。因此,在您的情况下,我会将月份和作者信息连接在一起作为维度。

The way I approach this type of problem is to 'group' the fields together into a single dimension. So in your case I would concatenate the month and author information together, into a dimension.

让这成为我们的测试数据:

Let this be our test data:

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);
  return 'month='+thisDate.getMonth()+';author='+d.author;
});

现在我们可以简单地做一个减少计数来计算每个作者有多少本书,每个月(即每个维度单位):

And now we can just simply do a reduce count to calculate how many books there are per author, per month (i.e. per dimension unit):

var monthAuthorCount = dimensionMonthAuthor.group().reduceCount(function (d) { return d.book; }).all();

结果如下:

{"key":"month=0;author=Mr X","value":2}
{"key":"month=0;author=Ms Y","value":3}
{"key":"month=1;author=Mr X","value":1}
{"key":"month=2;author=Mr X","value":1}
{"key":"month=2;author=Ms Y","value":3}
{"key":"month=3;author=Mr X","value":2}
{"key":"month=3;author=Ms Y","value":1}

这篇关于是否可以在crossfilter中按多个维度进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆