Crossfilter维度过滤

时间:2017-05-09 09:56:43

标签: d3.js dc.js crossfilter

我正在研究dc.js图表​​。我已经设置了小提琴here

我有以下数据:

var data = [
  {"state": "A","value": 100,"volume": 10,"id": 4,"date": "10/1/2017","category": "H","channel": "CRM"},
  {"state": "B","value": 50,"volume": 10,"id": 2,"date": "8/1/2017","category": "A","channel": "CRM"},
  {"state": "A","value": 250,"volume": 5,"id": 4,"date": "10/1/2017","category": "H","channel": "CRM"},
  {"state": "A","value": 40,"volume": 4,"id": 3,"date": "9/1/2017","category": "A","channel": "Sales"},
  {"state": "C","value": 10,"volume": 1,"id": 5,"date": "11/1/2017","category": "A","channel": "Sales"},
  {"state": "B","value": 10,"volume": 1,"id": 2,"date": "8/1/2017","category": "H","channel": "CRM"},
  {"state": "D","value": 150,"volume": 3,"id": 1,"date": "7/1/2017","category": "A","channel": "Sales"},
  {"state": "D","value": 100,"volume": 5,"id": 1,"date": "7/1/2017","category": "H","channel": "Sales"},
  {"state": "C","value": 50,"volume": 1,"id": 5,"date": "11/1/2017","category": "H","channel": "Sales"}
]

我有四个维度(州,类别,渠道,ID),所有维度都按值分组。

我需要知道是否可以过滤维度,以便在设置范围60时,图表应显示其总值大于60的所有ID。

例如,状态饼图应仅显示值大于60的切片,即仅A和D.

1 个答案:

答案 0 :(得分:1)

crossfilter本身不会按值过滤。如果你考虑一下,在它完成减少之前,将行数据集中的数据集减少到某个值会非常复杂。

但我认为你并不是在寻找过滤器,只是因为没有显示太小的切片。你可以使用"fake group"来做到这一点。这是一个后处理步骤,它不会改变交叉滤波器滤波器中包含的数据,只是您选择要显示的项目。

这个非常类似于"删除空箱"假组。但是,我们不是寻找值0,而是寻找最小值。

function remove_small_bins(source_group, lower_bound) {
    return {
        all:function () {
            return source_group.all().filter(function(d) {
                return d.value > lower_bound;
            });
        }
    };
}

将它应用于您的示例(小提琴数据与上面显示的数据略有不同),首先我们需要查看d.value.orderValue instead of just d.value`:

function remove_small_bins(source_group, lower_bound) {
    return {
        all:function () {
            return source_group.all().filter(function(d) {
                return d.value.orderValue > lower_bound;
            });
        }
    };
}

像这样申请假组:

var stateGroup = stateDim.group().reduce(reduceAdd, reduceRemove, reduceInitial);
var stateGroupNoSmalls = remove_small_bins(stateGroup, 60);
// ...
stateChart
  .group(stateGroupNoSmalls)

你小提琴的叉子:https://jsfiddle.net/gordonwoodhull/cyjztajv/2/