I'll attempt to at least point you in the right direction.
The following lines of code will return a sum of the commodity_id
's grouped by page_id
:
var dateDim = data.dimension(function(d) {return d.page_id;});
var hits = dateDim.group().reduceSum(function(d){return d.commodity_id;});
If a page had three commodities with the following commodity_id
's: [ 1, 2, 5 ]
reduceSum
would return the value 8 for that page (1+2+5=8); it would add each subsequent commodity_id
until it reached the end of the page.
Assuming that each entry in the dataset contains a commodity_id
that must be counted, you can get away with just grouping and using reduceCount
since you only care about the total number of commodities per page:
var hits = dateDim.group().reduceCount();
This will count how many times each page_id
appears in the database, which corresponds to the amount of commodity_id
's there are on that page.
If you would like to see the result of this grouping you can use:
hits.all();
This will return an array of objects Each object in this array contains a key
and a value
. The key
corresponds to the page_id
and the value
corresponds to the number of times that page_id
occurs in the dataset.
At this point, if you'd like to get the page_id
with the maximum value, you can use the top()
function. (group()
contains a top()
function just like dimension()
, which returns the group with the largest number of counts.) The following is how you can access the page_id
with the most entries in the database (which should correspond to the most amount of commodity_id
's) and the number of corresponding entries:
var page_id_with_most_entries = hits.top(1)[0].key;
var number_of_entries = hits.top(1)[0].value;
If you haven't already, check out the crossfilter.js API wiki:
https://github.com/square/crossfilter/wiki/API-Reference
I also have found the annotated source code of the DC.js Nasdaq Example shown on the DC.js page helpful:
http://nickqizhu.github.io/dc.js/docs/stock.html
(Just remove the "docs/stock.html" from the link to get to the Nasdaq Example.)