Dc.js - minimum value from group per day

I have the following data structure and want to display a lineChart string with a minimum "amount" (group) by Day (date) (dimension).

var data = [
 {date: "2014-01-01", amount: 10},
 {date: "2014-01-01", amount: 1},
 {date: "2014-01-15", amount: 0},
 {date: "2014-01-15", amount: 10 },
 {date: "2014-02-20", amount: 100 },
 {date: "2014-02-20", amount: 10 },
];

      

Where, as I usually do something in the following lines, I'm not sure how to find the mines in the group.

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

      

Is it possible? I can't seem to find any examples of this, so any help would be really appreciated.

Thank!

+3


source to share


1 answer


You will need to save your custom grouping. The basic idea is that you maintain an array (better ordered) of all values ​​in a group. In your add function, you get the current value of the array and assign the first value to the group's "min" property. In your delete 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 empty, in which case set it to undefined or something like the line).

Your 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 the value you want to use at least in reduceSum for example. Call each of these functions to return the function you are using at the appropriate place in the .group (add, remove, start) function.



This code is pretty much ripped straight from reductio , so you can 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 Dimgroup date will have a "min" property that should give you the minimum amount for each date.

+4


source







All Articles