在crossfilter中创建一个运行总和

时间:2014-02-11 10:12:21

标签: d3.js sum crossfilter running-total dc.js

我正在尝试在crossfilter中创建一个运行总和以与dc.js一起使用。 我有一组如下记录:

records = [{"date":"2014-01-01","field1":"value1","field2":"value11","value_field":-20},
           {"date":"2014-01-02","field1":"value2","field2":"value12","value_field":100},
           {"date":"2014-01-03","field1":"value1","field2":"value11","value_field":-10},
           {"date":"2014-01-04","field1":"value2","field2":"value12","value_field":150},
           ]

到目前为止,我已经创建了一个与其他维度很好地匹配的barGraph,但我希望能够显示理论字段runnning_total的线图(沿着维度日期)。

要在crossfilter中完成它,我可以使用fieldx字段进行分组,并轻松获得相同的运行总图,但使用dc.js限制为一个子组值。

欢迎任何帮助。

2 个答案:

答案 0 :(得分:4)

由于您要按日期进行分组(根据date dimension),reduce()函数将用于执行按日期分组的聚合,根据日期中突出显示的单元格我的米老鼠示例如下:

Aggregating across fruits

运行总计时,您需要执行完全不同的操作,循环行:

enter image description here

您可以聚合数据,然后按如下方式追加正在运行的总计字段。我还提供了一个如何使用reduce函数计算平均值的示例:

records = [{ "date": "2014-01-01", "field1": "value1", "field2": "value11", "value_field": -20 },
    { "date": "2014-01-02", "field1": "value2", "field2": "value12", "value_field": 100 },
    { "date": "2014-01-03", "field1": "value1", "field2": "value11", "value_field": -10 },
    { "date": "2014-01-04", "field1": "value2", "field2": "value12", "value_field": 150 }
];


var cf = crossfilter(records);                                 

var dimensionDate = cf.dimension(function (d) {
        return d.date;
    });


function reduceAdd(p, v) {
    p.total += v.value_field;
    p.count++;
    p.average = p.total / p.count;
    return p;
}
function reduceRemove(p, v) {
    p.total -= v.value_field;
    p.count--;
    p.average = p.count ? p.total / p.count : 0;
    return p;
}
function reduceInitial() {
    return {
        total: 0,
        count: 0,
        average: 0,
    };
}



var average = dimensionDate.group().reduce(reduceAdd, reduceRemove, reduceInitial).all();

var averageWithRunningTotal = appendRuningTotal(average);  

function appendRuningTotal(average) {
    var len = average.length,
        runningTotal = 0;
    for (var i = 0; i < len; i++) {
        runningTotal += average[i].value.total;
        average[i].RunningTotal = runningTotal;
    }

    return average;
}

这会返回以下内容:

{"key":"2014-01-01","value":{"total":-20,"count":1,"average":-20},"RunningTotal":-20}
{"key":"2014-01-02","value":{"total":100,"count":1,"average":100},"RunningTotal":80}
{"key":"2014-01-03","value":{"total":-10,"count":1,"average":-10},"RunningTotal":70}
{"key":"2014-01-04","value":{"total":150,"count":1,"average":150},"RunningTotal":220}

答案 1 :(得分:2)

嗯,我知道操作系统已经构建了一个解决方案,但经过一段时间的努力,我能够破解它,所以如果其他人搜索它就在这里发布。

使用以下累计:https://groups.google.com/forum/#!topic/dc-js-user-group/W9AvkP_dZ0U

运行总和:

$(window).scroll(function() {

    var timerDuration = 250; // In milliseconds
    clearTimeout($.data(this, 'scrollTimer'));
    $.data(this, 'scrollTimer', setTimeout(function() {
        // do something
         var x = $(window).scrollTop();
     console.log("Scrolling finished");
     if (x > 820) {
         $($('.entry').get(1)).addClass('expanded');
     }
     if (x > 1525) {
         $($('.entry').get(2)).addClass('expanded');
     }
    }, timerDuration));
});

};

for Trailing 12个月计算:

var _group   = dim.group().reduceSum(function(d) {return 1;});

var group = {
all:function () {
 var cumulate = 0;
 var g = [];
 _group.all().forEach(function(d,i) {
   cumulate += d.value;
   g.push({key:d.key,value:cumulate})
 });
 return g;
}