如何使用crossfilter按类别返回唯一值的数量? [英] How to return the number of unique values by category using crossfilter?

查看:404
本文介绍了如何使用crossfilter按类别返回唯一值的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面我把给定的数组,并创建目录的维度。我那么为了范畴内获得的计数使用组()和reduceCount()。不过,我想获得独特的独特的计数的类别,而不是简单的计数。

下面,A有2唯一的ID,B有4个唯一的ID,和C有5个唯一的ID。

  VAR数据= [{
    分类:A,
    编号:1
},{
    分类:A,
    编号:1
},{
    分类:A,
    编号:1
},{
    分类:A,
    编号:2
},{
    分类:A,
    编号:2
},{
    分类:B,
    编号:1
},{
    分类:B,
    编号:1
},{
    分类:B,
    编号:1
},{
    分类:B,
    编号:2
},{
    分类:B,
    ID:3
},{
    分类:B,
    ID:3
},{
    分类:B,
    ID:3
},{
    分类:B,
    编号:4
},{
    类别:C,
    编号:1
},{
    类别:C,
    编号:2
},{
    类别:C,
    ID:3
},{
    类别:C,
    编号:4
},{
    类别:C,
    ID:5
}];

下面显示crossfilter独特计数

  VAR NDX = crossfilter(数据);VAR XDimension = ndx.dimension(功能(D){
    返回d.Category;
});
变种y尺寸= XDimension.group()。reduceCount(功能(D){
    返回d.Category;
});

它返回此

 [对象的对象](3)= [
    {关键:B,价值:8},
        {关键:C,价值:5},
        {关键:A,价值:5}
    ]

但我想它返回这样的:

 [对象的对象](3)= [
    {关键:B,值:4},
    {关键:C,价值:5},
    {关键:A,价值:2}
]


解决方案

您需要一个定制减速追踪你以前见过的ID属性值是什么。你可以这样做你自己或者你可以使用像反证法库。还有就是如何做到这一点的自述一个例子。我称之为异常聚集。

Below I take the given array and create a dimension for Category. I then use group() and reduceCount() in order to get the count within Category. However, I'd like to get the unique unique count for the category instead of simply the count.

Below, A has 2 unique IDs, B has 4 unique IDs, and C has 5 unique IDs.

var data = [{
    Category: "A",
    ID: "1"
}, {
    Category: "A",
    ID: "1"
}, {
    Category: "A",
    ID: "1"
}, {
    Category: "A",
    ID: "2"
}, {
    Category: "A",
    ID: "2"
}, {
    Category: "B",
    ID: "1"
}, {
    Category: "B",
    ID: "1"
}, {
    Category: "B",
    ID: "1"
}, {
    Category: "B",
    ID: "2"
}, {
    Category: "B",
    ID: "3"
}, {
    Category: "B",
    ID: "3"
}, {
    Category: "B",
    ID: "3"
}, {
    Category: "B",
    ID: "4"
}, {
    Category: "C",
    ID: "1"
}, {
    Category: "C",
    ID: "2"
}, {
    Category: "C",
    ID: "3"
}, {
    Category: "C",
    ID: "4"
}, {
    Category: "C",
    ID: "5"
}];

The following shows the unique counts of crossfilter

var ndx = crossfilter(data); 

var XDimension = ndx.dimension(function (d) {
    return d.Category;
});
var YDimension = XDimension.group().reduceCount(function (d) {
    return d.Category;
});

It returns this

"[object Object](3) = [
    {"key":"B","value":8},
        {"key":"C","value":5},
        {"key":"A","value":5}
    ]"

but I'd like it to return this:

"[object Object](3) = [
    {"key":"B","value":4},
    {"key":"C","value":5},
    {"key":"A","value":2}
]"

解决方案

You need a custom reducer to track what values in the ID property you've seen before. You can do this yourself or you could use a library like reductio. There is an example of how to do this in the readme. I'm calling it "exception aggregation".

这篇关于如何使用crossfilter按类别返回唯一值的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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