D3 Crossfilter尺寸的离散过滤器

kum*_*oda 5 d3.js crossfilter

有没有办法在具有一个或多个值的属性上创建维度?例如

{quantity: 2, total: 190, tip: 100, items: ["apple","sandwich"],
{quantity: 2, total: 190, tip: 100, items: ["ice-cream"]},
{quantity: 1, total: 300, tip: 200, items: ["apple", "coffee"]}
Run Code Online (Sandbox Code Playgroud)

我的目标是创建一个交叉过滤器,可以过滤掉具有序数值的维度的条目.有没有办法我写一个过滤器/尺寸,让我说"我想要所有条目'苹果''?

我能想到的唯一解决方法是为每个项目创建一个维度.像这样:

var paymentsByApple = payments.dimension(function(d) { return $.inArray("apple", d.items); });
var paymentsByCoffee = payments.dimension(function(d) { return $.inArray("coffee", d.items); });
// and one for every possible item
Run Code Online (Sandbox Code Playgroud)

主要问题是我不想枚举和硬编码所有不同的对象.而且,我最终可能会有很多可能的不同项目.有更聪明的方法吗?

提前致谢!

Pab*_*rro 2

改变你的数据模型怎么样?我认为使用:

[{id: 1, quantity: 1, total: 100, tip:  50, item: "apple"},
 {id: 1, quantity: 1, total:  90, tip:  50, item: "sandwich"},
 {id: 2, quantity: 1, total: 190, tip: 100, item: "ice-cream"},
 {id: 3, quantity: 1, total: 300, tip: 100, item: "apple"}, 
 {id: 3, quantity: 1, total: 300, tip: 100, item: "coffee"}]
Run Code Online (Sandbox Code Playgroud)

也许你可以使用reduceSum通过id计算总数