如何找到每天的最低价值?维度为天,组为最小量。 使用以下数据作为示例 -
var data = [
{date: "2011-11-14T10:17:54Z", amount: 10 },
{date: "2011-11-14T12:20:19Z", amount: 1 },
{date: "2011-11-14T14:20:19Z", amount: 0 },
{date: "2011-11-15T06:30:43Z", amount: 10 },
{date: "2011-11-15T10:30:43Z", amount: 10 },
{date: "2011-11-15T16:28:54Z", amount: 100 },
{date: "2011-11-16T18:48:46Z", amount: 100 },
{date: "2011-11-16T20:53:41Z", amount: 11 },
{date: "2011-11-16T22:54:06Z", amount: 10 },
];
所以结果应该是 -
{date: "2011-11-14T14:20:19Z", amount: 0 },
{date: "2011-11-15T06:30:43Z", amount: 10 },
{date: "2011-11-15T10:30:43Z", amount: 10 },
{date: "2011-11-16T22:54:06Z", amount: 10 },
我试图解决这个问题,我现在完全糊涂了,所以任何帮助都会受到赞赏!
感谢。
答案 0 :(得分:0)
以下内容将为您提供绝对最低要求。虽然不适用于过滤!
var data = [
{date: "2011-11-14T10:17:54Z", amount: 10 },
{date: "2011-11-14T12:20:19Z", amount: 1 },
{date: "2011-11-14T14:20:19Z", amount: 0 },
{date: "2011-11-15T06:30:43Z", amount: 10 },
{date: "2011-11-15T10:30:43Z", amount: 10 },
{date: "2011-11-15T16:28:54Z", amount: 100 },
{date: "2011-11-16T18:48:46Z", amount: 100 },
{date: "2011-11-16T20:53:41Z", amount: 11 },
{date: "2011-11-16T22:54:06Z", amount: 10 },
];
var ndx = crossfilter(data);
var dayDim = ndx.dimension(function (d) { return "" + d.date.substr(8,2); });
var minGroup = dayDim.group().reduce(
function (p, v) { if(v.amount < p || p === null) p = v.amount; return p; },
function (p, v) { return p; },
function () { return null; }
);
console.table(minGroup.top(Infinity));
如果要进行过滤,则需要保留每个数组中找到的所有值的排序数组,然后在过滤和未过滤数据时添加/删除值。然后使用这个排序的数组来计算出你的最小值。类似于以下内容(未经测试):
var data = [
{date: "2011-11-14T10:17:54Z", amount: 10 },
{date: "2011-11-14T12:20:19Z", amount: 1 },
{date: "2011-11-14T14:20:19Z", amount: 0 },
{date: "2011-11-15T06:30:43Z", amount: 10 },
{date: "2011-11-15T10:30:43Z", amount: 10 },
{date: "2011-11-15T16:28:54Z", amount: 100 },
{date: "2011-11-16T18:48:46Z", amount: 100 },
{date: "2011-11-16T20:53:41Z", amount: 11 },
{date: "2011-11-16T22:54:06Z", amount: 10 },
];
var ndx = crossfilter(data);
var dayDim = ndx.dimension(function (d) { return "" + d.date.substr(8,2); });
var minGroup = dayDim.group().reduce(
function (p, v) {
if(v.amount < p.min || p.min === null) {
p.min = v.amount;
}
p.values.push(v.amount);
return p;
},
function (p, v) {
p.values.splice(p.values.indexOf(v.amount),1);
if(v.amount === p.min) {
p.values.sort(function(a,b) { return a < b ? -1 : 1; });
p.min = p.values[0];
}
return p;
},
function () { return { min: null, values: [] }; }
);
console.log(minGroup.top(Infinity));