使用crossfilter在JavaScript中动态返回结果

使用crossfilter在JavaScript中动态返回结果,javascript,json,crossfilter,Javascript,Json,Crossfilter,我觉得crossfilter库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

我觉得crossfilter库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"}
]);

我能够返回与特定键(数量、总数等)匹配的记录,但我不知道如何返回与键/值对组合匹配的结果。例如,如何返回与数量大于1、总数等于90、小费等于0和制表符类型匹配的结果集?这就是我完全迷路的地方。

您可以为每个属性创建一个维度,然后使用您指定的相应筛选条件调用每个维度的筛选方法,如下所示

var payments_by_quantity = payments.dimension(function(d){return d.quantity}),
    payments_by_total = payments.dimension(function(d){return d.total}),
    payments_by_tip = payments.dimension(function(d){return d.tip}),
    payments_by_type = payments.dimension(function(d){return d.type});

payments_by_quantity.filter([1, Infinity]);
payments_by_total.filter(90);
payments_by_tip.filter(0);
payments_by_type.filter("tab");

payments_by_type.top(Infinity)

效果是累积的,因此最后一行实际上是所有维度中所有过滤器的所有值的结果

作为初学者,我发现上面的答案准确但不准确,即我没有得到任何或意外的结果(没有对slo jo的不尊重,但我是从初学者的角度写的,因为我是一个交叉过滤nube)。 问题是在调用某些筛选器之前需要清除筛选器(您需要使用更多种类扩展数据集,例如不同的提示、总计等,以了解我的意思)。输出到控制台对我有帮助

以下是有助于我理解的内容:

    var data = [
  {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: 222, total: 990, 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: 5, 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: 990, 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"}
];



<script type="text/javascript">

// questions: For instance, how would I return the result set that matched results with a quantity more than 1, a total equal 90, a tip equal 0 and a type of tab? 
// create dimensions for each attribute
var payments_by_quantity = payments.dimension(function(d){return d.quantity});
     payments_by_total = payments.dimension(function(d){return d.total}),
     payments_by_tip = payments.dimension(function(d){return d.tip}),
     payments_by_type = payments.dimension(function(d){return d.type});

//need top(Infinity) to print out contents of filtered items
var morethan1 = payments_by_quantity.filter([1, Infinity]).top(Infinity);
console.log("morethan1",morethan1);

var tot_eq_90 = payments_by_total.filter(90).top(Infinity);
console.log("tot_eq_90",tot_eq_90);

// clear filters. If not, the result below will still be filtered by totals = 90
payments_by_total.filterAll();

console.log("top1= biggest paymt qty:", payments_by_quantity.top(1));
payments_by_total.filterAll();
console.log("top2= biggest paymt qty:", payments_by_quantity.top(2));
payments_by_total.filterAll();

console.log("bottom paymt tip:", payments_by_tip.bottom(1));

var tip_eq_0 = payments_by_tip.filter(0).top(Infinity);
console.log("tip_eq_0",tip_eq_0);
payments_by_total.filterAll();

var typetab = payments_by_type.filter("tab").top(Infinity);
console.log("typetab",typetab);
payments_by_total.filterAll();

var typetab_i = payments_by_type.top(Infinity);
console.log("typetab+i",typetab_i);
var数据=[
{日期:“2011-11-14T16:17:54Z”,数量:2个,总数:190个,提示:100个,键入:“tab”},
{日期:“2011-11-14T16:20:19Z”,数量:2个,总数:190个,提示:100个,键入:“tab”},
{日期:“2011-11-14T16:28:54Z”,数量:1,总数:300,小费:200,类型:“visa”},
{日期:“2011-11-14T16:30:43Z”,数量:222,总数:990,提示:0,键入:“tab”},
{日期:“2011-11-14T16:48:46Z”,数量:2,总数:90,提示:0,键入:“tab”},
{日期:“2011-11-14T16:53:41Z”,数量:5,总数:90,提示:0,键入:“tab”},
{日期:“2011-11-14T16:54:06Z”,数量:1,总数:100,小费:0,键入:“现金”},
{日期:“2011-11-14T16:58:03Z”,数量:2,总数:90,提示:0,键入:“tab”},
{日期:“2011-11-14T17:07:21Z”,数量:2,总数:90,提示:0,键入:“tab”},
{日期:“2011-11-14T17:22:59Z”,数量:2,总数:990,提示:0,键入:“tab”},
{日期:“2011-11-14T17:25:45Z”,数量:2,总数:200,小费:0,类型:“现金”},
{日期:“2011-11-14T17:29:52Z”,数量:1,总数:200,小费:100,类型:“visa”}
];
//问题:例如,我如何返回与数量大于1、总数等于90、小费等于0和制表符类型匹配的结果集?
//为每个属性创建维度
var payments按数量=payments.dimension(函数(d){return d.quantity});
payments_by_total=payments.dimension(函数(d){return d.total}),
payments_by_tip=payments.dimension(函数(d){return d.tip}),
payments_by_type=payments.dimension(函数(d){return d.type});
//需要top(无穷大)来打印过滤项的内容
var morethan1=按数量付款。过滤器([1,无穷大])。顶部(无穷大);
console.log(“超过1”,超过1);
var tot_eq_90=按总额计算的付款。过滤器(90)。顶部(无限);
控制台日志(“tot_eq_90”,tot_eq_90);
//清除过滤器。如果没有,下面的结果仍将按总计=90进行过滤
按总额付款。过滤();
console.log(“top1=最大付款数量:”,按数量付款。top(1));
按总额付款。过滤();
console.log(“top2=最大付款数量:”,按数量付款。top(2));
按总额付款。过滤();
控制台日志(“底部付款提示:”,按提示底部付款(1));
var tip_eq_0=按tip.filter(0.top)(无限大)支付的款项;
console.log(“tip_eq_0”,tip_eq_0);
按总额付款。过滤();
var typetab=按付款类型筛选(“tab”).top(无限大);
console.log(“typetab”,typetab);
按总额付款。过滤();
var typetab_i=按_类型的付款。顶部(无限);
console.log(“typetab+i”,typetab\u i);