]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fixed calculation of scale min and max when dataset contains no values (#4064)
authorGabrielMancik <gabriel.mancik@gmail.com>
Sat, 15 Apr 2017 16:37:49 +0000 (18:37 +0200)
committerEvert Timberg <evert.timberg+github@gmail.com>
Sat, 15 Apr 2017 16:37:49 +0000 (12:37 -0400)
* 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

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

index dc700f304ee52ea4a32d25e2254854e20d5b36e1..b87272177fc1eee6e0785d8a383d319ba42b8107 100644 (file)
@@ -30,13 +30,21 @@ module.exports = function(Chart) {
                        if (tickOpts.min !== undefined) {
                                me.min = tickOpts.min;
                        } else if (tickOpts.suggestedMin !== undefined) {
-                               me.min = Math.min(me.min, tickOpts.suggestedMin);
+                               if (me.min === null) {
+                                       me.min = tickOpts.suggestedMin;
+                               } else {
+                                       me.min = Math.min(me.min, tickOpts.suggestedMin);
+                               }
                        }
 
                        if (tickOpts.max !== undefined) {
                                me.max = tickOpts.max;
                        } else if (tickOpts.suggestedMax !== undefined) {
-                               me.max = Math.max(me.max, tickOpts.suggestedMax);
+                               if (me.max === null) {
+                                       me.max = tickOpts.suggestedMax;
+                               } else {
+                                       me.max = Math.max(me.max, tickOpts.suggestedMax);
+                               }
                        }
 
                        if (me.min === me.max) {
index e94741cf6a0e6fd824d50b3846ae7efbefa5f3e1..c63cad338f6b0e6553711aa0f1e869134a46edc8 100644 (file)
@@ -117,6 +117,35 @@ describe('Linear Scale', function() {
                expect(chart.scales.yScale0.max).toBe(150);
        });
 
+       it('Should correctly determine the max & min when no values provided and suggested minimum and maximum are set', function() {
+               var chart = window.acquireChart({
+                       type: 'bar',
+                       data: {
+                               datasets: [{
+                                       yAxisID: 'yScale0',
+                                       data: []
+                               }],
+                               labels: ['a', 'b', 'c', 'd', 'e', 'f']
+                       },
+                       options: {
+                               scales: {
+                                       yAxes: [{
+                                               id: 'yScale0',
+                                               type: 'linear',
+                                               ticks: {
+                                                       suggestedMin: -10,
+                                                       suggestedMax: 15
+                                               }
+                                       }]
+                               }
+                       }
+               });
+
+               expect(chart.scales.yScale0).not.toEqual(undefined); // must construct
+               expect(chart.scales.yScale0.min).toBe(-10);
+               expect(chart.scales.yScale0.max).toBe(15);
+       });
+
        it('Should correctly determine the max & min data values ignoring hidden datasets', function() {
                var chart = window.acquireChart({
                        type: 'bar',