Skip to content

Commit 7ecf81d

Browse files
committed
add smart orientation dflt logic for multicategory position axes
1 parent 80662d4 commit 7ecf81d

File tree

2 files changed

+168
-7
lines changed

2 files changed

+168
-7
lines changed

src/traces/box/defaults.js

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var Lib = require('../../lib');
1212
var Registry = require('../../registry');
1313
var Color = require('../../components/color');
1414
var handleGroupingDefaults = require('../bar/defaults').handleGroupingDefaults;
15+
var autoType = require('../../plots/cartesian/axis_autotype');
1516
var attributes = require('./attributes');
1617

1718
function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
@@ -128,7 +129,7 @@ function handleSampleDefaults(traceIn, traceOut, coerce, layout) {
128129
break;
129130
case '20':
130131
defaultOrientation = 'h';
131-
len = Math.min(sLen, xLen);
132+
len = Math.min(sLen, x.length);
132133
break;
133134
// just y
134135
case '01':
@@ -137,25 +138,50 @@ function handleSampleDefaults(traceIn, traceOut, coerce, layout) {
137138
break;
138139
case '02':
139140
defaultOrientation = 'v';
140-
len = Math.min(sLen, yLen);
141+
len = Math.min(sLen, y.length);
141142
break;
142143
// both
143144
case '12':
144145
defaultOrientation = 'v';
145-
len = Math.min(sLen, xLen, yLen);
146+
len = Math.min(sLen, xLen, y.length);
146147
break;
147148
case '21':
148149
defaultOrientation = 'h';
149-
len = Math.min(sLen, xLen, yLen);
150+
len = Math.min(sLen, x.length, yLen);
150151
break;
151152
case '11':
152153
// this one is ill-defined
153154
len = 0;
154155
break;
155156
case '22':
156-
// this one case happen on multi-category axes
157-
defaultOrientation = 'v';
158-
len = Math.min(sLen, xLen, yLen);
157+
var hasCategories = false;
158+
var i;
159+
for(i = 0; i < x.length; i++) {
160+
if(autoType(x[i]) === 'category') {
161+
hasCategories = true;
162+
break;
163+
}
164+
}
165+
166+
if(hasCategories) {
167+
defaultOrientation = 'v';
168+
len = Math.min(sLen, xLen, y.length);
169+
} else {
170+
for(i = 0; i < y.length; i++) {
171+
if(autoType(y[i]) === 'category') {
172+
hasCategories = true;
173+
break;
174+
}
175+
}
176+
177+
if(hasCategories) {
178+
defaultOrientation = 'h';
179+
len = Math.min(sLen, x.length, yLen);
180+
} else {
181+
defaultOrientation = 'v';
182+
len = Math.min(sLen, xLen, y.length);
183+
}
184+
}
159185
break;
160186
}
161187
} else if(yDims > 0) {

test/jasmine/tests/box_test.js

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,30 @@ describe('Test boxes supplyDefaults', function() {
260260
x0: undefined, dx: undefined,
261261
y0: 0, dy: 1
262262
});
263+
_check('with set 2D x (sliced to length q1/median/q3)', {
264+
x: [[1, 2, 3], [2, 3, 4]],
265+
q1: [1],
266+
median: [2],
267+
q3: [3]
268+
}, {
269+
visible: true,
270+
orientation: 'h',
271+
_length: 1,
272+
x0: undefined, dx: undefined,
273+
y0: 0, dy: 1
274+
});
275+
_check('with set 2D x (sliced to x.length)', {
276+
x: [[1, 2, 3]],
277+
q1: [1, 2],
278+
median: [2, 3],
279+
q3: [3, 4]
280+
}, {
281+
visible: true,
282+
orientation: 'h',
283+
_length: 1,
284+
x0: undefined, dx: undefined,
285+
y0: 0, dy: 1
286+
});
263287
_check('with set y', {
264288
y: [0],
265289
q1: [1],
@@ -284,6 +308,30 @@ describe('Test boxes supplyDefaults', function() {
284308
x0: 0, dx: 1,
285309
y0: undefined, dy: undefined
286310
});
311+
_check('with set 2d y (sliced to y.length)', {
312+
y: [[1, 2, 3]],
313+
q1: [1, 2],
314+
median: [2, 3],
315+
q3: [3, 4]
316+
}, {
317+
visible: true,
318+
orientation: 'v',
319+
_length: 1,
320+
x0: 0, dx: 1,
321+
y0: undefined, dy: undefined
322+
});
323+
_check('with set 2d y (sliced to q1/median/q3 length)', {
324+
y: [[1, 2, 3], [2, 3, 4]],
325+
q1: [1],
326+
median: [2],
327+
q3: [3]
328+
}, {
329+
visible: true,
330+
orientation: 'v',
331+
_length: 1,
332+
x0: 0, dx: 1,
333+
y0: undefined, dy: undefined
334+
});
287335
_check('with set x AND 2d y', {
288336
x: [0],
289337
y: [[1, 2, 3]],
@@ -299,6 +347,21 @@ describe('Test boxes supplyDefaults', function() {
299347
x0: undefined, dx: undefined,
300348
y0: undefined, dy: undefined
301349
});
350+
_check('with set x AND 2d y (sliced to y.length)', {
351+
x: [0, 1],
352+
y: [[1, 2, 3]],
353+
q1: [1, 2],
354+
median: [2, 3],
355+
q3: [3, 4]
356+
}, {
357+
visible: true,
358+
orientation: 'v',
359+
_length: 1,
360+
x: [0, 1],
361+
y: [[1, 2, 3]],
362+
x0: undefined, dx: undefined,
363+
y0: undefined, dy: undefined
364+
});
302365
_check('with set 2d x AND y', {
303366
x: [[1, 2, 3]],
304367
y: [4],
@@ -314,6 +377,78 @@ describe('Test boxes supplyDefaults', function() {
314377
x0: undefined, dx: undefined,
315378
y0: undefined, dy: undefined
316379
});
380+
_check('with set 2d x AND y (sliced to x.length)', {
381+
x: [[1, 2, 3]],
382+
y: [4, 5],
383+
q1: [1, 2],
384+
median: [2, 3],
385+
q3: [3, 4]
386+
}, {
387+
visible: true,
388+
orientation: 'h',
389+
_length: 1,
390+
x: [[1, 2, 3]],
391+
y: [4, 5],
392+
x0: undefined, dx: undefined,
393+
y0: undefined, dy: undefined
394+
});
395+
_check('with set 2d multicategory x AND 2d y', {
396+
x: [
397+
['2017', '2017', '2018', '2018'],
398+
['q1', 'q2', 'q1', 'q2']
399+
],
400+
y: [
401+
[0, 1, 2, 3, 4],
402+
[1, 2, 3, 4, 5],
403+
[2, 3, 4, 5, 6],
404+
[3, 4, 5, 6, 7]
405+
],
406+
q1: [1, 2, 3, 4],
407+
median: [2, 3, 4, 5],
408+
q3: [3, 4, 5, 6]
409+
}, {
410+
visible: true,
411+
orientation: 'v',
412+
_length: 4
413+
});
414+
_check('with set 2d x AND 2d multicategory y', {
415+
y: [
416+
['2017', '2017', '2018', '2018'],
417+
['q1', 'q2', 'q1', 'q2']
418+
],
419+
x: [
420+
[0, 1, 2, 3, 4],
421+
[1, 2, 3, 4, 5],
422+
[2, 3, 4, 5, 6],
423+
[3, 4, 5, 6, 7]
424+
],
425+
q1: [1, 2, 3, 4],
426+
median: [2, 3, 4, 5],
427+
q3: [3, 4, 5, 6]
428+
}, {
429+
visible: true,
430+
orientation: 'h',
431+
_length: 4
432+
});
433+
_check('with set category 2d x AND 2d y (edge case!)', {
434+
x: [
435+
['2017', '2017', '2018', '2018'],
436+
['q1', 'q2', 'q1', 'q2']
437+
],
438+
y: [
439+
['a', 'b', 'c'],
440+
['a', 'b', 'c'],
441+
['a', 'b', 'c'],
442+
['a', 'b', 'c']
443+
],
444+
q1: [1, 2, 3, 4],
445+
median: [2, 3, 4, 5],
446+
q3: [3, 4, 5, 6]
447+
}, {
448+
visible: true,
449+
orientation: 'v',
450+
_length: 4
451+
});
317452
_check('with just y0', {
318453
y0: 4,
319454
q1: [1],

0 commit comments

Comments
 (0)