Skip to content

Commit 4612e14

Browse files
committed
Refactoring code for simplicity
1 parent 6be5ab0 commit 4612e14

5 files changed

+50
-298
lines changed

crossfilter-ma.js

+24-148
Original file line numberDiff line numberDiff line change
@@ -205,45 +205,14 @@ crossfilterMA.accumulateGroupForNDayMovingAverage = function( sourceGroup, ndays
205205
var _this = this;
206206
var cumulate = 0;
207207
var all = sourceGroup.all();
208-
var fullDates = {};
209-
210-
var myDx = crossfilter( all );
211-
var myDimensionDate = myDx.dimension( function( d ) {
212-
return d.key;
213-
} );
214-
var myGroupingOnDate = myDimensionDate.group();
215-
216-
var reducer = myGroupingOnDate.reduce(
217-
function ( p, v ) {
218-
var selDate = _this.keyAccessor()( v );
219-
var selValue = _this.valueAccessor()( v );
220208

221-
if ( fullDates[ selDate ] ) {
222-
fullDates[ selDate ].myValue += selValue;
223-
} else {
224-
fullDates[ selDate ] = {
225-
myValue: selValue
226-
};
227-
}
228-
return p;
229-
},
230-
function ( p, v ) {
231-
var selDate = _this.keyAccessor()( v );
232-
var selValue = _this.valueAccessor()( v );
233-
234-
if ( fullDates[ selDate ] ) {
235-
fullDates[ selDate ].quantity -= selValue;
236-
} else {
237-
delete fullDates[ selDate ];
238-
}
239-
return p;
240-
},
241-
function () {
242-
fullDates = {};
209+
// Gather all values so we can compare against previous ones
210+
var fullDates = {};
211+
all.forEach(
212+
function( d ) {
213+
fullDates[ _this.keyAccessor()( d ) ] = _this.valueAccessor()( d );
243214
}
244215
);
245-
246-
reducer.all();
247216
var orderedDates = Object.keys( fullDates ).sort();
248217

249218
var accumulatedAll = all.map( function( d, i, arr ) {
@@ -272,7 +241,7 @@ crossfilterMA.accumulateGroupForNDayMovingAverage = function( sourceGroup, ndays
272241

273242
if ( targetDayId ) {
274243
var targetDayBlock = fullDates[ targetDayId ];
275-
var targetDayValue = targetDayBlock.myValue;
244+
var targetDayValue = targetDayBlock;
276245

277246
numsToAverage++;
278247
thisCumulate += targetDayValue;
@@ -322,45 +291,14 @@ crossfilterMA.accumulateGroupForNDayMovingAverage = function( sourceGroup, ndays
322291
var _this = this;
323292
var cumulate = 0;
324293
var all = sourceGroup.top.apply( sourceGroup, arguments );
325-
var fullDates = {};
326-
327-
var myDx = crossfilter( all );
328-
var myDimensionDate = myDx.dimension( function( d ) {
329-
return d.key;
330-
} );
331-
var myGroupingOnDate = myDimensionDate.group();
332-
333-
var reducer = myGroupingOnDate.reduce(
334-
function ( p, v ) {
335-
var selDate = _this.keyAccessor()( v );
336-
var selValue = _this.valueAccessor()( v );
337294

338-
if ( fullDates[ selDate ] ) {
339-
fullDates[ selDate ].myValue += selValue;
340-
} else {
341-
fullDates[ selDate ] = {
342-
myValue: selValue
343-
};
344-
}
345-
return p;
346-
},
347-
function ( p, v ) {
348-
var selDate = _this.keyAccessor()( v );
349-
var selValue = _this.valueAccessor()( v );
350-
351-
if ( fullDates[ selDate ] ) {
352-
fullDates[ selDate ].quantity -= selValue;
353-
} else {
354-
delete fullDates[ selDate ];
355-
}
356-
return p;
357-
},
358-
function () {
359-
fullDates = {};
295+
// Gather all values so we can compare against previous ones
296+
var fullDates = {};
297+
all.forEach(
298+
function( d ) {
299+
fullDates[ _this.keyAccessor()( d ) ] = _this.valueAccessor()( d );
360300
}
361301
);
362-
363-
reducer.all();
364302
var orderedDates = Object.keys( fullDates ).sort();
365303

366304
var accumulatedAll = all.map( function( d, i, arr ) {
@@ -388,7 +326,7 @@ crossfilterMA.accumulateGroupForNDayMovingAverage = function( sourceGroup, ndays
388326

389327
if ( targetDayId ) {
390328
var targetDayBlock = fullDates[ targetDayId ];
391-
var targetDayValue = targetDayBlock.myValue;
329+
var targetDayValue = targetDayBlock;
392330

393331
numsToAverage++;
394332
thisCumulate += targetDayValue;
@@ -573,45 +511,14 @@ crossfilterMA.accumulateGroupForPercentageChange = function( sourceGroup, debugM
573511
all: function () {
574512
var _this = this;
575513
var all = sourceGroup.all();
576-
var fullDates = {};
577-
578-
var myDx = crossfilter( all );
579-
var myDimensionDate = myDx.dimension( function( d ) {
580-
return d.key;
581-
} );
582-
var myGroupingOnDate = myDimensionDate.group();
583-
584-
var reducer = myGroupingOnDate.reduce(
585-
function ( p, v ) {
586-
var selDate = _this.keyAccessor()( v );
587-
var selValue = _this.valueAccessor()( v );
588514

589-
if ( fullDates[ selDate ] ) {
590-
fullDates[ selDate ].myValue += selValue;
591-
} else {
592-
fullDates[ selDate ] = {
593-
myValue: selValue
594-
};
595-
}
596-
return p;
597-
},
598-
function ( p, v ) {
599-
var selDate = _this.keyAccessor()( v );
600-
var selValue = _this.valueAccessor()( v );
601-
602-
if ( fullDates[ selDate ] ) {
603-
fullDates[ selDate ].quantity -= selValue;
604-
} else {
605-
delete fullDates[ selDate ];
606-
}
607-
return p;
608-
},
609-
function () {
610-
fullDates = {};
515+
// Gather all values so we can compare against previous ones
516+
var fullDates = {};
517+
all.forEach(
518+
function( d ) {
519+
fullDates[ _this.keyAccessor()( d ) ] = _this.valueAccessor()( d );
611520
}
612521
);
613-
614-
reducer.all();
615522
var orderedDates = Object.keys( fullDates ).sort();
616523

617524
var accumulatedAll = all.map( function( d, i, arr ) {
@@ -625,7 +532,7 @@ crossfilterMA.accumulateGroupForPercentageChange = function( sourceGroup, debugM
625532

626533
if ( prevDayId ) {
627534
var prevDayBlock = fullDates[ prevDayId ];
628-
prevDayValue = prevDayBlock.myValue;
535+
prevDayValue = prevDayBlock;
629536

630537
var diff = _this.valueAccessor()( thisDay ) - prevDayValue;
631538

@@ -664,45 +571,14 @@ crossfilterMA.accumulateGroupForPercentageChange = function( sourceGroup, debugM
664571

665572
var _this = this;
666573
var all = sourceGroup.top.apply( sourceGroup, arguments );
667-
var fullDates = {};
668-
669-
var myDx = crossfilter( all );
670-
var myDimensionDate = myDx.dimension( function( d ) {
671-
return d.key;
672-
} );
673-
var myGroupingOnDate = myDimensionDate.group();
674-
675-
var reducer = myGroupingOnDate.reduce(
676-
function ( p, v ) {
677-
var selDate = _this.keyAccessor()( v );
678-
var selValue = _this.valueAccessor()( v );
679574

680-
if ( fullDates[ selDate ] ) {
681-
fullDates[ selDate ].myValue += selValue;
682-
} else {
683-
fullDates[ selDate ] = {
684-
myValue: selValue
685-
};
686-
}
687-
return p;
688-
},
689-
function ( p, v ) {
690-
var selDate = _this.keyAccessor()( v );
691-
var selValue = _this.valueAccessor()( v );
692-
693-
if ( fullDates[ selDate ] ) {
694-
fullDates[ selDate ].quantity -= selValue;
695-
} else {
696-
delete fullDates[ selDate ];
697-
}
698-
return p;
699-
},
700-
function () {
701-
fullDates = {};
575+
// Gather all values so we can compare against previous ones
576+
var fullDates = {};
577+
all.forEach(
578+
function( d ) {
579+
fullDates[ _this.keyAccessor()( d ) ] = _this.valueAccessor()( d );
702580
}
703581
);
704-
705-
reducer.all();
706582
var orderedDates = Object.keys( fullDates ).sort();
707583

708584
var accumulatedAll = all.map( function( d, i, arr ) {
@@ -716,7 +592,7 @@ crossfilterMA.accumulateGroupForPercentageChange = function( sourceGroup, debugM
716592

717593
if ( prevDayId ) {
718594
var prevDayBlock = fullDates[ prevDayId ];
719-
prevDayValue = prevDayBlock.myValue;
595+
prevDayValue = prevDayBlock;
720596

721597
var diff = _this.valueAccessor()( thisDay ) - prevDayValue;
722598

crossfilter-ma.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)