]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
When all datasets are hidden, the linear scale defaults to a range of 0 - 1.
authoretimberg <evert.timberg@gmail.com>
Sun, 25 Jun 2017 13:06:22 +0000 (09:06 -0400)
committerEvert Timberg <evert.timberg+github@gmail.com>
Sun, 25 Jun 2017 17:32:08 +0000 (13:32 -0400)
If `ticks.min` was set this would not set the range correctly.
Added a test to cover this case as well

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

index da7c58cd1b674d88d3a82243186c0e4bf0b4530a..c9a6af41ed3b4b3d790583380b91e05b94335dc4 100644 (file)
@@ -124,8 +124,8 @@ module.exports = function(Chart) {
                                });
                        }
 
-                       me.min = isFinite(me.min) ? me.min : DEFAULT_MIN;
-                       me.max = isFinite(me.max) ? me.max : DEFAULT_MAX;
+                       me.min = isFinite(me.min) && !isNaN(me.min) ? me.min : DEFAULT_MIN;
+                       me.max = isFinite(me.max) && !isNaN(me.max) ? me.max : DEFAULT_MAX;
 
                        // Common base implementation to handle ticks.min, ticks.max, ticks.beginAtZero
                        this.handleTickRangeOptions();
index dba0a6f09ebda615dd07c9fbb388cd660d9b5fe0..28cde52d6559db7d82fab1511e594dca746e9d8c 100644 (file)
@@ -27,6 +27,9 @@ module.exports = function(Chart) {
                                }
                        }
 
+                       var setMin = tickOpts.min !== undefined || tickOpts.suggestedMin !== undefined;
+                       var setMax = tickOpts.max !== undefined || tickOpts.suggestedMax !== undefined;
+
                        if (tickOpts.min !== undefined) {
                                me.min = tickOpts.min;
                        } else if (tickOpts.suggestedMin !== undefined) {
@@ -47,6 +50,20 @@ module.exports = function(Chart) {
                                }
                        }
 
+                       if (setMin !== setMax) {
+                               // We set the min or the max but not both.
+                               // So ensure that our range is good
+                               // Inverted or 0 length range can happen when
+                               // ticks.min is set, and no datasets are visible
+                               if (me.min >= me.max) {
+                                       if (setMin) {
+                                               me.max = me.min + 1;
+                                       } else {
+                                               me.min = me.max - 1;
+                                       }
+                               }
+                       }
+
                        if (me.min === me.max) {
                                me.max++;
 
index 2a80c99a2cf3b9358d5704bcff4f8db73094d098..5b37a0c11f7199d7726d54c2f5963866a1b8ae92 100644 (file)
@@ -877,4 +877,33 @@ describe('Linear Scale', function() {
                expect(chart.scales['x-axis-0'].min).toEqual(0);
                expect(chart.scales['x-axis-0'].max).toEqual(1);
        });
+
+       it('max and min value should be valid when min is set and all datasets are hidden', function() {
+               var barData = {
+                       labels: ['S1', 'S2', 'S3'],
+                       datasets: [{
+                               label: 'dataset 1',
+                               backgroundColor: '#382765',
+                               data: [2500, 2000, 1500],
+                               hidden: true,
+                       }]
+               };
+
+               var chart = window.acquireChart({
+                       type: 'horizontalBar',
+                       data: barData,
+                       options: {
+                               scales: {
+                                       xAxes: [{
+                                               ticks: {
+                                                       min: 20
+                                               }
+                                       }]
+                               }
+                       }
+               });
+
+               expect(chart.scales['x-axis-0'].min).toEqual(20);
+               expect(chart.scales['x-axis-0'].max).toEqual(21);
+       });
 });