问题描述
让我说我有以下数据和维度:
Let me say that I have this data and dimension:
var data = [
{"fruit": "apple", "amount": "12"},
{"fruit": "orange", "amount": "6"},
{"fruit": "grape", "amount": "11"},
{"fruit": "melon", "amount": "26"},
{"fruit": "lemon", "amount": "15"}
]
var ndx = crossfilter(data);
var fruitDimension = ndx.dimension(function (d) {
return d.fruit;
});
...现在,我只想过滤苹果,柠檬和橙色 ,只需使用代码即可。现在,我正在尝试做类似的事情。
...and now, I want to filter just "apple","lemon" and "orange" just by using code. By now, I am trying to do something like.
fruitDimension.filter(["apple","lemon","orange"])
...但是它不能全部正常工作。
...but it does not work it all.
-
我知道函数# dimension.filterExact(value)适用于一个值。
-
如果我应用# dimension.filter(value),则将向量作为参数进行处理与它作为#Dimension.filterRange(范围)
If I apply # dimension.filter(value) passing a vector as parameter, it deals with it as # dimension.filterRange(range)
我找不到哪个过滤器适用于不同的值。
I could not find which filter works for different values.
引用来自:
有人暗示我可以做什么,以便过滤维度的不同元素(不遵循范围顺序)?
Someone has a hint about what I could be doing in order to filter different elements (that do not follow a range order) of a dimension?
在此先感谢
Roger
Thanks in advance, Roger
推荐答案
交叉过滤器为此需要一个自定义过滤器功能。 dc.js提供了一个。
Crossfilter requires a custom filter function for this. dc.js supplies one.
如果您使用的是dc图表,您应该通过图表的 filter
函数应用该过滤器,该函数的语法和语义与交叉过滤器维度过滤器不同:
If you're using a dc chart you should apply the filter via the chart's filter
function, which has different syntax and semantics from the crossfilter dimension filter:
chart.filter([["apple","lemon","orange"]]);
请注意多套括号。对吗?我不知道它是如何演变的。此外,dc会切换每个值,因此,如果要替换过滤器,请使用 replaceFilter
代替 filter
。
Note the extra set of brackets. Weird right? I do not know how it evolved this way. Also, dc will toggle each value, so if you want to replace the filter, use replaceFilter
instead of filter
.
这在,这是dc.js将过滤器应用于交叉过滤器的地方。
This is documented in the filterHandler
function, which is where dc.js applies filters to crossfilter.
图表不直接知道交叉过滤器维度的变化:如果使用图表的 .filter()
>然后图表将更新视觉选择。
The chart doesn't know about changes directly to the crossfilter dimension: if you use the chart's .filter()
then the chart will update the visual selection.
如果您想应用将多值过滤器直接转换为交叉过滤器维度,这是一个为值数组生成过滤器函数的函数:
If instead you want to apply a multivalue filter directly to a crossfilter dimension, here is a function that generates a filter function for an array of values:
function multivalue_filter(values) {
return function(v) {
return values.indexOf(v) !== -1;
};
}
fruitDimension.filterFunction(multivalue_filter(["apple","lemon","orange"]));
这篇关于如何在DC /交叉滤波器维度中过滤多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!