Crossfilter和DC.js:减少到唯一编号

时间:2017-07-31 12:08:10

标签: dc.js crossfilter

在下面的示例中,我试图通过Respond_Id的唯一出现来求和。例如。在这种情况下,它应该是3"Respond_Id" 258,261345

这是我的数据:

{"Respond_Id":258,"Gender":"Female","Age":"18-21","Answer":424},
{"Respond_Id":258,"Gender":"Female","Age":"18-21","Answer":428},
{"Respond_Id":261,"Gender":"Male","Age":"22-26", "Answer":427},
{"Respond_Id":261,"Gender":"Male","Age":"22-26",  "Answer":432},
{"Respond_Id":345,"Gender":"Female","Age":"27-30","Answer":424},
{"Respond_Id":345,"Gender":"Female","Age":"27-30","Answer":425},
{"Respond_Id":345,"Gender":"Female","Age":"27-30","Answer":433},

我知道我应该使用group reduce,所以我试过(改编自一个例子):

var ntotal =  answerDim.group().reduce(
                function(p, d) { 
                    if(d.Respond_Id in p.Respond_Ids){
                        p.Respond_Ids[d.Respond_Id]++;
                    }
                    else {
                        p.Respond_Ids[d.Respond_Id] = 1;
                        p.RespondCount++;
                    }
                    return p;
                },
                function (p, d) {
                    p.Respond_Ids[d.Respond_Id]--;
                    if(p.Respond_Ids[d.Respond_Id] === 0){
                        delete p.Respond_Ids[d.Respond_Id];
                        p.RespondCount--;
                    }
                    return p;
                },
                function () {
                    return {
                        RespondCount: 0,
                        Respond_Ids: {}
                    };

                }
            );

然后:

numberDisplay
         .group(ntotal)
         .valueAccessor(function(d){ return d.value.RespondCount; });

     dc.renderAll();

但似乎没有用。有人知道如何使它工作吗?谢谢

1 个答案:

答案 0 :(得分:0)

根据您的JSFiddle,您的设置如下:

var RespondDim       =  ndx.dimension(function (d) { return d.Respond_Id;});
var ntotal           =  RespondDim.group().reduce(
                                function(p, d) { 
                                    if(d.Respond_Id in p.Respond_Ids){
                                        p.Respond_Ids[d.Respond_Id]++;
                                    }
                                    else {
                                        p.Respond_Ids[d.Respond_Id] = 1;
                                        p.RespondCount++;
                                    }
                                    return p;
                                },
                                function (p, d) {
                                    p.Respond_Ids[d.Respond_Id]--;
                                    if(p.Respond_Ids[d.Respond_Id] === 0){
                                        delete p.Respond_Ids[d.Respond_Id];
                                        p.RespondCount--;
                                    }
                                    return p;
                                },
                                function () {
                                    return {
                                        RespondCount: 0,
                                        Respond_Ids: {}
                                    };
                                 });

这里需要注意的重要一点是,默认情况下,您的组密钥与维度密钥相同。因此,每个受访者ID将有一个组。这不是你想要的。

您可以切换到使用专为此用例设计的dimension.groupAll,但不幸的是dimension.groupAll.reduce签名略有不同。最简单的解决方法是将维度定义为具有单个值:

var RespondDim       =  ndx.dimension(function (d) { return true;});

现在您将看到ntotal.all()将如下所示:

{key: true, value: {RespondCount: 3, Respond_Ids: {258: 2, 261: 2, 345: 3}}}

工作小提琴:https://jsfiddle.net/v0rdoyrt/2/