我刚开始使用crossfilter和d3.js ...我正在尝试API参考中给出的一些片段...我有以下数据
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"]);
但我没有得到预期的结果!
有什么建议吗?
答案 0 :(得分:3)
使用Crossfilter.js主分支中的源代码,没有提供过滤器联合,您必须从Jason Davies' union branch获取代码。
然后,您应该能够paymentsByTotal.filter("cash","visa");
并获得所需的输出。
答案 1 :(得分:2)
显示自上次回复以来已添加filterFunction(function)
,因此您现在可以执行以下操作:
paymentsByTotal.filterFunction(function(d) { return d === "visa" || d === "cash" });
答案 2 :(得分:1)
如果您有一个值数组,并且您不想为每个项指定显式逻辑(即d === "visa"
),那么您可以扩展sai的filterFunction
解决方案以检查值是否为包含在您的数组中。如果要过滤的项目数组很大或可能会发生变化,这样做会让事情变得更容易。
var items = ['Visa', 'Cash'];
paymentsByTotal.filterFunction(function(d) { return items.indexOf(d) > -1;});