Skip to content

Commit 9a0416a

Browse files
committed
correct axes default logic
1 parent 84c2a6f commit 9a0416a

File tree

2 files changed

+43
-17
lines changed

2 files changed

+43
-17
lines changed

src/plots/cartesian/axis_defaults.js

+18-6
Original file line numberDiff line numberDiff line change
@@ -188,23 +188,35 @@ function rangebreaksDefaults(itemIn, itemOut, containerOut) {
188188
q = bnds[i];
189189
switch(pattern) {
190190
case DAY_OF_WEEK :
191-
if(isNumeric(q)) q = Math.floor(q);
191+
if(!isNumeric(q)) {
192+
itemOut.enabled = false;
193+
return;
194+
}
195+
q = +q;
192196

193-
q = Math.floor(+q);
194-
if(!(q >= 0 && q < 7)) {
197+
if(
198+
q !== Math.floor(q) || // don't accept fractional days for mow
199+
q < 0 || q >= 7
200+
) {
195201
itemOut.enabled = false;
196202
return;
197203
}
198-
// use int [0, 7)
204+
// use number
199205
itemOut.bounds[i] = bnds[i] = q;
200206
break;
201207

202208
case HOUR :
203-
if(!(q >= 0 && q <= 24)) { // accept 24
209+
if(!isNumeric(q)) {
210+
itemOut.enabled = false;
211+
return;
212+
}
213+
q = +q;
214+
215+
if(q < 0 || q > 24) { // accept 24
204216
itemOut.enabled = false;
205217
return;
206218
}
207-
// use float [0, 24]
219+
// use number
208220
itemOut.bounds[i] = bnds[i] = q;
209221
break;
210222
}

test/jasmine/tests/axes_test.js

+25-11
Original file line numberDiff line numberDiff line change
@@ -1165,15 +1165,19 @@ describe('Test axes', function() {
11651165

11661166
it('should validate inputs in respect to *day of week* pattern', function() {
11671167
layoutIn = {
1168-
xaxis: {type: 'date', rangebreaks: [{pattern: 'day of week', bounds: ['6.999', '0'] }]},
1168+
xaxis: {type: 'date', rangebreaks: [{pattern: 'day of week', bounds: ['6', '0'] }]},
11691169
xaxis2: {type: 'date', rangebreaks: [{bounds: ['Sunday'] }]},
11701170
xaxis3: {type: 'date', rangebreaks: [{bounds: ['sun', 'mon', 'tue'] }]},
11711171
xaxis4: {type: 'date', rangebreaks: [{pattern: 'day of week', bounds: [1, '-1'] }]},
1172-
xaxis5: {type: 'date', rangebreaks: [{pattern: 'day of week', bounds: [1, '-.001'] }]},
1172+
xaxis5: {type: 'date', rangebreaks: [{pattern: 'day of week', bounds: [1, '-.25'] }]},
11731173
xaxis6: {type: 'date', rangebreaks: [{pattern: 'day of week', bounds: [1, '7'] }]},
1174-
xaxis7: {type: 'date', rangebreaks: [{pattern: 'day of week', bounds: [1, '6.999'] }]}
1174+
xaxis7: {type: 'date', rangebreaks: [{pattern: 'day of week', bounds: [1, '6.75'] }]},
1175+
xaxis8: {type: 'date', rangebreaks: [{pattern: 'day of week', bounds: [1, ''] }]},
1176+
xaxis9: {type: 'date', rangebreaks: [{pattern: 'day of week', bounds: [1, null] }]},
1177+
xaxis10: {type: 'date', rangebreaks: [{pattern: 'day of week', bounds: [1, false] }]},
1178+
xaxis11: {type: 'date', rangebreaks: [{pattern: 'day of week', bounds: [1, true] }]}
11751179
};
1176-
layoutOut._subplots.xaxis.push('x2', 'x3', 'x4', 'x5', 'x6', 'x7');
1180+
layoutOut._subplots.xaxis.push('x2', 'x3', 'x4', 'x5', 'x6', 'x7', 'x8', 'x9', 'x10', 'x11');
11771181
supplyLayoutDefaults(layoutIn, layoutOut, fullData);
11781182

11791183
expect(layoutOut.xaxis.rangebreaks[0].enabled).toBe(true, 'valid');
@@ -1185,34 +1189,44 @@ describe('Test axes', function() {
11851189
expect(layoutOut.xaxis4.rangebreaks[0].enabled).toBe(false, 'reject bound < 0');
11861190
expect(layoutOut.xaxis5.rangebreaks[0].enabled).toBe(false, 'reject bound < 0');
11871191
expect(layoutOut.xaxis6.rangebreaks[0].enabled).toBe(false, 'reject bound >= 7');
1188-
expect(layoutOut.xaxis7.rangebreaks[0].enabled).toBe(true, 'do not reject bound < 7');
1192+
expect(layoutOut.xaxis7.rangebreaks[0].enabled).toBe(false, 'reject bound < 7 - not supported yet');
1193+
expect(layoutOut.xaxis8.rangebreaks[0].enabled).toBe(false, 'reject blank string');
1194+
expect(layoutOut.xaxis9.rangebreaks[0].enabled).toBe(false, 'reject null');
1195+
expect(layoutOut.xaxis10.rangebreaks[0].enabled).toBe(false, 'reject false');
1196+
expect(layoutOut.xaxis11.rangebreaks[0].enabled).toBe(false, 'reject true');
11891197
});
11901198

11911199
it('should validate inputs in respect to *hour* pattern', function() {
11921200
layoutIn = {
1193-
xaxis: {type: 'date', rangebreaks: [{pattern: 'hour', bounds: ['23.999', '0'] }]},
1201+
xaxis: {type: 'date', rangebreaks: [{pattern: 'hour', bounds: ['24', '1e-3'] }]},
11941202
xaxis2: {type: 'date', rangebreaks: [{pattern: 'hour', bounds: [1] }]},
11951203
xaxis3: {type: 'date', rangebreaks: [{pattern: 'hour', bounds: [1, 2, 3] }]},
11961204
xaxis4: {type: 'date', rangebreaks: [{pattern: 'hour', bounds: [1, '-1'] }]},
11971205
xaxis5: {type: 'date', rangebreaks: [{pattern: 'hour', bounds: [1, '-.001'] }]},
11981206
xaxis6: {type: 'date', rangebreaks: [{pattern: 'hour', bounds: [1, '24.001'] }]},
11991207
xaxis7: {type: 'date', rangebreaks: [{pattern: 'hour', bounds: [1, '23.999'] }]},
1200-
xaxis8: {type: 'date', rangebreaks: [{pattern: 'hour', bounds: [1, '24'] }]}
1208+
xaxis8: {type: 'date', rangebreaks: [{pattern: 'day of week', bounds: [1, ''] }]},
1209+
xaxis9: {type: 'date', rangebreaks: [{pattern: 'day of week', bounds: [1, null] }]},
1210+
xaxis10: {type: 'date', rangebreaks: [{pattern: 'day of week', bounds: [1, false] }]},
1211+
xaxis11: {type: 'date', rangebreaks: [{pattern: 'day of week', bounds: [1, true] }]}
12011212
};
1202-
layoutOut._subplots.xaxis.push('x2', 'x3', 'x4', 'x5', 'x6', 'x7', 'x8');
1213+
layoutOut._subplots.xaxis.push('x2', 'x3', 'x4', 'x5', 'x6', 'x7', 'x8', 'x9', 'x10', 'x11');
12031214
supplyLayoutDefaults(layoutIn, layoutOut, fullData);
12041215

12051216
expect(layoutOut.xaxis.rangebreaks[0].enabled).toBe(true, 'valid');
1206-
expect(layoutOut.xaxis.rangebreaks[0].bounds[0]).toBe('23.999', 'do not cast float to int');
1207-
expect(layoutOut.xaxis.rangebreaks[0].bounds[1]).toBe('0', 'do not cast string to int');
1217+
expect(layoutOut.xaxis.rangebreaks[0].bounds[0]).toBe(24, 'accept 24');
1218+
expect(layoutOut.xaxis.rangebreaks[0].bounds[1]).toBe(0.001, 'cast string to float');
12081219
expect(layoutOut.xaxis2.rangebreaks[0].enabled).toBe(false, 'reject bounds.length < 2');
12091220
expect(layoutOut.xaxis3.rangebreaks[0].enabled).toBe(true, 'do not reject bounds.length > 2');
12101221
expect(layoutOut.xaxis3.rangebreaks[0].bounds.length).toBe(2, 'pick first two');
12111222
expect(layoutOut.xaxis4.rangebreaks[0].enabled).toBe(false, 'reject bound < 0');
12121223
expect(layoutOut.xaxis5.rangebreaks[0].enabled).toBe(false, 'reject bound < 0');
12131224
expect(layoutOut.xaxis6.rangebreaks[0].enabled).toBe(false, 'reject bound > 24');
12141225
expect(layoutOut.xaxis7.rangebreaks[0].enabled).toBe(true, 'do not reject bound <= 24');
1215-
expect(layoutOut.xaxis8.rangebreaks[0].enabled).toBe(true, 'do not reject 24');
1226+
expect(layoutOut.xaxis8.rangebreaks[0].enabled).toBe(false, 'reject blank string');
1227+
expect(layoutOut.xaxis9.rangebreaks[0].enabled).toBe(false, 'reject null');
1228+
expect(layoutOut.xaxis10.rangebreaks[0].enabled).toBe(false, 'reject false');
1229+
expect(layoutOut.xaxis11.rangebreaks[0].enabled).toBe(false, 'reject true');
12161230
});
12171231
});
12181232

0 commit comments

Comments
 (0)