您好我在从我的数据文件中过滤掉最新数据时遇到了问题。 我有以下数据
var data=
[
{"id":1,"speed":50,time:10:51.30},
{"id":1,"speed":40,time:10:51.40},
{"id":1,"speed":60,time:10:51.50},
{"id":1,"speed":55,time:10:51.55},
{"id":2,"speed":55,time:10:51.50},
{"id":2,"speed":65,time:10:51.58}
]
我想过滤掉数据,以便在最近的时间内显示或显示数据。所以我的过滤数据应包含以下内容
var filtereddata=
[
{"id":1,"speed":55,time:10:51.55},
{"id":2,"speed":65,time:10:51.58}
]
如何使用crossfilter从数据中获取过滤后的数据? 我正在尝试
var ndx=crossfilter(data);
var dim=ndx.dimension(function(d){return d.time;});
var filter=dim.filter(function(d){d3.max(data,function(d){return d.time;})});
但它不起作用?我该怎么办?
答案 0 :(得分:2)
问题是您正在查看过滤器对象。您需要使用顶部或底部将过滤的dim转换为数组。
请参阅下面的代码或更好地查看Here的工作版本。
var data=[
{id:1,speed:50, time: new Date('2011-04-11T11:51:00')},
{id:2,speed:40, time: new Date('2011-04-11T11:51:10')},
{id:3,speed:60, time: new Date('2011-04-11T11:51:20')},
{id:4,speed:51, time: new Date('2011-04-11T11:51:30')},
{id:5,speed:55, time: new Date('2011-04-11T11:51:40')},
{id:6,speed:65, time: new Date('2011-04-11T11:51:50')}];
var ndx = crossfilter(data);
var dataByTime = ndx.dimension(function (d) {
return d.time;
});
var dataBySpeed = ndx.dimension(function (d) {
return d.speed;
});
//var speedFilter = dataBySpeed.filter(function (d) {});
var timeFilter = dataByTime.filter(function(d){});
//console.log(speedFilter.filterRange([40, 55]).top(3));
console.log(timeFilter.filterRange([new Date('2011-04-11T11:51:00'), new Date('2011-04-11T11:51:40')]).top(3));
<强> ______ UPDATE _____ 强>
好的,我明白你的意思了。请参阅下面的更新代码段。我还更新了solution at jsfiddle
var data=[
{id:1,speed:50, time: new Date('2011-04-11T11:51:00')},
{id:2,speed:40, time: new Date('2011-04-11T11:51:10')},
{id:2,speed:60, time: new Date('2011-04-11T11:51:20')},
{id:3,speed:51, time: new Date('2011-04-11T11:51:30')},
{id:3,speed:55, time: new Date('2011-04-11T11:51:40')},
{id:3,speed:65, time: new Date('2011-04-11T11:51:50')}];
var uniqueVals = new Map();
data.forEach(function(d){
var existingVal = uniqueVals.get(d.id);
if (existingVal){
if (existingVal.time < d.time){
uniqueVals.set(d.id, d);
}
} else {
uniqueVals.set(d.id, d);
}
});
var finalData = [];
uniqueVals.forEach(function(d){ finalData.push(d); });
console.log(uniqueVals);