Skip to content

Commit 254bd4b

Browse files
GabrielManciketimberg
authored andcommitted
Fixed calculation of scale min and max when dataset contains no values (#4064)
* Fixed different calculation of scale min and max when dataset contains no values * Removed trailing spaces * Added test for correct min/max calculation * Removed trailing spaces
1 parent ecac839 commit 254bd4b

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/scales/scale.linearbase.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,21 @@ module.exports = function(Chart) {
3030
if (tickOpts.min !== undefined) {
3131
me.min = tickOpts.min;
3232
} else if (tickOpts.suggestedMin !== undefined) {
33-
me.min = Math.min(me.min, tickOpts.suggestedMin);
33+
if (me.min === null) {
34+
me.min = tickOpts.suggestedMin;
35+
} else {
36+
me.min = Math.min(me.min, tickOpts.suggestedMin);
37+
}
3438
}
3539

3640
if (tickOpts.max !== undefined) {
3741
me.max = tickOpts.max;
3842
} else if (tickOpts.suggestedMax !== undefined) {
39-
me.max = Math.max(me.max, tickOpts.suggestedMax);
43+
if (me.max === null) {
44+
me.max = tickOpts.suggestedMax;
45+
} else {
46+
me.max = Math.max(me.max, tickOpts.suggestedMax);
47+
}
4048
}
4149

4250
if (me.min === me.max) {

test/specs/scale.linear.tests.js

+29
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,35 @@ describe('Linear Scale', function() {
117117
expect(chart.scales.yScale0.max).toBe(150);
118118
});
119119

120+
it('Should correctly determine the max & min when no values provided and suggested minimum and maximum are set', function() {
121+
var chart = window.acquireChart({
122+
type: 'bar',
123+
data: {
124+
datasets: [{
125+
yAxisID: 'yScale0',
126+
data: []
127+
}],
128+
labels: ['a', 'b', 'c', 'd', 'e', 'f']
129+
},
130+
options: {
131+
scales: {
132+
yAxes: [{
133+
id: 'yScale0',
134+
type: 'linear',
135+
ticks: {
136+
suggestedMin: -10,
137+
suggestedMax: 15
138+
}
139+
}]
140+
}
141+
}
142+
});
143+
144+
expect(chart.scales.yScale0).not.toEqual(undefined); // must construct
145+
expect(chart.scales.yScale0.min).toBe(-10);
146+
expect(chart.scales.yScale0.max).toBe(15);
147+
});
148+
120149
it('Should correctly determine the max & min data values ignoring hidden datasets', function() {
121150
var chart = window.acquireChart({
122151
type: 'bar',

0 commit comments

Comments
 (0)