]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
beginAtZero support for logarithmic (#7862)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Wed, 7 Oct 2020 21:37:10 +0000 (00:37 +0300)
committerGitHub <noreply@github.com>
Wed, 7 Oct 2020 21:37:10 +0000 (17:37 -0400)
src/scales/scale.logarithmic.js
test/fixtures/controller.bar/stacking/logarithmic-strings.png
test/fixtures/controller.bar/stacking/logarithmic.png
test/specs/scale.logarithmic.tests.js

index 98942f9b60e93befe7d56acd4aaab22a8dd41acb..57bbafa78bb9f3deb3bf0cb67e441a8fe9b63590 100644 (file)
@@ -64,6 +64,7 @@ export default class LogarithmicScale extends Scale {
        parse(raw, index) {
                const value = LinearScaleBase.prototype.parse.apply(this, [raw, index]);
                if (value === 0) {
+                       this._zero = true;
                        return undefined;
                }
                return isFinite(value) && value > 0 ? value : NaN;
@@ -101,6 +102,11 @@ export default class LogarithmicScale extends Scale {
                if (max <= 0) {
                        max = Math.pow(10, Math.floor(log10(min)) + 1);
                }
+               // if data has `0` in it or `beginAtZero` is true, and min (non zero) value is at bottom
+               // of scale, lower the min bound by one exp.
+               if (!me._userMin && me._zero && min === Math.pow(10, Math.floor(log10(me.min)))) {
+                       min = Math.pow(10, Math.floor(log10(min)) - 1);
+               }
                me.min = min;
                me.max = max;
        }
@@ -153,6 +159,7 @@ export default class LogarithmicScale extends Scale {
 
                me._startValue = log10(start);
                me._valueRange = log10(me.max) - log10(start);
+               me._zero = me.options.beginAtZero;
        }
 
        getPixelForValue(value) {
index 52dae9b93d79f8ea5f602f22caa253e1e407a583..377b6c59ff1eba426ce114ee4dac236f97362509 100644 (file)
Binary files a/test/fixtures/controller.bar/stacking/logarithmic-strings.png and b/test/fixtures/controller.bar/stacking/logarithmic-strings.png differ
index 52dae9b93d79f8ea5f602f22caa253e1e407a583..377b6c59ff1eba426ce114ee4dac236f97362509 100644 (file)
Binary files a/test/fixtures/controller.bar/stacking/logarithmic.png and b/test/fixtures/controller.bar/stacking/logarithmic.png differ
index 19a616ab2765c7eac8b6887ed872c553f683a91e..df169c954166fd00ba8a0d0e831e0b281ee84e91 100644 (file)
@@ -478,7 +478,7 @@ describe('Logarithmic Scale tests', function() {
                });
 
                var y = chart.scales.y;
-               expect(y.min).toBe(1);
+               expect(y.min).toBe(0.1);
                expect(y.max).toBe(2);
        });
 
@@ -508,7 +508,7 @@ describe('Logarithmic Scale tests', function() {
                });
 
                var y = chart.scales.y;
-               expect(y.min).toBe(1);
+               expect(y.min).toBe(0.1);
                expect(y.max).toBe(2);
        });
 
@@ -638,7 +638,7 @@ describe('Logarithmic Scale tests', function() {
                        type: 'line',
                        data: {
                                datasets: [{
-                                       data: [10, 5, 1, 25, 0, 78]
+                                       data: [10, 5, 1.1, 25, 0, 78]
                                }],
                                labels: []
                        },
@@ -751,7 +751,7 @@ describe('Logarithmic Scale tests', function() {
                                                min: 0
                                        }
                                },
-                               firstTick: 1,
+                               firstTick: 0.1,
                                describe: 'all stacks are defined and min: 0'
                        },
                        {
@@ -762,7 +762,7 @@ describe('Logarithmic Scale tests', function() {
                                                min: 0
                                        }
                                },
-                               firstTick: 1,
+                               firstTick: 0.1,
                                describe: 'not stacks are defined and min: 0'
                        },
                        {
@@ -783,7 +783,7 @@ describe('Logarithmic Scale tests', function() {
                                                min: 0
                                        }
                                },
-                               firstTick: 1,
+                               firstTick: 0.1,
                                describe: 'all stacks are defined and min: 0'
                        },
                        {
@@ -794,7 +794,7 @@ describe('Logarithmic Scale tests', function() {
                                                min: 0
                                        }
                                },
-                               firstTick: 1,
+                               firstTick: 0.1,
                                describe: 'not all stacks are defined and min: 0'
                        },
                ];
@@ -812,7 +812,8 @@ describe('Logarithmic Scale tests', function() {
                                chartEnd = 'top';
                        }
                        scaleConfig[setup.axis] = {
-                               type: 'logarithmic'
+                               type: 'logarithmic',
+                               beginAtZero: false
                        };
                        Object.assign(scaleConfig, setup.scale);
                        scaleConfig[setup.axis].type = 'logarithmic';