From: Jukka Kurkela Date: Wed, 7 Oct 2020 21:37:10 +0000 (+0300) Subject: beginAtZero support for logarithmic (#7862) X-Git-Tag: v3.0.0-beta.4~16 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f1ed2ee932621d8d7d3a5ea81b9bd30992294e38;p=thirdparty%2FChart.js.git beginAtZero support for logarithmic (#7862) --- diff --git a/src/scales/scale.logarithmic.js b/src/scales/scale.logarithmic.js index 98942f9b6..57bbafa78 100644 --- a/src/scales/scale.logarithmic.js +++ b/src/scales/scale.logarithmic.js @@ -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) { diff --git a/test/fixtures/controller.bar/stacking/logarithmic-strings.png b/test/fixtures/controller.bar/stacking/logarithmic-strings.png index 52dae9b93..377b6c59f 100644 Binary files a/test/fixtures/controller.bar/stacking/logarithmic-strings.png and b/test/fixtures/controller.bar/stacking/logarithmic-strings.png differ diff --git a/test/fixtures/controller.bar/stacking/logarithmic.png b/test/fixtures/controller.bar/stacking/logarithmic.png index 52dae9b93..377b6c59f 100644 Binary files a/test/fixtures/controller.bar/stacking/logarithmic.png and b/test/fixtures/controller.bar/stacking/logarithmic.png differ diff --git a/test/specs/scale.logarithmic.tests.js b/test/specs/scale.logarithmic.tests.js index 19a616ab2..df169c954 100644 --- a/test/specs/scale.logarithmic.tests.js +++ b/test/specs/scale.logarithmic.tests.js @@ -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';