Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore invalid log scale min and max #6058

Merged
merged 7 commits into from Feb 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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