Skip to content

Commit

Permalink
Fix min and max option checks in linear scales (chartjs#5209)
Browse files Browse the repository at this point in the history
When 0, the min and max options was considered not being set, instead we should check for null or undefined.
  • Loading branch information
teroman authored and simonbrunel committed Jul 29, 2018
1 parent 75c90a4 commit 86eb862
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/scales/scale.linearbase.js
Expand Up @@ -36,7 +36,7 @@ function generateTicks(generationOptions, dataRange) {
var niceMax = Math.ceil(dataRange.max / spacing) * spacing;

// If min, max and stepSize is set and they make an evenly spaced scale use it.
if (generationOptions.min && generationOptions.max && generationOptions.stepSize) {
if (!helpers.isNullOrUndef(generationOptions.min) && !helpers.isNullOrUndef(generationOptions.max) && generationOptions.stepSize) {
// If very close to our whole number, use it.
if (helpers.almostWhole((generationOptions.max - generationOptions.min) / generationOptions.stepSize, spacing / 1000)) {
niceMin = generationOptions.min;
Expand Down
56 changes: 56 additions & 0 deletions test/specs/scale.linear.tests.js
Expand Up @@ -965,4 +965,60 @@ describe('Linear Scale', function() {
expect(chart.scales['x-axis-0'].min).toEqual(20);
expect(chart.scales['x-axis-0'].max).toEqual(21);
});

it('min settings should be used if set to zero', function() {
var barData = {
labels: ['S1', 'S2', 'S3'],
datasets: [{
label: 'dataset 1',
backgroundColor: '#382765',
data: [2500, 2000, 1500]
}]
};

var chart = window.acquireChart({
type: 'horizontalBar',
data: barData,
options: {
scales: {
xAxes: [{
ticks: {
min: 0,
max: 3000
}
}]
}
}
});

expect(chart.scales['x-axis-0'].min).toEqual(0);
});

it('max settings should be used if set to zero', function() {
var barData = {
labels: ['S1', 'S2', 'S3'],
datasets: [{
label: 'dataset 1',
backgroundColor: '#382765',
data: [-2500, -2000, -1500]
}]
};

var chart = window.acquireChart({
type: 'horizontalBar',
data: barData,
options: {
scales: {
xAxes: [{
ticks: {
min: -3000,
max: 0
}
}]
}
}
});

expect(chart.scales['x-axis-0'].max).toEqual(0);
});
});

0 comments on commit 86eb862

Please sign in to comment.