0
votes

I have the following data structure and I'm displaying information in the form of a few charts. I would like to have a time chart that displays Days on the X axis and just the top min_val for that day of the Y.

date               type  value  min_val_day
01/12/13 10:00:00   A     10      10
01/12/13 12:00:00   A     20      10
02/12/13 10:00:00   A     15      15
02/12/13 11:00:00   A     28      15

I do not want to reduceCount or reduceSum the values, I would like the result to be the actual value in my table. So 01/12/13 would show the Y axis value as 10, 02/12/13 would be 15.

I'm sure this is simple but I cannot get my head around it! This is what I'm using now and it's not right as it's reduceSum

var mindayDim = facts.dimension(function (d) {return d3.time.day(d.date); });
var mindayDimGroup = mindayDim.group().reduceSum(function (d) { return d.min_val_day; });

Thanks for any help!

1
When you say "Top min_val for that day", do you mean the maximum value? If Jan 12 had min_val_day rows with 10, 15, 20 (instead of 10, 10) - do you want it to return 20? If so, I believe you will need a custom reduce function that stores the sorted values in some way. Will you be processing many records per day? - DJ Martin

1 Answers

1
votes

Assuming you mean MAX value, here is an example:

priceGroup = priceDimension.group().reduce(
            function (p, v) {
                ++p.count;
                if(p.maxCompetitivePrice < v.competitive_price)
                    p.maxCompetitivePrice = v.competitive_price;
                return p;
            },
            null,
            function () {
                return { count:0, maxCompetitivePrice: 0};
            }
        );

The second function that is passed to reduce is null, which forces the grouping to recalculate the entire dataset on reduce. This was acceptable for my small dataset but should be used with caution.

The alternative approach is to store a sorted javascript structure of some sort and to maintain the sorted structure on both add and remove.