Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions src/traces/box/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var Lib = require('../../lib');
var Registry = require('../../registry');
var Color = require('../../components/color');
var handleGroupingDefaults = require('../bar/defaults').handleGroupingDefaults;
var autoType = require('../../plots/cartesian/axis_autotype');
var attributes = require('./attributes');

function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
Expand Down Expand Up @@ -128,7 +129,7 @@ function handleSampleDefaults(traceIn, traceOut, coerce, layout) {
break;
case '20':
defaultOrientation = 'h';
len = Math.min(sLen, xLen);
len = Math.min(sLen, x.length);
break;
// just y
case '01':
Expand All @@ -137,25 +138,50 @@ function handleSampleDefaults(traceIn, traceOut, coerce, layout) {
break;
case '02':
defaultOrientation = 'v';
len = Math.min(sLen, yLen);
len = Math.min(sLen, y.length);
break;
// both
case '12':
defaultOrientation = 'v';
len = Math.min(sLen, xLen, yLen);
len = Math.min(sLen, xLen, y.length);
break;
case '21':
defaultOrientation = 'h';
len = Math.min(sLen, xLen, yLen);
len = Math.min(sLen, x.length, yLen);
break;
case '11':
// this one is ill-defined
len = 0;
break;
case '22':
// this one case happen on multi-category axes
defaultOrientation = 'v';
len = Math.min(sLen, xLen, yLen);
var hasCategories = false;
var i;
for(i = 0; i < x.length; i++) {
if(autoType(x[i]) === 'category') {
hasCategories = true;
break;
}
}

if(hasCategories) {
defaultOrientation = 'v';
len = Math.min(sLen, xLen, y.length);
} else {
for(i = 0; i < y.length; i++) {
if(autoType(y[i]) === 'category') {
hasCategories = true;
break;
}
}

if(hasCategories) {
defaultOrientation = 'h';
len = Math.min(sLen, x.length, yLen);
} else {
defaultOrientation = 'v';
len = Math.min(sLen, xLen, y.length);
}
}
break;
}
} else if(yDims > 0) {
Expand Down
135 changes: 135 additions & 0 deletions test/jasmine/tests/box_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,30 @@ describe('Test boxes supplyDefaults', function() {
x0: undefined, dx: undefined,
y0: 0, dy: 1
});
_check('with set 2D x (sliced to length q1/median/q3)', {
x: [[1, 2, 3], [2, 3, 4]],
q1: [1],
median: [2],
q3: [3]
}, {
visible: true,
orientation: 'h',
_length: 1,
x0: undefined, dx: undefined,
y0: 0, dy: 1
});
_check('with set 2D x (sliced to x.length)', {
x: [[1, 2, 3]],
q1: [1, 2],
median: [2, 3],
q3: [3, 4]
}, {
visible: true,
orientation: 'h',
_length: 1,
x0: undefined, dx: undefined,
y0: 0, dy: 1
});
_check('with set y', {
y: [0],
q1: [1],
Expand All @@ -284,6 +308,30 @@ describe('Test boxes supplyDefaults', function() {
x0: 0, dx: 1,
y0: undefined, dy: undefined
});
_check('with set 2d y (sliced to y.length)', {
y: [[1, 2, 3]],
q1: [1, 2],
median: [2, 3],
q3: [3, 4]
}, {
visible: true,
orientation: 'v',
_length: 1,
x0: 0, dx: 1,
y0: undefined, dy: undefined
});
_check('with set 2d y (sliced to q1/median/q3 length)', {
y: [[1, 2, 3], [2, 3, 4]],
q1: [1],
median: [2],
q3: [3]
}, {
visible: true,
orientation: 'v',
_length: 1,
x0: 0, dx: 1,
y0: undefined, dy: undefined
});
_check('with set x AND 2d y', {
x: [0],
y: [[1, 2, 3]],
Expand All @@ -299,6 +347,21 @@ describe('Test boxes supplyDefaults', function() {
x0: undefined, dx: undefined,
y0: undefined, dy: undefined
});
_check('with set x AND 2d y (sliced to y.length)', {
x: [0, 1],
y: [[1, 2, 3]],
q1: [1, 2],
median: [2, 3],
q3: [3, 4]
}, {
visible: true,
orientation: 'v',
_length: 1,
x: [0, 1],
y: [[1, 2, 3]],
x0: undefined, dx: undefined,
y0: undefined, dy: undefined
});
_check('with set 2d x AND y', {
x: [[1, 2, 3]],
y: [4],
Expand All @@ -314,6 +377,78 @@ describe('Test boxes supplyDefaults', function() {
x0: undefined, dx: undefined,
y0: undefined, dy: undefined
});
_check('with set 2d x AND y (sliced to x.length)', {
x: [[1, 2, 3]],
y: [4, 5],
q1: [1, 2],
median: [2, 3],
q3: [3, 4]
}, {
visible: true,
orientation: 'h',
_length: 1,
x: [[1, 2, 3]],
y: [4, 5],
x0: undefined, dx: undefined,
y0: undefined, dy: undefined
});
_check('with set 2d multicategory x AND 2d y', {
x: [
['2017', '2017', '2018', '2018'],
['q1', 'q2', 'q1', 'q2']
],
y: [
[0, 1, 2, 3, 4],
[1, 2, 3, 4, 5],
[2, 3, 4, 5, 6],
[3, 4, 5, 6, 7]
],
q1: [1, 2, 3, 4],
median: [2, 3, 4, 5],
q3: [3, 4, 5, 6]
}, {
visible: true,
orientation: 'v',
_length: 4
});
_check('with set 2d x AND 2d multicategory y', {
y: [
['2017', '2017', '2018', '2018'],
['q1', 'q2', 'q1', 'q2']
],
x: [
[0, 1, 2, 3, 4],
[1, 2, 3, 4, 5],
[2, 3, 4, 5, 6],
[3, 4, 5, 6, 7]
],
q1: [1, 2, 3, 4],
median: [2, 3, 4, 5],
q3: [3, 4, 5, 6]
}, {
visible: true,
orientation: 'h',
_length: 4
});
_check('with set category 2d x AND 2d y (edge case!)', {
x: [
['2017', '2017', '2018', '2018'],
['q1', 'q2', 'q1', 'q2']
],
y: [
['a', 'b', 'c'],
['a', 'b', 'c'],
['a', 'b', 'c'],
['a', 'b', 'c']
],
q1: [1, 2, 3, 4],
median: [2, 3, 4, 5],
q3: [3, 4, 5, 6]
}, {
visible: true,
orientation: 'v',
_length: 4
});
_check('with just y0', {
y0: 4,
q1: [1],
Expand Down