Skip to content

Commit 5a0b003

Browse files
committed
Adding orderByMovingAverage
1 parent 5c87a3e commit 5a0b003

File tree

3 files changed

+386
-22
lines changed

3 files changed

+386
-22
lines changed

Diff for: spec/math-rolling-average-spec.js

+317
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,150 @@ describe('accumulateGroupForNDayMovingAverage', function() {
508508
});
509509

510510

511+
describe('orderByMovingAverage', function() {
512+
513+
var movingAverageGroupVisitsByPlaceAndTerritoryByDate;
514+
515+
beforeEach(function() {
516+
mockCustomKeyValueData();
517+
movingAverageGroupVisitsByPlaceAndTerritoryByDate = crossfilterMa.accumulateGroupForNDayMovingAverage( groupVisitsByPlaceAndTerritoryByDate );
518+
movingAverageGroupVisitsByPlaceAndTerritoryByDate._debug(true);
519+
520+
});
521+
522+
afterEach(function() {
523+
movingAverageGroupVisitsByPlaceAndTerritoryByDate = null;
524+
});
525+
526+
it('allows retrieving the current configuration', function() {
527+
528+
var state = movingAverageGroupVisitsByPlaceAndTerritoryByDate.orderByMovingAverage();
529+
expect( state ).toBe( false );
530+
531+
});
532+
533+
534+
it('is off by default', function() {
535+
536+
movingAverageGroupVisitsByPlaceAndTerritoryByDate.valueAccessor( function(d) { return d.value.places.A.visits; } );
537+
var state = movingAverageGroupVisitsByPlaceAndTerritoryByDate.orderByMovingAverage();
538+
expect( state ).toBe( false );
539+
540+
});
541+
542+
it('can be turned on', function() {
543+
544+
var state = movingAverageGroupVisitsByPlaceAndTerritoryByDate.orderByMovingAverage();
545+
expect( state ).not.toBe( true );
546+
547+
movingAverageGroupVisitsByPlaceAndTerritoryByDate.orderByMovingAverage(1);
548+
549+
state = movingAverageGroupVisitsByPlaceAndTerritoryByDate.orderByMovingAverage();
550+
expect( state ).toBe( 1 );
551+
552+
});
553+
554+
555+
it('can be turned off by passing 0 or false', function() {
556+
557+
var state = movingAverageGroupVisitsByPlaceAndTerritoryByDate.orderByMovingAverage();
558+
expect( state ).not.toBe( 1 );
559+
expect( state ).not.toBe( -1 );
560+
561+
movingAverageGroupVisitsByPlaceAndTerritoryByDate.orderByMovingAverage(1);
562+
state = movingAverageGroupVisitsByPlaceAndTerritoryByDate.orderByMovingAverage();
563+
564+
expect( state ).toBe( 1 );
565+
566+
movingAverageGroupVisitsByPlaceAndTerritoryByDate.orderByMovingAverage(0);
567+
state = movingAverageGroupVisitsByPlaceAndTerritoryByDate.orderByMovingAverage();
568+
569+
expect( state ).not.toBe( 1 );
570+
expect( state ).not.toBe( -1 );
571+
expect( state ).toBe( false );
572+
573+
});
574+
575+
it('allows sorting ascending by passing 1', function() {
576+
577+
movingAverageGroupVisitsByPlaceAndTerritoryByDate.valueAccessor( function(d) { return d.value.places.A.visits; } );
578+
movingAverageGroupVisitsByPlaceAndTerritoryByDate.orderByMovingAverage( 1 );
579+
580+
groupVisitsByPlaceAndTerritoryByDate.order( function(d) { return d.places.A.visits; } );
581+
582+
var resultsAll = movingAverageGroupVisitsByPlaceAndTerritoryByDate.top(Infinity);
583+
584+
expect( resultsAll[ 2 ].key ).toBe( "2012-01-13" );
585+
expect( resultsAll[ 0 ].key ).toBe( "2012-01-11" );
586+
expect( resultsAll[ 3 ].key ).toBe( "2012-01-15" );
587+
expect( resultsAll[ 1 ].key ).toBe( "2012-01-12" );
588+
589+
expect( resultsAll[ 2 ].value ).toBe( 17 );
590+
expect( resultsAll[ 0 ].value ).toBe( 3 );
591+
expect( resultsAll[ 3 ].value ).toBe( 10 );
592+
expect( resultsAll[ 1 ].value ).toBe( 0 );
593+
594+
expect( resultsAll[ 2 ].rollingAverage ).toBe( 8.5 );
595+
expect( resultsAll[ 0 ].rollingAverage ).toBe( 0 );
596+
expect( resultsAll[ 3 ].rollingAverage ).toBe( 13.5 );
597+
expect( resultsAll[ 1 ].rollingAverage ).toBe( 1.5 );
598+
599+
});
600+
601+
it('allows sorting descending by passing -1', function() {
602+
603+
movingAverageGroupVisitsByPlaceAndTerritoryByDate.valueAccessor( function(d) { return d.value.places.A.visits; } );
604+
movingAverageGroupVisitsByPlaceAndTerritoryByDate.orderByMovingAverage( -1 );
605+
606+
var state = movingAverageGroupVisitsByPlaceAndTerritoryByDate.orderByMovingAverage();
607+
expect( state ).toBe( -1 );
608+
609+
groupVisitsByPlaceAndTerritoryByDate.order( function(d) { return d.places.A.visits; } );
610+
611+
var resultsAll = movingAverageGroupVisitsByPlaceAndTerritoryByDate.top(Infinity);
612+
613+
expect( resultsAll[ 1 ].key ).toBe( "2012-01-13" );
614+
expect( resultsAll[ 3 ].key ).toBe( "2012-01-11" );
615+
expect( resultsAll[ 0 ].key ).toBe( "2012-01-15" );
616+
expect( resultsAll[ 2 ].key ).toBe( "2012-01-12" );
617+
618+
expect( resultsAll[ 1 ].value ).toBe( 17 );
619+
expect( resultsAll[ 3 ].value ).toBe( 3 );
620+
expect( resultsAll[ 0 ].value ).toBe( 10 );
621+
expect( resultsAll[ 2 ].value ).toBe( 0 );
622+
623+
expect( resultsAll[ 1 ].rollingAverage ).toBe( 8.5 );
624+
expect( resultsAll[ 3 ].rollingAverage ).toBe( 0 );
625+
expect( resultsAll[ 0 ].rollingAverage ).toBe( 13.5 );
626+
expect( resultsAll[ 2 ].rollingAverage ).toBe( 1.5 );
627+
628+
});
629+
630+
it('treats other values as false', function() {
631+
632+
var state;
633+
634+
movingAverageGroupVisitsByPlaceAndTerritoryByDate.orderByMovingAverage(16);
635+
state = movingAverageGroupVisitsByPlaceAndTerritoryByDate.orderByMovingAverage();
636+
expect( state ).toBe( false );
637+
638+
movingAverageGroupVisitsByPlaceAndTerritoryByDate.orderByMovingAverage(1);
639+
state = movingAverageGroupVisitsByPlaceAndTerritoryByDate.orderByMovingAverage();
640+
expect( state ).toBe( 1 );
641+
642+
movingAverageGroupVisitsByPlaceAndTerritoryByDate.orderByMovingAverage(-123);
643+
state = movingAverageGroupVisitsByPlaceAndTerritoryByDate.orderByMovingAverage();
644+
expect( state ).toBe( false );
645+
646+
movingAverageGroupVisitsByPlaceAndTerritoryByDate.orderByMovingAverage(16);
647+
state = movingAverageGroupVisitsByPlaceAndTerritoryByDate.orderByMovingAverage();
648+
expect( state ).toBe( false );
649+
650+
651+
});
652+
653+
});
654+
511655
describe('all()', function() {
512656

513657
it('calculate 2 point rolling average over a set of numbers', function() {
@@ -620,6 +764,64 @@ describe('accumulateGroupForNDayMovingAverage', function() {
620764
});
621765

622766

767+
it('calculate 3 point rolling average over set of numbers ordered ascending by the moving averages', function() {
768+
769+
var rollingAverageFakeGroup = crossfilterMa.accumulateGroupForNDayMovingAverage( groupVisitsByDate, 3 );
770+
rollingAverageFakeGroup.orderByMovingAverage( 1 );
771+
rollingAverageFakeGroup._debug( true );
772+
773+
var results = rollingAverageFakeGroup.all();
774+
775+
expect( results[0].key ).toBe( '2012-01-11' );
776+
expect( results[0].rollingAverage ).toBe( 0 );
777+
expect( results[0]._debug.datumsUsed.length ).toBe( 1 );
778+
expect( results[1].key ).toBe( '2012-01-12' );
779+
expect( results[1].rollingAverage ).toBe( 0 );
780+
expect( results[1]._debug.datumsUsed.length ).toBe( 1 );
781+
expect( results[2].key ).toBe( '2012-01-13' );
782+
expect( results[2].rollingAverage ).toBe( 5 );
783+
expect( results[2]._debug.datumsUsed.length ).toBe( 3 );
784+
expect( results[3].key ).toBe( '2012-01-14' );
785+
expect( results[3].rollingAverage ).toBeCloseTo( 5.333333333333333 );
786+
expect( results[3]._debug.datumsUsed.length ).toBe( 3 );
787+
expect( results[4].key ).toBe( '2012-01-15' );
788+
expect( results[4].rollingAverage ).toBeCloseTo( 7.666666666666667 );
789+
expect( results[5].key ).toBe( '2012-01-16' );
790+
expect( results[5].rollingAverage ).toBeCloseTo( 8.333333333333334 );
791+
expect( results[6].key ).toBe( '2012-01-17' );
792+
expect( results[6].rollingAverage ).toBeCloseTo( 9.666666666666666 );
793+
794+
});
795+
796+
it('calculate 3 point rolling average over set of numbers ordered descending by the moving averages', function() {
797+
798+
var rollingAverageFakeGroup = crossfilterMa.accumulateGroupForNDayMovingAverage( groupVisitsByDate, 3 );
799+
rollingAverageFakeGroup.orderByMovingAverage( -1 );
800+
rollingAverageFakeGroup._debug( true );
801+
802+
var results = rollingAverageFakeGroup.all();
803+
804+
expect( results[5].key ).toBe( '2012-01-11' );
805+
expect( results[5].rollingAverage ).toBe( 0 );
806+
expect( results[5]._debug.datumsUsed.length ).toBe( 1 );
807+
expect( results[6].key ).toBe( '2012-01-12' );
808+
expect( results[6].rollingAverage ).toBe( 0 );
809+
expect( results[6]._debug.datumsUsed.length ).toBe( 1 );
810+
expect( results[4].key ).toBe( '2012-01-13' );
811+
expect( results[4].rollingAverage ).toBe( 5 );
812+
expect( results[4]._debug.datumsUsed.length ).toBe( 3 );
813+
expect( results[3].key ).toBe( '2012-01-14' );
814+
expect( results[3].rollingAverage ).toBeCloseTo( 5.333333333333333 );
815+
expect( results[3]._debug.datumsUsed.length ).toBe( 3 );
816+
expect( results[2].key ).toBe( '2012-01-15' );
817+
expect( results[2].rollingAverage ).toBeCloseTo( 7.666666666666667 );
818+
expect( results[1].key ).toBe( '2012-01-16' );
819+
expect( results[1].rollingAverage ).toBeCloseTo( 8.333333333333334 );
820+
expect( results[0].key ).toBe( '2012-01-17' );
821+
expect( results[0].rollingAverage ).toBeCloseTo( 9.666666666666666 );
822+
823+
});
824+
623825
it('supports filtering in crossfilter', function() {
624826

625827
var percentageChangeFakeGroup = crossfilterMa.accumulateGroupForNDayMovingAverage( groupVisitsByDate, 3 );
@@ -1332,6 +1534,121 @@ describe('accumulateGroupForNDayMovingAverage', function() {
13321534

13331535
});
13341536

1537+
1538+
it('calculate 3 point rolling average over set of numbers ordered ascending by the moving averages', function() {
1539+
1540+
var rollingAverageFakeGroup = crossfilterMa.accumulateGroupForNDayMovingAverage( groupVisitsByDate, 3 );
1541+
rollingAverageFakeGroup.orderByMovingAverage( 1 );
1542+
rollingAverageFakeGroup._debug( true );
1543+
1544+
var results = rollingAverageFakeGroup.top(Infinity);
1545+
1546+
expect( results[5].key ).toBe( '2012-01-16' );
1547+
expect( results[5].rollingAverage ).toBeCloseTo( 8.333333333333334 );
1548+
expect( results[5]._debug.datumsUsed.length ).toBe( 3 );
1549+
expect( results[4].key ).toBe( '2012-01-15' );
1550+
expect( results[4].rollingAverage ).toBeCloseTo( 7.666666666666667 );
1551+
expect( results[4]._debug.datumsUsed.length ).toBe( 3 );
1552+
expect( results[2].key ).toBe( '2012-01-13' );
1553+
expect( results[2].rollingAverage ).toBe( 5 );
1554+
expect( results[2]._debug.datumsUsed.length ).toBe( 3 );
1555+
expect( results[6].key ).toBe( '2012-01-17' );
1556+
expect( results[6].rollingAverage ).toBeCloseTo( 9.666666666666666 );
1557+
expect( results[6]._debug.datumsUsed.length ).toBe( 3 );
1558+
expect( results[3].key ).toBe( '2012-01-14' );
1559+
expect( results[3].rollingAverage ).toBeCloseTo( 5.333333333333333 );
1560+
expect( results[0].key ).toBe( '2012-01-12' );
1561+
expect( results[0].rollingAverage ).toBe( 0 );
1562+
expect( results[1].key ).toBe( '2012-01-11' );
1563+
expect( results[1].rollingAverage ).toBe( 0 );
1564+
expect( results[1]._debug.datumsUsed.length ).toBe( 1 );
1565+
1566+
});
1567+
1568+
it('calculate 3 point rolling average over set of numbers ordered descending by the moving averages', function() {
1569+
1570+
var rollingAverageFakeGroup = crossfilterMa.accumulateGroupForNDayMovingAverage( groupVisitsByDate, 3 );
1571+
rollingAverageFakeGroup.orderByMovingAverage( -1 );
1572+
rollingAverageFakeGroup._debug( true );
1573+
1574+
var results = rollingAverageFakeGroup.top(Infinity);
1575+
1576+
expect( results[1].key ).toBe( '2012-01-16' );
1577+
expect( results[1].rollingAverage ).toBeCloseTo( 8.333333333333334 );
1578+
expect( results[1]._debug.datumsUsed.length ).toBe( 3 );
1579+
expect( results[2].key ).toBe( '2012-01-15' );
1580+
expect( results[2].rollingAverage ).toBeCloseTo( 7.666666666666667 );
1581+
expect( results[2]._debug.datumsUsed.length ).toBe( 3 );
1582+
expect( results[4].key ).toBe( '2012-01-13' );
1583+
expect( results[4].rollingAverage ).toBe( 5 );
1584+
expect( results[4]._debug.datumsUsed.length ).toBe( 3 );
1585+
expect( results[0].key ).toBe( '2012-01-17' );
1586+
expect( results[0].rollingAverage ).toBeCloseTo( 9.666666666666666 );
1587+
expect( results[0]._debug.datumsUsed.length ).toBe( 3 );
1588+
expect( results[3].key ).toBe( '2012-01-14' );
1589+
expect( results[3].rollingAverage ).toBeCloseTo( 5.333333333333333 );
1590+
expect( results[5].key ).toBe( '2012-01-12' );
1591+
expect( results[5].rollingAverage ).toBe( 0 );
1592+
expect( results[6].key ).toBe( '2012-01-11' );
1593+
expect( results[6].rollingAverage ).toBe( 0 );
1594+
expect( results[6]._debug.datumsUsed.length ).toBe( 1 );
1595+
1596+
});
1597+
1598+
1599+
it('supports filtering in crossfilter', function() {
1600+
1601+
var percentageChangeFakeGroup = crossfilterMa.accumulateGroupForNDayMovingAverage( groupVisitsByDate, 3 );
1602+
percentageChangeFakeGroup._debug( true );
1603+
1604+
var results = percentageChangeFakeGroup.top(Infinity);
1605+
1606+
expect( results[0].key ).toBe( '2012-01-16' );
1607+
expect( results[0].rollingAverage ).toBeCloseTo( 8.333333333333334 );
1608+
expect( results[0]._debug.datumsUsed.length ).toBe( 3 );
1609+
expect( results[1].key ).toBe( '2012-01-15' );
1610+
expect( results[1].rollingAverage ).toBeCloseTo( 7.666666666666667 );
1611+
expect( results[1]._debug.datumsUsed.length ).toBe( 3 );
1612+
expect( results[2].key ).toBe( '2012-01-13' );
1613+
expect( results[2].rollingAverage ).toBe( 5 );
1614+
expect( results[2]._debug.datumsUsed.length ).toBe( 3 );
1615+
expect( results[3].key ).toBe( '2012-01-17' );
1616+
expect( results[3].rollingAverage ).toBeCloseTo( 9.666666666666666 );
1617+
expect( results[3]._debug.datumsUsed.length ).toBe( 3 );
1618+
expect( results[4].key ).toBe( '2012-01-14' );
1619+
expect( results[4].rollingAverage ).toBeCloseTo( 5.333333333333333 );
1620+
expect( results[5].key ).toBe( '2012-01-12' );
1621+
expect( results[5].rollingAverage ).toBe( 0 );
1622+
expect( results[6].key ).toBe( '2012-01-11' );
1623+
expect( results[6].rollingAverage ).toBe( 0 );
1624+
expect( results[6]._debug.datumsUsed.length ).toBe( 1 );
1625+
1626+
dimensionVisitsForFiltering.filterRange( [ 3,11 ] );
1627+
1628+
var results = percentageChangeFakeGroup.top(Infinity);
1629+
1630+
expect( results[0].key ).toBe( '2012-01-15' );
1631+
expect( results[0].rollingAverage ).toBeCloseTo( 7.666666666666667 );
1632+
expect( results[0]._debug.datumsUsed.length ).toBe( 3 );
1633+
expect( results[1].key ).toBe( '2012-01-13' );
1634+
expect( results[1].rollingAverage ).toBeCloseTo( 4.3333333 );
1635+
expect( results[1]._debug.datumsUsed.length ).toBe( 3 );
1636+
expect( results[2].key ).toBe( '2012-01-17' );
1637+
expect( results[2].rollingAverage ).toBeCloseTo( 5.6666667 );
1638+
expect( results[2]._debug.datumsUsed.length ).toBe( 3 );
1639+
expect( results[3].key ).toBe( '2012-01-14' );
1640+
expect( results[3].rollingAverage ).toBeCloseTo( 5.333333 );
1641+
expect( results[3]._debug.datumsUsed.length ).toBe( 3 );
1642+
expect( results[4].key ).toBe( '2012-01-12' );
1643+
expect( results[4].rollingAverage ).toBe( 0 );
1644+
expect( results[5].key ).toBe( '2012-01-16' );
1645+
expect( results[5].rollingAverage ).toBeCloseTo( 4.33333 );
1646+
expect( results[6].key ).toBe( '2012-01-11' );
1647+
expect( results[6].rollingAverage ).toBe( 0 );
1648+
expect( results[6]._debug.datumsUsed.length ).toBe( 1 );
1649+
1650+
});
1651+
13351652
it('only returns debug information when debug flag is engaged', function() {
13361653

13371654
var percentageChangeFakeGroup = crossfilterMa.accumulateGroupForNDayMovingAverage( groupVisitsByDate );

Diff for: spec/percentage-change-spec.js

-19
Original file line numberDiff line numberDiff line change
@@ -437,25 +437,6 @@ describe('accumulateGroupForPercentageChange', function() {
437437
var state = percentageChangeGroupVisitsByPlaceAndTerritoryByDate.orderByPercentageChange();
438438
expect( state ).toBe( false );
439439

440-
groupVisitsByPlaceAndTerritoryByDate.order( function(d) { return d.places.A.visits; } );
441-
442-
var resultsAll = percentageChangeGroupVisitsByPlaceAndTerritoryByDate.top(Infinity);
443-
444-
expect( resultsAll[ 0 ].key ).toBe( "2012-01-13" );
445-
expect( resultsAll[ 1 ].key ).toBe( "2012-01-15" );
446-
expect( resultsAll[ 2 ].key ).toBe( "2012-01-11" );
447-
expect( resultsAll[ 3 ].key ).toBe( "2012-01-12" );
448-
449-
expect( resultsAll[ 0 ].value.places.A.visits ).toBe( 17 );
450-
expect( resultsAll[ 1 ].value.places.A.visits ).toBe( 10 );
451-
expect( resultsAll[ 2 ].value.places.A.visits ).toBe( 3 );
452-
expect( resultsAll[ 3 ].value.places.A.visits ).toBe( 0 );
453-
454-
expect( resultsAll[ 0 ].percentageChange ).toBe( Infinity );
455-
expect( resultsAll[ 1 ].percentageChange ).toBeCloseTo( -41.18 );
456-
expect( resultsAll[ 2 ].percentageChange ).toBe( 0 );
457-
expect( resultsAll[ 3 ].percentageChange ).toBe( -100 );
458-
459440
});
460441

461442
it('can be turned on', function() {

0 commit comments

Comments
 (0)