首页 > 解决方案 > Crossfilter - 无法从其他组(不是从关联组)获取过滤记录

问题描述

我正在使用此参考http://square.github.io/crossfilter/中的“飞机”数据集

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 的文档,“组不观察他们自己维度上的过滤器” => 我们可以从他们的维度目前没有过滤的组中获取过滤记录,不是吗?

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

  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. 如果我们真的无法通过这种方式从“距离维度”中获取“过滤记录”,我该如何实现呢?

谢谢。

标签: javascriptcrossfilter

解决方案


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

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

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

编辑:这里是带有进一步解释的 Crossfilter Gotchas 条目

如果要删除零,可以使用“假组”来执行此操作。

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

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

chart.group(remove_empty_bins(yourGroup));

推荐阅读