Sam
Sam

Reputation: 515

barChart plot with time dimension

I would like to make a bar chart where x-axis is date e.g. 2017-08-12, and the y value is the number of data rows (i.e. records count) on the same date.

// get data into right format
var dateFormat = d3.time.format("%Y-%m-%d");
pageViews.forEach(function(d) {
    d['timestamp'] = dateFormat.parse(d['timestamp'].slice(0,10));
    d['timestamp'].setDate(1);
});

// Create Crossfilter instance
var cf = crossfilter(data);

var dateDim = cf.dimension(function(d) {return d['timestamp'];});

var numByDate = dateDim.group();

but if I do

console.log(numByDate.top(Infinity));

this returns 5 elements that are the first day of each month, so the group I have is in terms of Year-Month instead of Year-Month-Day, how can I resolve this?

I looked into this crossfilter.js: group data by month, but it did not work, after using .all() I still get the same thing back.

And I tried this:

var numByDate = dateDim.group(function(d) {return d['timestamp'];});

it just returns me an empty object.

Upvotes: 1

Views: 116

Answers (1)

Sam
Sam

Reputation: 515

So this is a result of my unfamiliarity with js and copy paste example code ...

The issue comes from misuse of the setDate method,

setDate(1) 

would just set the day of the month in the data to the first day, so the group method would only have several unique dates to work with.

Upvotes: 1

Related Questions