简体   繁体   English

在dc.js仪表板的一个交叉过滤器维度中使用多个字段

[英]Using multiple fields in one crossfilter dimension for a dc.js dashboard

In my dataset I have 4 columns which are formatted like this: 在我的数据集中,我有4列,其格式如下:

data-example 数据示例

Basically we are following patients throughout the time and as soon as the event occurs, all the values from that timeslot till the end will be true. 基本上,我们一直在跟踪患者,一旦事件发生,从该时隙到结束的所有值都是真实的。 So to see the percentage of survival at a certain time I need to see the amount of people that have value 'false' at that moment in time I am trying to get this into a linechart (dc.js) where the x-as represent time and y-axis represents percentage of dataset that contains 'false'(at that time) 因此,要查看某个时间的生存百分比,我需要查看当时具有“假”值的人数,我试图将其放入折线图(dc.js),其中x-as表示时间和y轴表示包含“ false”(当时)的数据集的百分比

I tried first doing this with 4 different dimensions, 1 for each field. 我首先尝试使用4个不同的维度(每个字段1个)进行此操作。 This did not work correctly. 这不能正常工作。

I assume i need to get these 4 fields into one dimension. 我假设我需要将这4个字段整合为一个维度。

i have a printfilter function to check if my dimensions and groups work correctly: 我有一个printfilter函数来检查我的尺寸和组是否正常工作:

function print_filter(filter){
        var f=eval(filter);
        if (typeof(f.length) != "undefined") {}else{}
        if (typeof(f.top) != "undefined") {f=f.top(Infinity);}else{}
        if (typeof(f.dimension) != "undefined") {f=f.dimension(function(d) { return "";}).top(Infinity);}else{}
        console.log(filter+"("+f.length+") = "+JSON.stringify(f).replace("[","[\n\t").replace(/}\,/g,"},\n\t").replace("]","\n]"));
    }; 

When i call this filter on myDimension.group.reduceCount() i want the output to be as following: 当我在myDimension.group.reduceCount()上调用此过滤器时,我希望输出如下:

[{0 : 100}, {13: 48}, {26 : 60}, {52: 72}] 

the first item in the object is the time in weeks and the second item is the percentage of people with false at that time 对象中的第一项是以星期为单位的时间,第二项是当时有错误的人所占的百分比

How should i set up the dimensions and groups to achieve this output? 我应该如何设置维度和组以实现此输出?

UPDATE: tried using the following code which did not work either: 更新:尝试使用下面的代码也不起作用:

var recDim = {
                            x0: cf1.dimension(function(d){return d.recidiefNa0;}),
                            x13: cf1.dimension(function(d){return d.recidiefNa13;}),
                            x26: cf1.dimension(function(d){return d.recidiefNa26;}),
                            x52: cf1.dimension(function(d){return d.recidiefNa52;})
                         };
            var recGroup =  {
                                0: recDim.x0.group(),
                                13: recDim.x13.group(),
                                26: recDim.x26.group(),
                                52: recDim.x52.group()
                            } ;

          print_filter(recGroup); 

UPDATE 2 : I tried using 4 dimensions instead and making a composite linechart. 更新2:我尝试使用4个尺寸来代替,并制作一个复合的折线图。 This is the current code. 这是当前代码。 It calculates and renders the datapoints at the right place put because its 4 different chart is doenst show the correct line 它计算并在正确位置放置数据点,因为它的4个不同的图表确实显示了正确的线

var recDim0 = cf1.dimension(function(d){ return [d.recidiefNa0]});
            var recDim13 = cf1.dimension(function(d){return [d.recidiefNa13]});
            var recDim26 = cf1.dimension(function(d){return [d.recidiefNa26]});
            var recDim52 = cf1.dimension(function(d){return [d.recidiefNa52]});

            var recGroup0 = recDim0.group();
            var recGroup13 = recDim13.group();
            var recGroup26 = recDim26.group();
            var recGroup52 = recDim52.group();

            var scChart = dc.compositeChart("#scStepChart");
            scChart
            .width(600)
            .height(400)
            .x(d3.scale.linear().domain([0,52]))
            .y(d3.scale.linear().domain([0,100]))
            .clipPadding(10)
            .brushOn(false)
            .xAxisLabel("tijd in weken")
            .yAxisLabel("percentage vrij van residu/recidief")
            .compose([
                dc.lineChart(scChart)
                    .dimension(recDim0)
                    .group(recGroup0)
                    .renderDataPoints(true)
                    .renderTitle(false)
                    .keyAccessor(function(d) {return 0;})
                    .valueAccessor(function(d){return d.value/cf1.groupAll().reduceCount().value()*100;}),
                dc.lineChart(scChart)
                    .dimension(recDim13)
                    .group(recGroup13)
                    .renderDataPoints(true)
                    .renderTitle(false)
                    .colors(['blue'])
                    .keyAccessor(function(d){return 13;})
                    .valueAccessor(function(d){return d.value/cf1.groupAll().reduceCount().value()*100;}),
                dc.lineChart(scChart)
                    .dimension(recDim26)
                    .group(recGroup26)
                    .renderDataPoints(true)
                    .renderTitle(false)
                    .colors(['blue'])
                    .keyAccessor(function(d){return 26;})
                    .valueAccessor(function(d){return d.value/cf1.groupAll().reduceCount().value()*100;}),
                dc.lineChart(scChart)
                    .dimension(recDim52)
                    .group(recGroup52)
                    .renderDataPoints(true)
                    .renderTitle(false)
                    .colors(['blue'])
                    .keyAccessor(function(d){return 52;})
                    .valueAccessor(function(d){return d.value/cf1.groupAll().reduceCount().value()*100})


            ])                    
            .xAxis().ticks(4);
            scChart.render();

This is the result , as you can see the line isn't showing correctly. 这是结果,如您所见,该行显示不正确。

http://postimg.org/image/gitdw1yj9/ http://postimg.org/image/gitdw1yj9/

In the comments are the links for JSFiddle and CodePen 注释中是JSFiddle和CodePen的链接

Fixed the issue with help of @Gordon. 在@Gordon的帮助下解决了该问题。 changed my dataformat back to one field: with the number of weeks as value. 将我的数据格式改回一个字段:以星期数为值。

using this fakegroup i was able to get the graph correct: 使用这个fakegroup,我能够得到正确的图形:

function accumulate_subtract_from_100_and_prepend_start_point_group(source_group) {
return {
    all:function () {
        var cumulate = 100;
        var result = [];
        return [{key: 0, value: cumulate}]
           .concat(source_group.all().map(function(d) {
              cumulate -= d.value;
              return {key:d.key, value:cumulate};
           }));
    }
};

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM