]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fix min and max option checks in linear scales (#5209)
authorColin <34158322+teroman@users.noreply.github.com>
Sun, 29 Jul 2018 16:09:16 +0000 (17:09 +0100)
committerSimon Brunel <simonbrunel@users.noreply.github.com>
Sun, 29 Jul 2018 16:09:16 +0000 (18:09 +0200)
When 0, the min and max options was considered not being set, instead we should check for null or undefined.

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

index ce53da7b98dd9a05f8fa32325a40da7caf0c131f..c78dc64f298f15a385d4b6a3ffc52b85ba291cf4 100644 (file)
@@ -36,7 +36,7 @@ function generateTicks(generationOptions, dataRange) {
        var niceMax = Math.ceil(dataRange.max / spacing) * spacing;
 
        // If min, max and stepSize is set and they make an evenly spaced scale use it.
-       if (generationOptions.min && generationOptions.max && generationOptions.stepSize) {
+       if (!helpers.isNullOrUndef(generationOptions.min) && !helpers.isNullOrUndef(generationOptions.max) && generationOptions.stepSize) {
                // If very close to our whole number, use it.
                if (helpers.almostWhole((generationOptions.max - generationOptions.min) / generationOptions.stepSize, spacing / 1000)) {
                        niceMin = generationOptions.min;
index b42675d8a8d612e85a8b363991f5ddd1e855cafa..ab046078391eb592bbe7a8f235f68158a89ebd48 100644 (file)
@@ -965,4 +965,60 @@ describe('Linear Scale', function() {
                expect(chart.scales['x-axis-0'].min).toEqual(20);
                expect(chart.scales['x-axis-0'].max).toEqual(21);
        });
+
+       it('min settings should be used if set to zero', function() {
+               var barData = {
+                       labels: ['S1', 'S2', 'S3'],
+                       datasets: [{
+                               label: 'dataset 1',
+                               backgroundColor: '#382765',
+                               data: [2500, 2000, 1500]
+                       }]
+               };
+
+               var chart = window.acquireChart({
+                       type: 'horizontalBar',
+                       data: barData,
+                       options: {
+                               scales: {
+                                       xAxes: [{
+                                               ticks: {
+                                                       min: 0,
+                                                       max: 3000
+                                               }
+                                       }]
+                               }
+                       }
+               });
+
+               expect(chart.scales['x-axis-0'].min).toEqual(0);
+       });
+
+       it('max settings should be used if set to zero', function() {
+               var barData = {
+                       labels: ['S1', 'S2', 'S3'],
+                       datasets: [{
+                               label: 'dataset 1',
+                               backgroundColor: '#382765',
+                               data: [-2500, -2000, -1500]
+                       }]
+               };
+
+               var chart = window.acquireChart({
+                       type: 'horizontalBar',
+                       data: barData,
+                       options: {
+                               scales: {
+                                       xAxes: [{
+                                               ticks: {
+                                                       min: -3000,
+                                                       max: 0
+                                               }
+                                       }]
+                               }
+                       }
+               });
+
+               expect(chart.scales['x-axis-0'].max).toEqual(0);
+       });
 });