我正在尝试使用 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/
最佳答案
在我看来,您的数据中的“日期”属性是一个字符串,但您正在尝试使用日期对象进行过滤。准确吗?我认为这行不通。也许可以尝试一下
dimensionByDate.filter("2014-07-01T06:00:00.000Z");
关于javascript - 通过 Crossfilter 使用日期作为过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24268540/