Skip to content

Commit 595a00c

Browse files
committed
use null in ranges to set defaults
1 parent cb55c08 commit 595a00c

File tree

4 files changed

+63
-14
lines changed

4 files changed

+63
-14
lines changed

src/plots/cartesian/axis_defaults.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,19 @@ module.exports = function handleAxisDefaults(containerIn, containerOut, coerce,
9292

9393
setConvert(containerOut, layoutOut);
9494

95-
var autorangeDflt = !containerOut.isValidRange(containerIn.range);
96-
if(autorangeDflt && options.reverseDflt) autorangeDflt = 'reversed';
95+
coerce('minallowed');
96+
coerce('maxallowed');
97+
var range = coerce('range');
98+
99+
var autorangeDflt = containerOut.getAutorangeDflt(range, options);
100+
97101
var autoRange = coerce('autorange', autorangeDflt);
98102
if(autoRange) {
99103
handleAutorangeOptionsDefaults(coerce);
100104

101105
if(axType === 'linear' || axType === '-') coerce('rangemode');
102106
}
103107

104-
coerce('minallowed');
105-
coerce('maxallowed');
106-
coerce('range');
107108
containerOut.cleanRange();
108109

109110
handleCategoryOrderDefaults(containerIn, containerOut, coerce, options);

src/plots/cartesian/set_convert.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -877,15 +877,30 @@ module.exports = function setConvert(ax, fullLayout) {
877877
return arrayOut;
878878
};
879879

880-
ax.isValidRange = function(range) {
880+
ax.isValidRange = function(range, nullOk) {
881881
return (
882882
Array.isArray(range) &&
883883
range.length === 2 &&
884-
isNumeric(ax.r2l(range[0])) &&
885-
isNumeric(ax.r2l(range[1]))
884+
((nullOk && range[0] === null) || isNumeric(ax.r2l(range[0]))) &&
885+
((nullOk && range[1] === null) || isNumeric(ax.r2l(range[1])))
886886
);
887887
};
888888

889+
ax.getAutorangeDflt = function(range, options) {
890+
var autorangeDflt = !ax.isValidRange(range, 'nullOk');
891+
if(autorangeDflt && options && options.reverseDflt) autorangeDflt = 'reversed';
892+
else if(range) {
893+
if(range[0] === null && range[1] === null) {
894+
autorangeDflt = true;
895+
} else if(range[0] === null && range[1] !== null) {
896+
autorangeDflt = 'min';
897+
} else if(range[0] !== null && range[1] === null) {
898+
autorangeDflt = 'max';
899+
}
900+
}
901+
return autorangeDflt;
902+
};
903+
889904
ax.isPtWithinRange = function(d, calendar) {
890905
var coord = ax.c2l(d[axLetter], null, calendar);
891906
var r0 = ax.r2l(ax.range[0]);

src/plots/polar/layout_defaults.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,13 @@ function handleDefaults(contIn, contOut, coerce, opts) {
7777

7878
switch(axName) {
7979
case 'radialaxis':
80-
var autoRange = coerceAxis('autorange', !axOut.isValidRange(axIn.range));
80+
coerceAxis('minallowed');
81+
coerceAxis('maxallowed');
82+
var range = coerceAxis('range');
83+
84+
var autorangeDflt = axOut.getAutorangeDflt(range);
85+
86+
var autoRange = coerceAxis('autorange', autorangeDflt);
8187
axIn.autorange = autoRange;
8288
if(autoRange) {
8389
handleAutorangeOptionsDefaults(coerceAxis);
@@ -86,9 +92,6 @@ function handleDefaults(contIn, contOut, coerce, opts) {
8692
if(autoRange === 'reversed') axOut._m = -1;
8793
}
8894

89-
coerceAxis('minallowed');
90-
coerceAxis('maxallowed');
91-
coerceAxis('range');
9295
axOut.cleanRange('range', {dfltRange: [0, 1]});
9396
break;
9497

test/jasmine/tests/axes_test.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -718,10 +718,10 @@ describe('Test axes', function() {
718718
it('should set autorange to true when input range is invalid', function() {
719719
layoutIn = {
720720
xaxis: { range: 'not-gonna-work' },
721-
xaxis2: { range: [1, 2, 3] },
721+
xaxis2: { range: [1] },
722722
yaxis: { range: ['a', 2] },
723723
yaxis2: { range: [1, 'b'] },
724-
yaxis3: { range: [null, {}] }
724+
yaxis3: { range: [undefined, {}] }
725725
};
726726
layoutOut._subplots.cartesian.push('x2y2', 'xy3');
727727
layoutOut._subplots.yaxis.push('x2', 'y2', 'y3');
@@ -750,6 +750,36 @@ describe('Test axes', function() {
750750
});
751751
});
752752

753+
it('should set autorange to true when range[0] and range[1] are set to null', function() {
754+
layoutIn = {
755+
xaxis: { range: [null, null] }
756+
};
757+
758+
supplyLayoutDefaults(layoutIn, layoutOut, fullData);
759+
760+
expect(layoutOut.xaxis.autorange).toBe(true);
761+
});
762+
763+
it('should set autorange to min when range[0] is set to null', function() {
764+
layoutIn = {
765+
xaxis: { range: [null, 1] }
766+
};
767+
768+
supplyLayoutDefaults(layoutIn, layoutOut, fullData);
769+
770+
expect(layoutOut.xaxis.autorange).toBe('min');
771+
});
772+
773+
it('should set autorange to max when range[1] is set to null', function() {
774+
layoutIn = {
775+
xaxis: { range: [1, null] }
776+
};
777+
778+
supplyLayoutDefaults(layoutIn, layoutOut, fullData);
779+
780+
expect(layoutOut.xaxis.autorange).toBe('max');
781+
});
782+
753783
it('only allows rangemode with linear axes', function() {
754784
layoutIn = {
755785
xaxis: {type: 'log', rangemode: 'tozero'},

0 commit comments

Comments
 (0)