crossfilter.js:按月分组数据 [英] crossfilter.js: group data by month

查看:165
本文介绍了crossfilter.js:按月分组数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象数组,每个对象都有 Created 字段(类型为 Date )。使用crossfilter.js,我想按月对这些数据进行分组。但它似乎没有正常工作。这是代码:

I have an array of objects, each with Created field (of type Date). Using crossfilter.js, I want to group this data by month. But it doesn't seem to work properly. Here's code:

要生成示例数据,我正在使用 d3的时间实用程序:

To generate example data, I'm using d3's time utilities:

var now = new Date();
var yearAgo = d3.time.year.offset(now, -1);
var after6months = d3.time.month.offset(now, 6);

// data[] will contain one object for each month in the range
var data = d3.time.month.range(yearAgo, after6months).map(function(date) {
    return {
        "Created": date,
        "Type": 'solid',
        "State": "Down"
    };
});

var cf = crossfilter(data);
var byDate = cf.dimension(function(d) { return d.Created; });

// here I want to group by month and year, eg "October 2015"
var groupByMonth = byDate.group(function(date) { return d3.time.format("%B %Y")(date); });
var grouped = groupByMonth.all();

我希望分组包含一个元素对于该范围内的每个月(1。5年,它将是18),但我得到这个:

I would expect grouped to contain an element for each of the months in the range (for 1.5 year it would be 18), but I'm getting this:

[
{
关键:2014年10月,
价值:11
},
{
关键:2015年9月,
value:7
}
]

Crossfilter将所有记录分组在两组之下,不希望18.
我在这里做错了吗?

Crossfilter is grouping all records under two groups, not desired 18. Am I doing something wrong here?

这段代码产生了我需要的东西,但我必须从crossfilter分组中得到它:

This code produces what I need, but I have to get it from crossfilter grouping:

var desired = d3.nest()
    .key(function(d) { return d3.time.format("%B %Y")(d.Created)})
    .rollup(function(group) { return group.length; })
    .entries(data);

这里的 jsFiddle http://jsfiddle.net/xxjpusa7/5

推荐答案

这是一个更新的小提琴,可以做我认为你想要它做的事。

Here's an updated fiddle which does what I think you wanted it to do.

https://jsfiddle.net/xxjpusa7/4/

var now = new Date();
var yearAgo = d3.time.year.offset(now, -1);
var after6months = d3.time.month.offset(now, 6);

// data[] will contain one object for each month in the range
var data = d3.time.month.range(yearAgo, after6months).map(function (date) {
    return {
        "Created": date,
        "Type": 'solid',
        "State": "Down"
    };
});

var cf = crossfilter(data);
var dateFormat = d3.time.format("%B %Y");

var byDate = cf.dimension(function (d) {
     return dateFormat(d.Created);
});

// Groups using the identity function
var groupByMonth = byDate.group();

var grouped = groupByMonth.all();

d3.select("div").text(JSON.stringify(grouped));

基本上,维度已更改为按格式化日期索引数据。并且 byDate.group(); 负责计算维度中的唯一索引(在本例中为月份 )。它可以将组视为 map-reduce 而非实际分组。

Basically the dimension was changed to index your data by the formatted date. And byDate.group(); takes care of counting the unique indexes in your dimension (in this case the month year). It serves to think of group as a map-reduce instead of actual grouping.

这篇关于crossfilter.js:按月分组数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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