]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Linear scale: use suggested limits as defaults (#6892)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Fri, 3 Jan 2020 14:07:33 +0000 (16:07 +0200)
committerEvert Timberg <evert.timberg+github@gmail.com>
Fri, 3 Jan 2020 14:07:33 +0000 (09:07 -0500)
* Linear scale: use suggested limits as defaults

* Review update

src/scales/scale.linear.js
test/specs/scale.linear.tests.js

index 32183499113bbcb773bc5acac6782c363339f0f2..27a67a76f65d73316e0b2ea0d31e04eed833fce7 100644 (file)
@@ -1,6 +1,7 @@
 'use strict';
 
-import helpers from '../helpers/index';
+import {isFinite, valueOrDefault} from '../helpers/helpers.core';
+import {_parseFont} from '../helpers/helpers.options';
 import LinearScaleBase from './scale.linearbase';
 import Ticks from '../core/core.ticks';
 
@@ -12,18 +13,17 @@ const defaultConfig = {
 
 class LinearScale extends LinearScaleBase {
        determineDataLimits() {
-               var me = this;
-               var DEFAULT_MIN = 0;
-               var DEFAULT_MAX = 1;
-               var minmax = me._getMinMax(true);
-               var min = minmax.min;
-               var max = minmax.max;
+               const me = this;
+               const options = me.options;
+               const minmax = me._getMinMax(true);
+               let min = minmax.min;
+               let max = minmax.max;
 
-               me.min = helpers.isFinite(min) && !isNaN(min) ? min : DEFAULT_MIN;
-               me.max = helpers.isFinite(max) && !isNaN(max) ? max : DEFAULT_MAX;
+               me.min = isFinite(min) ? min : valueOrDefault(options.suggestedMin, 0);
+               me.max = isFinite(max) ? max : valueOrDefault(options.suggestedMax, 1);
 
                // Backward compatible inconsistent min for stacked
-               if (me.options.stacked && min > 0) {
+               if (options.stacked && min > 0) {
                        me.min = 0;
                }
 
@@ -39,7 +39,7 @@ class LinearScale extends LinearScaleBase {
                if (me.isHorizontal()) {
                        return Math.ceil(me.width / 40);
                }
-               tickFont = helpers.options._parseFont(me.options.ticks);
+               tickFont = _parseFont(me.options.ticks);
                return Math.ceil(me.height / tickFont.lineHeight);
        }
 
index 90a4f05fdf5afe94a52fc2163bea193feeb1c897..cad0e51819f0a50e22e2fbffc58849c65815e4f2 100644 (file)
@@ -141,6 +141,27 @@ describe('Linear Scale', function() {
                expect(chart.scales.y.max).toBe(15);
        });
 
+       it('Should correctly determine the max & min when no datasets are associated and suggested minimum and maximum are set', function() {
+               var chart = window.acquireChart({
+                       type: 'bar',
+                       data: {
+                               datasets: []
+                       },
+                       options: {
+                               scales: {
+                                       y: {
+                                               type: 'linear',
+                                               suggestedMin: -10,
+                                               suggestedMax: 0
+                                       }
+                               }
+                       }
+               });
+
+               expect(chart.scales.y.min).toBe(-10);
+               expect(chart.scales.y.max).toBe(0);
+       });
+
        it('Should correctly determine the max & min data values ignoring hidden datasets', function() {
                var chart = window.acquireChart({
                        type: 'bar',