3
votes

I am wondering how to return the max value from a dc.js group.

For example:

  var dateDim = data.dimension(function(d) {return d.page_id;});
  var hits = dateDim.group().reduceSum(function(d) {return d.commodity_id;}); 

This will return a bar chart plotting page_ids on the X axis, against commodity_ids on the Y axis.

However, I would like it to return the amount of commodity_ids on that particular page on the Y axis.

Would it be accomplished with something similar to the max and min dimension functionality?

 var minDate = dateDim.bottom(1)[0].page_id;
 var maxDate = dateDim.top(1)[0].page_id;

Apologies if the description is unclear.

Thanks

1
with finding max it is clear, but how to return min value on the group? Since hits.bottom does not exists...Dmitry Buzolin
If the question was how to obtain the minimum/maximum value of the rows within each group, I've just added a FAQ and an exampleGordon

1 Answers

2
votes

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.)