Skip to content

Commit

Permalink
Ignore invalid log scale min and max (#6058)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann authored and simonbrunel committed Feb 24, 2019
1 parent f3b1837 commit 317cae1
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/scales/scale.logarithmic.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

Expand Down
62 changes: 62 additions & 0 deletions test/specs/scale.logarithmic.tests.js
Expand Up @@ -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',
Expand Down

0 comments on commit 317cae1

Please sign in to comment.