Skip to content

Commit 3522686

Browse files
teromansimonbrunel
authored andcommitted
Fix min and max option checks in linear scales (#5209)
When 0, the min and max options was considered not being set, instead we should check for null or undefined.
1 parent 493eaa8 commit 3522686

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/scales/scale.linearbase.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function generateTicks(generationOptions, dataRange) {
3636
var niceMax = Math.ceil(dataRange.max / spacing) * spacing;
3737

3838
// If min, max and stepSize is set and they make an evenly spaced scale use it.
39-
if (generationOptions.min && generationOptions.max && generationOptions.stepSize) {
39+
if (!helpers.isNullOrUndef(generationOptions.min) && !helpers.isNullOrUndef(generationOptions.max) && generationOptions.stepSize) {
4040
// If very close to our whole number, use it.
4141
if (helpers.almostWhole((generationOptions.max - generationOptions.min) / generationOptions.stepSize, spacing / 1000)) {
4242
niceMin = generationOptions.min;

test/specs/scale.linear.tests.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,4 +965,60 @@ describe('Linear Scale', function() {
965965
expect(chart.scales['x-axis-0'].min).toEqual(20);
966966
expect(chart.scales['x-axis-0'].max).toEqual(21);
967967
});
968+
969+
it('min settings should be used if set to zero', function() {
970+
var barData = {
971+
labels: ['S1', 'S2', 'S3'],
972+
datasets: [{
973+
label: 'dataset 1',
974+
backgroundColor: '#382765',
975+
data: [2500, 2000, 1500]
976+
}]
977+
};
978+
979+
var chart = window.acquireChart({
980+
type: 'horizontalBar',
981+
data: barData,
982+
options: {
983+
scales: {
984+
xAxes: [{
985+
ticks: {
986+
min: 0,
987+
max: 3000
988+
}
989+
}]
990+
}
991+
}
992+
});
993+
994+
expect(chart.scales['x-axis-0'].min).toEqual(0);
995+
});
996+
997+
it('max settings should be used if set to zero', function() {
998+
var barData = {
999+
labels: ['S1', 'S2', 'S3'],
1000+
datasets: [{
1001+
label: 'dataset 1',
1002+
backgroundColor: '#382765',
1003+
data: [-2500, -2000, -1500]
1004+
}]
1005+
};
1006+
1007+
var chart = window.acquireChart({
1008+
type: 'horizontalBar',
1009+
data: barData,
1010+
options: {
1011+
scales: {
1012+
xAxes: [{
1013+
ticks: {
1014+
min: -3000,
1015+
max: 0
1016+
}
1017+
}]
1018+
}
1019+
}
1020+
});
1021+
1022+
expect(chart.scales['x-axis-0'].max).toEqual(0);
1023+
});
9681024
});

0 commit comments

Comments
 (0)