Using Custom reduce function with Fake Groups

110 views
Skip to first unread message

Robert Anderson

unread,
Jan 29, 2017, 11:06:29 PM1/29/17
to dc-js user group

I have line chart where I need to show frequency of order executions over the course of a day. These orders are grouped by time interval, for example every hour, using custom reduce functions. There could be an hour interval when there were no order executions, but I need to show that as a zero point on the line. I create a 'fake group' containing all the bins with a zero count...and the initial load of the page is correct.


However the line chart is one of 11 charts on the page, and needs to be updated when filters are applied to other charts. When I filter on another chart, the effects on this particular frequency line chart are incorrect. The dimension and the 'fake group' are used for the dc.chart.


I put console.log messages in the reduceRemove function and can see that there is something wrong...but not sure why.


Any thoughts on where I could be going wrong. The following code is how I set-up the dimension & group, and where I believe I am having the issue.


FrequencyVsTimeDimension = crossfilterData.dimension(function (d) { return d.execution_datetime; });

FrequencyVsTimeGroup = FrequencyVsTimeDimension.group(n_seconds_interval(interval));

FrequencyVsTimeGroup.reduce(
    function (p, d) { //reduceAdd
        if (d.execution_datetime in p.order_list) {
            p.order_list[d.execution_datetime] += 1;
        }
        else {
            p.order_list[d.execution_datetime] = 1;
            if (d.execution_type !== FILL) p.order_count++;
        }
        return p;
    },
    function (p, d) { //reduceRemove
        if (d.execution_type !== FILL) p.order_count--;

        p.order_list[d.execution_datetime]--;
        if (p.order_list[d.execution_datetime] === 0) {
            delete p.order_list[d.execution_datetime];
        }
        return p;
    },
    function () { //reduceInitial
        return { order_list: {}, order_count: 0 };
    }
);

var FrequencyVsTimeFakeGroup = ensure_group_bins(FrequencyVsTimeGroup, interval); // function that returns bins for all the intervals, even those without data.

Robert Anderson

unread,
Jan 30, 2017, 11:04:36 AM1/30/17
to dc-js user group
Issue resolved; logic flaw in the reduceRemove function

Gordon Woodhull

unread,
Jan 30, 2017, 11:43:44 AM1/30/17
to dc.js user group
I think it would be simpler to convert those datetimes to Date objects and use d3.time.hour for your dimension value function, plus chart.x(d3.time.scale()).xUnits(d3.time.hours)

Then you wouldn't need a custom reduction. Keeping track of that extra list of orders inside the groups is probably difficult to maintain, if you don't need that extra data.

But maybe I'm not looking at the whole problem.


-- 
You received this message because you are subscribed to the Google Groups "dc-js user group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dc-js-user-gro...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dc-js-user-group/28261873-d5d9-40fd-961a-f3cce6f61cc9%40googlegroups.com.

Robert Anderson

unread,
Jan 30, 2017, 12:14:55 PM1/30/17
to dc-js user group
Greatly appreciate the response.

Re: your comments, unfortunately, the requirement is a bit more complex, and that is why the custom reduction was required. There is a configurable interval slider on the page, and we need to be able to display intervals dynamically from 1 second up to 1 hour. 

Re: keeping track of orders inside the groups, that was to aid in debugging, and that actually turned out to be the source of my issue. I needed to separate the counting of orders from the tracking the orders list and the number of occurences of executions within orders. Once I separated that logic within the reduction, all turned out as expected

Of course, this requirement to be able to dynamically change intervals on this chart is problematic within the whole scope of the page, where I have 11 dc charts, and not all the interactions are working as needed, but if needed, I seek guidance on those in another post(s).  Thanks again.
To unsubscribe from this group and stop receiving emails from it, send an email to dc-js-user-group+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages