dc.js-每天从组中获取的最小值 [英] dc.js - min value from group per day

查看:49
本文介绍了dc.js-每天从组中获取的最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下数据结构,并希望显示按日期(日期)(维度)的最小值为金额(组)的折线图。

I have the following data structure and would like to display a lineChart with the minimum value of 'amount' (group) by Day (of 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 would normally be doing something along the following lines, I'm not sure how to find the min 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 this possible? I can't seem to find any examples of this so any help would be really appreciated.

谢谢!

推荐答案

您将需要保留自定义分组。基本思想是维护一个组中所有值的数组(最好是有序的)。在添加函数中,将当前值分配给数组,然后将第一个值分配给组的 min属性。在删除函数中,您删除值,然后从数组中删除当前值,然后将第一个值分配给'min'属性(并检查列表是否为空,在这种情况下,将其设置为undefined或类似的内容)

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).

您的函数将如下所示:

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'是一个函数,用于获取所需的最小值,就像在reduceSum中一样。调用这些函数中的每一个,以返回您在.group(添加,删除,初始)函数中相应位置使用的函数。

'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());

然后,您的dateDimGroup将具有 min属性,该属性应为您提供每个日期的最小金额。

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

这篇关于dc.js-每天从组中获取的最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆