2012-10-29 26 views
6

使用滤镜的我刚开始用crossfilter和d3.js ......我想在API参考给定的一些片断......我有以下数据在Crossfilter

var payments = crossfilter([ 
    {date: "2011-11-14T16:17:54Z", quantity: 2, total: 190, tip: 100, type: "tab"}, 
    {date: "2011-11-14T16:20:19Z", quantity: 2, total: 190, tip: 100, type: "tab"}, 
    {date: "2011-11-14T16:28:54Z", quantity: 1, total: 300, tip: 200, type: "visa"}, 
    {date: "2011-11-14T16:30:43Z", quantity: 2, total: 90, tip: 0, type: "tab"}, 
    {date: "2011-11-14T16:48:46Z", quantity: 2, total: 90, tip: 0, type: "tab"}, 
    {date: "2011-11-14T16:53:41Z", quantity: 2, total: 90, tip: 0, type: "tab"}, 
    {date: "2011-11-14T16:54:06Z", quantity: 1, total: 100, tip: 0, type: "cash"}, 
    {date: "2011-11-14T16:58:03Z", quantity: 2, total: 90, tip: 0, type: "tab"}, 
    {date: "2011-11-14T17:07:21Z", quantity: 2, total: 90, tip: 0, type: "tab"}, 
    {date: "2011-11-14T17:22:59Z", quantity: 2, total: 90, tip: 0, type: "tab"}, 
    {date: "2011-11-14T17:25:45Z", quantity: 2, total: 200, tip: 0, type: "cash"}, 
    {date: "2011-11-14T17:29:52Z", quantity: 1, total: 200, tip: 100, type: "visa"} 
]); 

我可以工作通过类型创建维度

var paymentsByTotal = payments.dimension(function(d) { return d.type; }); 

我的问题是,如何过滤字符串数组。我试过了:

paymentsByTotal.filterRange(["cash","visa"]); 

但是我没有得到预期的结果!

有什么建议吗?

回答

3

由于Crossfilter.js主分支中的源代码没有提供过滤器联合,所以您必须从Jason Davies' union branch获取代码。

然后,你应该可以做paymentsByTotal.filter("cash","visa");并获得所需的输出。

+0

感谢您的回答。 – selvagsz

2

出现一个filterFunction(function)已自上次响应加入,所以你现在可以做到这一点有:

paymentsByTotal.filterFunction(function(d) { return d === "visa" || d === "cash" }); 
1

如果你有值的数组,你不想指定明确的逻辑(即d === "visa"),那么你可以扩展sai的filterFunction解决方案来检查数值是否包含在你的数组中。这样做只是让事情变得更容易,如果您要过滤的项目数组很大或可能发生变化。

var items = ['Visa', 'Cash']; 
paymentsByTotal.filterFunction(function(d) { return items.indexOf(d) > -1;});