10000 aj-proof multicategory ax-autotype logic · lzhice/plotly.js@3746e03 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3746e03

Browse files
committed
aj-proof multicategory ax-autotype logic
1 parent 69ccfd3 commit 3746e03

File tree

4 files changed

+55
-17
lines changed

4 files changed

+55
-17
lines changed

src/plots/cartesian/axis_autotype.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ var isNumeric = require('fast-isnumeric');
1414
var Lib = require('../../lib');
1515
var BADNUM = require('../../constants/numerical').BADNUM;
1616

17-
module.exports = function autoType(array, calendar) {
18-
if(multiCategory(array)) return 'multicategory';
17+
module.exports = function autoType(array, calendar, opts) {
18+
opts = opts || {};
19+
20+
if(!opts.noMultiCategory && multiCategory(array)) return 'multicategory';
1921
if(moreDates(array, calendar)) return 'date';
2022
if(category(array)) return 'category';
2123
if(linearOK(array)) return 'linear';
@@ -83,9 +85,9 @@ function category(a) {
8385
return curvecats > curvenums * 2;
8486
}
8587

88+
// very-loose requirements for multicategory,
89+
// trace modules that should never auto-type to multicategory
90+
// should be declared with 'noMultiCategory'
8691
function multiCategory(a) {
87-
return (
88-
Array.isArray(a[0]) && Array.isArray(a[1]) &&
89-
(category(a[0]) || category(a[1]))
90-
);
92+
return Array.isArray(a[0]) && Array.isArray(a[1]);
9193
}

src/plots/cartesian/type_defaults.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
'use strict';
1010

11-
var Registry = require('../../registry');
11+
var traceIs = require('../../registry').traceIs;
1212
var autoType = require('./axis_autotype');
1313

1414
/*
@@ -57,6 +57,7 @@ function setAutoType(ax, data) {
5757

5858
var calAttr = axLetter + 'calendar';
5959
var calendar = d0[calAttr];
60+
var opts = {noMultiCategory: !traceIs(d0, 'cartesian') || traceIs(d0, 'noMultiCategory')};
6061
var i;
6162

6263
// check all boxes on this x axis to see
@@ -67,8 +68,7 @@ function setAutoType(ax, data) {
6768

6869
for(i = 0; i < data.length; i++) {
6970
var trace = data[i];
70-
if(!Registry.traceIs(trace, 'box-violin') ||
71-
(trace[axLetter + 'axis'] || axLetter) !== id) continue;
71+
if(!traceIs(trace, 'box-violin') || (trace[axLetter + 'axis'] || axLetter) !== id) continue;
7272

7373
if(trace[posLetter] !== undefined) boxPositions.push(trace[posLetter][0]);
7474
else if(trace.name !== undefined) boxPositions.push(trace.name);
@@ -77,21 +77,21 @@ function setAutoType(ax, data) {
7777
if(trace[calAttr] !== calendar) calendar = undefined;
7878
}
7979

80-
ax.type = autoType(boxPositions, calendar);
80+
ax.type = autoType(boxPositions, calendar, opts);
8181
}
8282
else if(d0.type === 'splom') {
8383
var dimensions = d0.dimensions;
8484
var diag = d0._diag;
8585
for(i = 0; i < dimensions.length; i++) {
8686
var dim = dimensions[i];
8787
if(dim.visible && (diag[i][0] === id || diag[i][1] === id)) {
88-
ax.type = autoType(dim.values, calendar);
88+
ax.type = autoType(dim.values, calendar, opts);
8989
break;
9090
}
9191
}
9292
}
9393
else {
94-
ax.type = autoType(d0[axLetter] || [d0[axLetter + '0']], calendar);
94+
ax.type = autoType(d0[axLetter] || [d0[axLetter + '0']], calendar, opts);
9595
}
9696
}
9797

@@ -122,9 +122,9 @@ function getBoxPosLetter(trace) {
122122
}
123123

124124
function isBoxWithoutPositionCoords(trace, axLetter) {
125-
var posLetter = getBoxPosLetter(trace),
126-
isBox = Registry.traceIs(trace, 'box-violin'),
127-
isCandlestick = Registry.traceIs(trace._fullInput || {}, 'candlestick');
125+
var posLetter = getBoxPosLetter(trace);
126+
var isBox = traceIs(trace, 'box-violin');
127+
var isCandlestick = traceIs(trace._fullInput || {}, 'candlestick');
128128

129129
return (
130130
isBox &&

src/traces/carpet/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Carpet.isContainer = true; // so carpet traces get `calc` before other traces
2121
Carpet.moduleType = 'trace';
2222
Carpet.name = 'carpet';
2323
Carpet.basePlotModule = require('../../plots/cartesian');
24-
Carpet.categories = ['cartesian', 'svg', 'carpet', 'carpetAxis', 'notLegendIsolatable'];
24+
Carpet.categories = ['cartesian', 'svg', 'carpet', 'carpetAxis', 'notLegendIsolatable', 'noMultiCategory'];
2525
Carpet.meta = {
2626
description: [
2727
'The data describing carpet axis layout is set in `y` and (optionally)',

test/jasmine/tests/axes_test.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ describe('Test axes', function() {
268268
checkTypes('date', 'linear');
269269
});
270270

271-
it('needs one *true* category inner-array to be consider *multicategory*', function() {
271+
it('2d coordinate array are considered *multicategory*', function() {
272272
supplyWithTrace({
273273
x: [
274274
[2018, 2018, 2017, 2017],
@@ -280,6 +280,42 @@ describe('Test axes', function() {
280280
]
281281
});
282282
checkTypes('multicategory', 'multicategory');
283+
284+
supplyWithTrace({
285+
x: [
286+
[2018, 2018, 2017, 2017],
287+
[2018, 2018, 2017, 2017]
288+
],
289+
y: [
290+
['2018', '2018', '2017', '2017'],
291+
['2018', '2018', '2017', '2017']
292+
]
293+
});
294+
checkTypes('multicategory', 'multicategory');
295+
296+
supplyWithTrace({
297+
x: [
298+
[2018, 2018, 2017, 2017]
299+
],
300+
y: [
301+
null,
302+
['d', 'e', 'f']
303+
]
304+
});
305+
checkTypes('linear', 'linear');
306+
307+
supplyWithTrace({
308+
type: 'carpet',
309+
x: [
310+
[2018, 2018, 2017, 2017],
311+
['a', 'b', 'a', 'b']
312+
],
313+
y: [
314+
['a', 'b', 'c'],
315+
['d', 'e', 'f']
316+
]
317+
});
318+
checkTypes('linear', 'linear');
283319
});
284320
});
285321

0 commit comments

Comments
 (0)
0