|
1 | 1 | var Plotly = require('@lib'); |
2 | 2 | var Lib = require('@src/lib'); |
| 3 | +var Plots = require('@src/plots/plots'); |
3 | 4 |
|
4 | 5 | var Box = require('@src/traces/box'); |
5 | 6 |
|
@@ -610,3 +611,94 @@ describe('Test box restyle:', function() { |
610 | 611 | .then(done); |
611 | 612 | }); |
612 | 613 | }); |
| 614 | + |
| 615 | +describe('Test box calc', function() { |
| 616 | + var gd; |
| 617 | + |
| 618 | + function _calc(attrs, layout) { |
| 619 | + gd = { |
| 620 | + data: [Lib.extendFlat({type: 'box'}, attrs)], |
| 621 | + layout: layout || {}, |
| 622 | + calcdata: [] |
| 623 | + }; |
| 624 | + supplyAllDefaults(gd); |
| 625 | + Plots.doCalcdata(gd); |
| 626 | + return gd.calcdata[0]; |
| 627 | + } |
| 628 | + |
| 629 | + it('should compute q1/q3 depending on *quartilemethod*', function() { |
| 630 | + // samples from https://en.wikipedia.org/wiki/Quartile |
| 631 | + var specs = { |
| 632 | + // N is odd and is spanned by (4n+3) |
| 633 | + odd: { |
| 634 | + sample: [6, 7, 15, 36, 39, 40, 41, 42, 43, 47, 49], |
| 635 | + methods: { |
| 636 | + linear: {q1: 20.25, q3: 42.75}, |
| 637 | + exclusive: {q1: 15, q3: 43}, |
| 638 | + inclusive: {q1: 25.5, q3: 42.5} |
| 639 | + } |
| 640 | + }, |
| 641 | + // N is odd and is spanned by (4n+1) |
| 642 | + odd2: { |
| 643 | + sample: [6, 15, 36, 39, 40, 42, 43, 47, 49], |
| 644 | + methods: { |
| 645 | + linear: {q1: 30.75, q3: 44}, |
| 646 | + exclusive: {q1: 25.5, q3: 45}, |
| 647 | + inclusive: {q1: 36, q3: 43} |
| 648 | + } |
| 649 | + }, |
| 650 | + // N is even |
| 651 | + even: { |
| 652 | + sample: [7, 15, 36, 39, 40, 41], |
| 653 | + methods: { |
| 654 | + linear: {q1: 15, q3: 40}, |
| 655 | + exclusive: {q1: 15, q3: 40}, |
| 656 | + inclusive: {q1: 15, q3: 40} |
| 657 | + } |
| 658 | + }, |
| 659 | + // samples from http://jse.amstat.org/v14n3/langford.html |
| 660 | + s4: { |
| 661 | + sample: [1, 2, 3, 4], |
| 662 | + methods: { |
| 663 | + linear: {q1: 1.5, q3: 3.5}, |
| 664 | + exclusive: {q1: 1.5, q3: 3.5}, |
| 665 | + inclusive: {q1: 1.5, q3: 3.5} |
| 666 | + } |
| 667 | + }, |
| 668 | + s5: { |
| 669 | + sample: [1, 2, 3, 4, 5], |
| 670 | + methods: { |
| 671 | + linear: {q1: 1.75, q3: 4.25}, |
| 672 | + exclusive: {q1: 1.5, q3: 4.5}, |
| 673 | + inclusive: {q1: 2, q3: 4} |
| 674 | + } |
| 675 | + }, |
| 676 | + s6: { |
| 677 | + sample: [1, 2, 3, 4, 5, 6], |
| 678 | + methods: { |
| 679 | + linear: {q1: 2, q3: 5}, |
| 680 | + exclusive: {q1: 2, q3: 5}, |
| 681 | + inclusive: {q1: 2, q3: 5} |
| 682 | + } |
| 683 | + }, |
| 684 | + s7: { |
| 685 | + sample: [1, 2, 3, 4, 5, 6, 7], |
| 686 | + methods: { |
| 687 | + linear: {q1: 2.25, q3: 5.75}, |
| 688 | + exclusive: {q1: 2, q3: 6}, |
| 689 | + inclusive: {q1: 2.5, q3: 5.5} |
| 690 | + } |
| 691 | + } |
| 692 | + }; |
| 693 | + |
| 694 | + for(var name in specs) { |
| 695 | + var spec = specs[name]; |
| 696 | + |
| 697 | + for(var m in spec.methods) { |
| 698 | + var cd = _calc({y: spec.sample, quartilemethod: m}); |
| 699 | + expect(cd[0].q1).toBe(spec.methods[m].q1, ['q1', m, name].join(' | ')); |
| 700 | + expect(cd[0].q3).toBe(spec.methods[m].q3, ['q3', m, name].join(' | ')); |
| 701 | + } |
| 702 | + } |
| 703 | + }); |
| 704 | +}); |
0 commit comments