dc.js一次添加多个过滤器 [英] dc.js add multiple filters at once

查看:67
本文介绍了dc.js一次添加多个过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在DC图表上一次添加一组过滤器?例如,假设我有一个pieChart和要应用的一系列过滤器值。

Is it possible to add a set of filters at once on a dc chart? For instance, say I have a pieChart, and an array of filter values to apply.

var osChart = dc.pieChart('#oschart');

输入一组过滤器,例如

var filters = ["linux", "mac osx", "windows", "solaris"]

如何应用过滤器,以便仅生成一个过滤事件?
我可以做类似的事情

How can I apply filters so that only one "filtered" event is generated? I can do something like

for (var i=0; i < filters.length; i++) {
  osChart.filter(filters[i]);
}

但是,这将生成4个已过滤事件。
我正在根据用户在文本框中键入的内容应用过滤器。应用过滤器时,我正在进行一些Ajax调用,如果我一个接一个地应用过滤器,这往往会减慢速度。如果可以立即设置过滤器,则可以避免额外的ajax调用。

However that would generate 4 filtered event. I am applying filters based on what a user typed on a text box. When a filter is applied, I am making some ajax calls, and this tends to slow down if I apply filters one by one. I can avoid extra ajax calls if the filters can be set at once.

crossfilter具有函数 filterFunction 完成此任务,但我不确定如何将其应用于直流图表。在 osChart.dimension()上应用 filterFunction 无效。在最新的dc版本中,我看到了一些功能,例如 addFilterHandler removeFilterHandler ,但是我现在无法测试和部署该版本。

crossfilter has a function filterFunction which can get this task done, but I am not sure how I can apply that on a dc chart. Apply filterFunction on osChart.dimension() did not work. With the latest dc release, I saw some functions like addFilterHandler and removeFilterHandler however I cannot test and deploy that version right now.

我还有什么其他选择?

What other options do I have?

推荐答案

通过查看有些复杂的代码,可以将数组传递到另一个数组中 .filter()(或未记录但不真实的 .replaceFilter() ),而不会影响性能,因为它将应用所有过滤器,然后调用 filtered 事件。

From looking at the code, which is somewhat convoluted, you can pass the array inside another array to .filter() (or the undocumented but unmagical .replaceFilter()) without a performance penalty, because it will apply all of the filters before invoking the filtered event.

来自最新的 .filter()源,该源使用处理程序但具有相同的行为:

From the latest .filter() source, which uses handlers but has the same behavior:

    if (_ instanceof Array && _[0] instanceof Array && !_.isFiltered) {
        _[0].forEach(function (d) {
            if (_chart.hasFilter(d)) {
                _removeFilterHandler(_filters, d);
            } else {
                _addFilterHandler(_filters, d);
            }
        });
    } else if (_ === null) {
        _filters = _resetFilterHandler(_filters);
    } else {
        if (_chart.hasFilter(_)) {
            _removeFilterHandler(_filters, _);
        } else {
            _addFilterHandler(_filters, _);
        }
    }
    applyFilters();
    _chart._invokeFilteredListener(_);

因此,如果它找到一个没有 isFiltered 方法,并且该数组的第一个元素也是一个数组,它将迭代嵌套数组中的元素。

So if it finds an array that does not have an isFiltered method, and that array's first element is also an array, it will iterate over the elements in the nested array.

例如,传递 [[ linux, mac osx, windows, solaris]] 可以过滤这四个值。 (感谢@marcin进行澄清!)

For example, pass [["linux", "mac osx", "windows", "solaris"]] to filter on those four values. (Thanks @marcin for clarifying!)

这篇关于dc.js一次添加多个过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆