dc.js - min value from group per day

天涯浪子 提交于 2019-12-03 21:37:16
Ethan Jewett

You're going to need to keep a custom grouping. The basic idea is that you maintain an array (ordered is best) of all the values in a group. In your add function, you the current value to the array and assign the first value to a 'min' property of the group. In your remove function you remove values, you remove the current value from the array and then assign the first value to the 'min' property (and check if the list is empty, in which case set it to undefined or something along those lines).

You functions will look something like this:

function add(accessor) {
    var i;
    var bisect = crossfilter.bisect.by(function(d) { return d; }).left;
    return function (p, v) {
        // Not sure if this is more efficient than sorting.
        i = bisect(p.valueList, accessor(v), 0, p.valueList.length);
        p.valueList.splice(i, 0, accessor(v));
        p.min = p.valueList[0];
        return p;
    };
};
function remove(accessor) {
    var i;
    var bisect = crossfilter.bisect.by(function(d) { return d; }).left;
    return function (p, v) {
        i = bisect(p.valueList, accessor(v), 0, p.valueList.length);
        // Value already exists or something has gone terribly wrong.
        p.valueList.splice(i, 1);
        p.min = p.valueList[0];
        return p;
    };
};
function initial() {
    return function () {
        return {
            valueList: [],
            min: undefined
        };
    };
}

'accessor' is a function to get at the value you want the minimum of, like in reduceSum. Call each of these functions to return the function you use in the corresponding place in the .group(add, remove, initial) function.

This code is pretty much ripped straight out of reductio, so you could also use reductio and do:

var dateDim = facts.dimension(function (d) {return d3.time.day(d.date);}); var dateDimGroup = reductio().min(function (d) { return d.amount; })(dateDim.group());

Your dateDimGroup will then have a 'min' property that should give you the minimum amount for every date.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!