1
votes

I want to make a query in crossfilter to filter all data from today's date.

Example: I have this query:

var countPerDim2 = Dim2.filterExact("1.1.2018").group().reduceSum();

So crossfilter will filter data from 1.1.2018.

But I want crossfilter to automatically get the the current date. My reason is that I want to draw two charts to compare them. Like :

Chart 1 by Date 1.1.2018 and Chart 2 by Date 31.12.17

How can I get the filter by yesterday's date? Is there a function like datenow() for the current day and maybe datenow(-1) for yesterday's date?

Thank you and happy new year!

1

1 Answers

1
votes

Today and yesterday

To answer your immediate question, JavaScript's new Date() will return the current time and date as a JavaScript Date object. Then you just need to convert the date object to a string to match your date format.

You can use d3.time.format to produce those strings. Looks like you would want something like

d3.time.format('%-d.%-m.%Y')

(or perhaps with m and d reversed - unclear from your example whether month or day comes first)

Yesterday is something like

var date = new Date();
date.setDate(date.getDate()-1);

See the JavaScript Date documentation for more details.

Comparing two days

However, I think you'll run into a more fundamental problem, which is that crossfilter doesn't have the concept of multiple filters, so it's hard to compare one date against another in side-by-side charts.

Off the top of my head, the best thing I can think of is to "freeze" a group using a fake group:

function freeze_group(group) {
    var _all = group.all().slice();
    return {
        all: function() {
            return _all;
        }
    };
}

You'd apply one date filter, then call

chart.group(freeze_group(group));

on the chart you want to have that date. Now it won't change when the filter changes, and you can apply the other date filter for other charts.