Skip to content

Commit 03f65d4

Browse files
kurkleetimberg
authored andcommitted
Linear scale: use suggested limits as defaults (#6892)
* Linear scale: use suggested limits as defaults * Review update
1 parent 27129db commit 03f65d4

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

src/scales/scale.linear.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

3-
import helpers from '../helpers/index';
3+
import {isFinite, valueOrDefault} from '../helpers/helpers.core';
4+
import {_parseFont} from '../helpers/helpers.options';
45
import LinearScaleBase from './scale.linearbase';
56
import Ticks from '../core/core.ticks';
67

@@ -12,18 +13,17 @@ const defaultConfig = {
1213

1314
class LinearScale extends LinearScaleBase {
1415
determineDataLimits() {
15-
var me = this;
16-
var DEFAULT_MIN = 0;
17-
var DEFAULT_MAX = 1;
18-
var minmax = me._getMinMax(true);
19-
var min = minmax.min;
20-
var max = minmax.max;
16+
const me = this;
17+
const options = me.options;
18+
const minmax = me._getMinMax(true);
19+
let min = minmax.min;
20+
let max = minmax.max;
2121

22-
me.min = helpers.isFinite(min) && !isNaN(min) ? min : DEFAULT_MIN;
23-
me.max = helpers.isFinite(max) && !isNaN(max) ? max : DEFAULT_MAX;
22+
me.min = isFinite(min) ? min : valueOrDefault(options.suggestedMin, 0);
23+
me.max = isFinite(max) ? max : valueOrDefault(options.suggestedMax, 1);
2424

2525
// Backward compatible inconsistent min for stacked
26-
if (me.options.stacked && min > 0) {
26+
if (options.stacked && min > 0) {
2727
me.min = 0;
2828
}
2929

@@ -39,7 +39,7 @@ class LinearScale extends LinearScaleBase {
3939
if (me.isHorizontal()) {
4040
return Math.ceil(me.width / 40);
4141
}
42-
tickFont = helpers.options._parseFont(me.options.ticks);
42+
tickFont = _parseFont(me.options.ticks);
4343
return Math.ceil(me.height / tickFont.lineHeight);
4444
}
4545

test/specs/scale.linear.tests.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,27 @@ describe('Linear Scale', function() {
141141
expect(chart.scales.y.max).toBe(15);
142142
});
143143

144+
it('Should correctly determine the max & min when no datasets are associated and suggested minimum and maximum are set', function() {
145+
var chart = window.acquireChart({
146+
type: 'bar',
147+
data: {
148+
datasets: []
149+
},
150+
options: {
151+
scales: {
152+
y: {
153+
type: 'linear',
154+
suggestedMin: -10,
155+
suggestedMax: 0
156+
}
157+
}
158+
}
159+
});
160+
161+
expect(chart.scales.y.min).toBe(-10);
162+
expect(chart.scales.y.max).toBe(0);
163+
});
164+
144165
it('Should correctly determine the max & min data values ignoring hidden datasets', function() {
145166
var chart = window.acquireChart({
146167
type: 'bar',

0 commit comments

Comments
 (0)