Ignore invalid log scale min and max (#6058)

This commit is contained in:
Ben McCann 2019-02-24 01:59:21 -08:00 committed by Simon Brunel
parent f3b18373e6
commit 317cae11dc
2 changed files with 71 additions and 4 deletions

View File

@ -62,6 +62,11 @@ var defaultConfig = {
}
};
// TODO(v3): change this to positiveOrDefault
function nonNegativeOrDefault(value, defaultValue) {
return helpers.isFinite(value) && value >= 0 ? value : defaultValue;
}
module.exports = Scale.extend({
determineDataLimits: function() {
var me = this;
@ -174,8 +179,8 @@ module.exports = Scale.extend({
var DEFAULT_MIN = 1;
var DEFAULT_MAX = 10;
me.min = valueOrDefault(tickOpts.min, me.min);
me.max = valueOrDefault(tickOpts.max, me.max);
me.min = nonNegativeOrDefault(tickOpts.min, me.min);
me.max = nonNegativeOrDefault(tickOpts.max, me.max);
if (me.min === me.max) {
if (me.min !== 0 && me.min !== null) {
@ -211,8 +216,8 @@ module.exports = Scale.extend({
var reverse = !me.isHorizontal();
var generationOptions = {
min: tickOpts.min,
max: tickOpts.max
min: nonNegativeOrDefault(tickOpts.min),
max: nonNegativeOrDefault(tickOpts.max)
};
var ticks = me.ticks = generateTicks(generationOptions, me);

View File

@ -477,6 +477,68 @@ describe('Logarithmic Scale tests', function() {
expect(yScale.ticks[tickCount - 1]).toBe(10);
});
it('should ignore negative min and max options', function() {
var chart = window.acquireChart({
type: 'bar',
data: {
datasets: [{
data: [1, 1, 1, 2, 1, 0]
}],
labels: []
},
options: {
scales: {
yAxes: [{
id: 'yScale',
type: 'logarithmic',
ticks: {
min: -10,
max: -1010,
callback: function(value) {
return value;
}
}
}]
}
}
});
var yScale = chart.scales.yScale;
expect(yScale.min).toBe(0);
expect(yScale.max).toBe(2);
});
it('should ignore invalid min and max options', function() {
var chart = window.acquireChart({
type: 'bar',
data: {
datasets: [{
data: [1, 1, 1, 2, 1, 0]
}],
labels: []
},
options: {
scales: {
yAxes: [{
id: 'yScale',
type: 'logarithmic',
ticks: {
min: '',
max: false,
callback: function(value) {
return value;
}
}
}]
}
}
});
var yScale = chart.scales.yScale;
expect(yScale.min).toBe(0);
expect(yScale.max).toBe(2);
});
it('should generate tick marks', function() {
var chart = window.acquireChart({
type: 'bar',