将过滤器从一个Crossfilter数据集应用到另一个Crossfilter [英] Apply Filter from one Crossfilter dataset to another Crossfilter

查看:150
本文介绍了将过滤器从一个Crossfilter数据集应用到另一个Crossfilter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据集具有相似的列/尺寸,但按行不同,并包含不同的度量。

I have two datasets that have similar columns/dimensions but are grouped differently by row and contain different measures.

例如:

资料集1

Year   Category   SubCategory    Value01    Value02
2000   Cars       Sport          10         11
2000   Cars       Family         15         16
2000   Boats      Sport          20         21
2000   Boats      Family         25         26
...

资料集2

Year   Category    ValueA     ValueB
2000   Cars        100        101
2000   Boats       200        201
...

数据集1有自己的crossfilter对象,Dataset 2有一个单独的crossfilter对象。我有多个dc.js图表​​,一些绑定到数据集1,一些绑定到数据集2.

Dataset 1 has its own crossfilter object, Dataset 2 has a separate crossfilter object. I have multiple dc.js charts, some tied to the dataset 1, some to dataset 2.

当dc.js图表​​过滤列/维上的数据集1也存在于数据集2中,我想将相同的过滤器应用于数据集2.如何实现?

When a dc.js chart filters dataset 1 on a column/dimension that also exists in dataset 2, I want to apply that same filter to dataset 2. How can this be achieved?

推荐答案

't认为有任何自动的方法来做这个crossfilter或dc.js.但是如果你愿意滚动你自己的维度包装,你可以提供它,而不是原始的维度对象,并转发到所有底层维度。

I don't think there is any automatic way to do this in crossfilter or dc.js. But if you're willing to roll your own dimension wrapper, you could supply that instead of the original dimension objects and have that forward to all the underlying dimensions.

编辑:基于@ Aravind的小调,这里是一个维度镜像,至少对于这个简单的例子:

based on @Aravind's fiddle below, here is a "dimension mirror" that works, at least for this simple example:

function mirror_dimension() {
    var dims = Array.prototype.slice.call(arguments, 0);
    function mirror(fname) {
        return function(v) {
            dims.forEach(function(dim) {
                dim[fname](v);
            });
        };
    }
    return {
        filter: mirror('filter'),
        filterExact: mirror('filterExact'),
        filterRange: mirror('filterRange'),
        filterFunction: mirror('filterFunction')
    };
}

这有点乱。对于要从​​交叉过滤器A到交叉过滤器B镜像的每个维度,您需要在交叉过滤器B上创建一个镜像维度,反之亦然:

It's a bit messy using this. For each dimension you want to mirror from crossfilter A to crossfilter B, you'll need to create a mirror dimension on crossfilter B, and vice versa:

// Creating the dimensions
subject_DA = CFA.dimension(function(d){ return d.Subject; });
name_DA = CFA.dimension(function(d){ return d.Name; });
// mirror dimensions to receive events from crossfilter B
mirror_subject_DA = CFA.dimension(function(d){ return d.Subject; });
mirror_name_DA = CFA.dimension(function(d){ return d.Name; });

subject_DB = CFB.dimension(function(d){ return d.Subject; });
name_DB = CFB.dimension(function(d){ return d.Name; });
// mirror dimensions to receive events from crossfilter A
mirror_subject_DB = CFB.dimension(function(d){ return d.Subject; });
mirror_name_DB = CFB.dimension(function(d){ return d.Name; });

现在,将它们传递到图表时,将它们绑定在一起:

Now you tie them together when passing them off to the charts:

// subject Chart
subjectAChart
    .dimension(mirror_dimension(subject_DA, mirror_subject_DB))
    // ...

// subject Chart
subjectBChart
    .dimension(mirror_dimension(subject_DB, mirror_subject_DA))
    // ...

nameAChart
    .dimension(mirror_dimension(name_DA, mirror_name_DB))
    // ...

nameBChart
    .dimension(mirror_dimension(name_DB, mirror_name_DA))
    // ...

由于所有图表在同一图表组上,重绘事件将在它们之间自动传播,当它们被过滤时。

Since all the charts are implicitly on the same chart group, the redraw events will automatically get propagated between them when they are filtered. And each filter action on one crossfilter will get applied to the mirror dimension on the other crossfilter.

也许不是我会建议的 做的事,

Maybe not something I'd recommend doing, but as usual, it can be made to work.

这里是小提琴: https://jsfiddle.net/gordonwoodhull/7dwn4y87/8/

这篇关于将过滤器从一个Crossfilter数据集应用到另一个Crossfilter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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