交叉过滤器维度和组以过滤出低于特定阈值的数据 [英] Crossfilter dimension and group to filter out data below certain threshold

查看:56
本文介绍了交叉过滤器维度和组以过滤出低于特定阈值的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据,我试图绘制2个条形图:




  • 给定类型的总单位

  • 类型小于版本2.0的单位



两个图表中的x轴均为该类型



图表很好。问题是:



当我选择第二张图表上的条形图时,我希望仅看到版本<数据表和第一个图表中的2.0,在这种情况下为一个记录。但是我得到的是该特定类型的所有单位(如果我单击第一个图表中的条形,这就是我的期望),即3条记录。



请参见 http://jsfiddle.net/y1o52tk4/ 6 /



我想的问题是我如何分组版本图:

  var versionGroup = type1Dim.group()。reduceSum(dc.pluck('version_count')); 

数据:

  var data = [{{
version:1.0,
serial:'1A',
type: a
},{
版本:2.0,
序列: 2A,
类型: a
},{
版本:2.0,
serial:'2AA',
type: a
},{
version:2.0,
serial:'2B ',
type: b
},{
version:2.5,
serial:'25B',
type: b
},{
version:1.0,
serial:'1B',
type: b
}];


解决方案

如上面的评论所述,但让我们拼一下

您可以使用组合键,然后使用假组过滤组,就像在上面更新的小提琴中所做的那样,为了获得这种效果。这可能是最通用,最稳定的方法,而无需进行任何UI黑客攻击。



不过,关于交叉过滤器键的了解是,它总是会迫使他们遵循价值观。因此,如果您需要组合键,最好自己将其构造为字符串,因为如果将其作为数组传递,则crossfilter会将其强制为字符串,并为您提供一个, / code>-可以正常工作,我知道dc.js散点图可以做到这一点,但我不建议这样做。它比您自己创建字符串的效率低(因为您只会做一次而不是让它发生在crossfilter内部),并且因为您拥有更多的控制权。



以下是与杰森·戴维斯(Jason Davies)进行的长时间讨论,这使我相信:
https://github.com / square / crossfilter / pull / 48



因此,这是小提琴的字符串串联复合键版本。



首先使用组合键:

  var type1Dim = ndx.dimension(function(d){
if (d.version< 2.0)
返回 1。 + d.type;
否则
返回 2。 + d.type;});

(最简单的分隔符是'\x0',但'。'在这里工作正常。)



接下来,是一个普通键访问器:

  .keyAccessor(function(d){return d.key;})

我们也让dc.js推断序数域:

  .x(d3.scale.ordinal()); 

它可以工作,但是现在刻度线具有丑陋的复合键。要解决此问题:

  versionChart.xAxis()。tickFormat(function(d){
return d.split( '。')[1];
});

我的叉子: http://jsfiddle.net/gordonwoodhull/y09cok1z/4/



编辑:我忘了更新假组生成器,这在这里无关紧要,但是出于一般性考虑,它也应该拆分键:

  function removeVersion2(source_group) {
return {
all:function(){
return source_group.all()。filter(function(d){
return d.key.split('。') [0]!= 2;
});
}
};
}


I have the following data, and I am trying to plot 2 bar charts:

  • Total units of a given type
  • Units of a type less than version 2.0

Where x-axis in both the charts is the type of unit.

The charts are fine. The problem is:

When I select the bar on the 2nd chart I expect to see only units with version < 2.0 in the data table and the 1st chart i.e. one record in this case. But what I get is all the units of that particular type (which is what I expect if I click on a bar in first chart) i.e. 3 records.

See http://jsfiddle.net/y1o52tk4/6/

The problem I guess is with how I group for version chart:

var versionGroup = type1Dim.group().reduceSum(dc.pluck('version_count'));

Data:

var data = [{
         "version": 1.0,
         "serial": '1A',
         "type": "a"
},{
         "version": 2.0,
         "serial": '2A',
         "type": "a"
},{
         "version": 2.0,
         "serial": '2AA',
         "type": "a"
},{
         "version": 2.0,
         "serial": '2B',
         "type": "b"
},{
         "version": 2.5,
         "serial": '25B',
         "type": "b"
},{
         "version": 1.0,
         "serial": '1B',
         "type": "b"
}];

解决方案

As described in the comments above, but let's spell it out and clarify a couple of things.

You can use composite keys and then filter the group using a "fake group", as you did in your updated fiddle above, in order to get this effect. And this is probably the most general and most stable way to do this, without having to do any UI hacks.

The thing to understand about crossfilter keys, however, is that it is always going to coerce them to values. So if you want a composite key, it's best to just construct it as a string yourself, because if you pass it as a an array, crossfilter will coerce it to a string and give you a join with , - which can be made to work, and I am aware that the dc.js scatterplot does this, but I don't recommend it. It's less efficient than creating the strings yourself (because you will only do it once rather than having it happen inside crossfilter internals), and because you have a lot more control.

Here is a long discussion with Jason Davies which convinced me: https://github.com/square/crossfilter/pull/48

So here's a string concatenation composite key version of your fiddle.

First the composite key:

    var type1Dim = ndx.dimension(function(d){
                            if(d.version<2.0)
                                return '1.' + d.type;
                            else
                                return '2.' + d.type;});

(The most foolproof separator would be '\x0' but '.' works fine here.)

Next, a plain key accessor:

    .keyAccessor(function(d) {return d.key;})

And we let dc.js infer the domain of the ordinal scale too:

    .x(d3.scale.ordinal());

And it works, but now the ticks have the ugly composite key. To fix that:

    versionChart.xAxis().tickFormat(function(d) {
        return d.split('.')[1];
    });

My fork: http://jsfiddle.net/gordonwoodhull/y09cok1z/4/

EDIT: I forgot to update the fake group generator, which happens not to matter here, but for generality, it should split the key as well:

function removeVersion2(source_group) {
    return {
        all:function () {
            return source_group.all().filter(function(d) {
                return d.key.split('.')[0] != 2;
            });
        }
    };
}

这篇关于交叉过滤器维度和组以过滤出低于特定阈值的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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