Crossfilter - 无法从其他组(不是来自关联组)获取过滤记录

Crossfilter - Cannot get filtered records from other groups (NOT from associate groups)

我正在使用来自此参考 http://square.github.io/crossfilter/

的 "airplane" 数据集
date,delay,distance,origin,destination
01010001,14,405,MCI,MDW
01010530,-11,370,LAX,PHX
...

  // Create the crossfilter for the relevant dimensions and groups.
  var flight = crossfilter(flights),
      all = flight.groupAll(),
      date = flight.dimension(function(d) { return d.date; }),
      dates = date.group(d3.time.day),
      hour = flight.dimension(function(d) { return d.date.getHours() + d.date.getMinutes() / 60; }),
      hours = hour.group(Math.floor),
      delay = flight.dimension(function(d) { return Math.max(-60, Math.min(149, d.delay)); }),
      delays = delay.group(function(d) { return Math.floor(d / 10) * 10; }),
      distance = flight.dimension(function(d) { return Math.min(1999, d.distance); }),
      distances = distance.group(function(d) { return Math.floor(d / 50) * 50; });

跟随Crossfilter的document,"groups don't observe the filters on their own dimension" => 我们可以从组中获取过滤记录,此时他们的维度没有被过滤,不是吗?

我已经进行了一些测试,但这是不正确的:

  console.dir(date.group().all()); // 50895 records
  console.dir(distance.group().all()); // 297 records

  date.filter([new Date(2001, 1, 1), new Date(2001, 2, 1)]);

  console.dir(date.group().all()); // 50895 records => this number still the same because we are filtering on its dimension
  console.dir(distance.group().all()); // 297 records => but this number still the same too. I don't know why
  1. 能否请您解释一下,为什么 "distance.group().all()" 的数量仍然与我们执行过滤之前相同?我是不是漏掉了什么?

  2. 如果真的不能通过这种方式从"distance dimension"得到"filtered records",怎么办呢?

谢谢。

所以,是的,这是预期的行为。

Crossfilter 将通过应用维度键和组键函数为它找到的每个值在组中创建一个 "bin"。然后当应用过滤器时,它将应用 reduce-remove 函数,默认情况下减去删除的行数。

结果是空 bin 仍然存在,但它们的值为 0。

编辑:这里是the Crossfilter Gotchas entry with further explanation

如果要删除零,可以使用 "fake group" 来完成。

function remove_empty_bins(source_group) {
    return {
        all:function () {
            return source_group.all().filter(function(d) {
                //return Math.abs(d.value) > 0.00001; // if using floating-point numbers
                return d.value !== 0; // if integers only
            });
        }
    };
}

https://github.com/dc-js/dc.js/wiki/FAQ#remove-empty-bins

此函数将组包装在一个对象中,该对象通过调用 source_group.all() 实现 .all(),然后过滤结果。因此,如果您使用的是 dc.js,您可以像这样向您的图表提供这个假组:

chart.group(remove_empty_bins(yourGroup));