使用日期作为Crossfilter的过滤器

时间:2014-06-17 16:06:19

标签: javascript crossfilter

我正在尝试在非常大的数据集上使用Crossfilter JavaScript库。我已经简化了这个问题的数据。

var tempArray = [

    {"date": new Date(2013, 6),"dataval":400,"col1":"Test 1","col2":"Billy Recycling Group","col3":"Billy Recycling Group - Mobile"},
    {"date": new Date(2013, 6),"dataval":400,"col1":"Test 1","col2":"Billy Recycling Group","col3":"Billy Recycling Group - Mobile"},
    {"date": new Date(2013, 6),"dataval":400,"col1":"Test 1","col2":"Billy Recycling Group","col3":"Billy Recycling Group - Mobile"},
    {"date": new Date(2013, 6),"dataval":400,"col1":"Test 1","col2":"Billy Recycling Group","col3":"Billy Recycling Group - Mobile"},

    {"date": new Date(2013, 6),"dataval":600,"col1":"Test 2","col2":"James Recycling Group","col3":"James Recycling Group - Mobile"},
    {"date": new Date(2013, 6),"dataval":600,"col1":"Test 2","col2":"James Recycling Group","col3":"James Recycling Group - Mobile"},
    {"date":new Date(2013, 6),"dataval":600,"col1":"Test 2","col2":"James Recycling Group","col3":"James Recycling Group - Mobile"},
    {"date":new Date(2013, 6),"dataval":600,"col1":"Test 2","col2":"James Recycling Group","col3":"James Recycling Group - Mobile"},

    {"date":new Date(2014, 6),"dataval":200,"col1":"Test 1","col2":"Billy Recycling Group","col3":"Billy Recycling Group - Mobile"},
    {"date":new Date(2014, 6),"dataval":200,"col1":"Test 1","col2":"Billy Recycling Group","col3":"Billy Recycling Group - Mobile"},
    {"date":new Date(2014, 6),"dataval":200,"col1":"Test 1","col2":"Billy Recycling Group","col3":"Billy Recycling Group - Mobile"},
    {"date":new Date(2014, 6),"dataval":200,"col1":"Test 1","col2":"Billy Recycling Group","col3":"Billy Recycling Group - Mobile"},

    {"date":new Date(2014, 6),"dataval":300,"col1":"Test 2","col2":"James Recycling Group","col3":"James Recycling Group - Mobile"},
    {"date":new Date(2014, 6),"dataval":300,"col1":"Test 2","col2":"James Recycling Group","col3":"James Recycling Group - Mobile"},
    {"date":new Date(2014, 6),"dataval":300,"col1":"Test 2","col2":"James Recycling Group","col3":"James Recycling Group - Mobile"},
    {"date":new Date(2014, 6),"dataval":300,"col1":"Test 2","col2":"James Recycling Group","col3":"James Recycling Group - Mobile"}

]

//**********************************************************************************
// Creating Crossfilter
//**********************************************************************************
var data = crossfilter(tempArray);

//**********************************************************************************
// Getting Dimension by Company Name
//**********************************************************************************
var dimensionByName = data.dimension(function (d) {return d.col1})

//**********************************************************************************
// Getting Dimension by Date
//**********************************************************************************
var dimensionByDate = data.dimension(function (d){return d.date})

//**********************************************************************************
// Dimension by Company name that I can use to get Data
//**********************************************************************************
var dim = data.dimension(function (d) {return d.col1})

dimensionByDate.filter(new Date(2014, 6))

var thisMonthMeasure = dim.group().reduceSum(function (d){return d.dataval});
console.log('thisMonthMeasure', thisMonthMeasure.top(Infinity))

//**********************************************************************************
// Clearing Data Filter
//**********************************************************************************
dimensionByDate.filterAll();

dimensionByDate.filter(new Date(2013, 6))
var lastYearMeasure = dim.group().reduceSum(function (d){return d.dataval});
console.log('lastYearMeasure', lastYearMeasure.top(Infinity))

问题是我的代码会向控制台打印两次相同的数组而不是两个不同的数组。值完全相同,结果如下:

thisMonthMeasure等于[ { key: "Test 2", value : 3600 } , { key: "Test 1", value : 2400 } ]

lastYearMeasure等于[ { key: "Test 2", value : 3600 } , { key: "Test 1", value : 2400 } ]

...而不是下面的预期结果:

thisMonthMeasure等于[ { key: "Test 2", value : 1200 } , { key: "Test 1", value : 800 } ]

lastYearMeasure等于[ { key: "Test 2", value : 2400 } , { key: "Test 1", value : 1600 } ]

经过一些额外的测试后,似乎我的日期过滤器无效。

我应该如何按日期过滤?

链接到JSFiddle http://jsfiddle.net/TheMcMurder/u89Wv/

1 个答案:

答案 0 :(得分:0)

在我看来,您数据中的'date'属性是一个字符串,但您尝试使用Date对象进行过滤。那是准确的吗?我认为这不会起作用。也许试试

dimensionByDate.filter("2014-07-01T06:00:00.000Z");