]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Scale: Make sure grace is applied only once (#8913)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Sat, 17 Apr 2021 11:31:15 +0000 (14:31 +0300)
committerGitHub <noreply@github.com>
Sat, 17 Apr 2021 11:31:15 +0000 (07:31 -0400)
* Scale: Make sure grace is applied only once
* Add description to fixture

src/core/core.scale.js
src/scales/scale.linearbase.js
test/fixtures/scale.linear/grace/grace-neg.js [moved from test/fixtures/scale.linear/grace-neg.js with 100% similarity]
test/fixtures/scale.linear/grace/grace-neg.png [moved from test/fixtures/scale.linear/grace-neg.png with 100% similarity]
test/fixtures/scale.linear/grace/grace-pos.js [moved from test/fixtures/scale.linear/grace-pos.js with 100% similarity]
test/fixtures/scale.linear/grace/grace-pos.png [moved from test/fixtures/scale.linear/grace-pos.png with 100% similarity]
test/fixtures/scale.linear/grace/grace.js [moved from test/fixtures/scale.linear/grace.js with 100% similarity]
test/fixtures/scale.linear/grace/grace.png [moved from test/fixtures/scale.linear/grace.png with 100% similarity]
test/fixtures/scale.linear/grace/issue-8912.js [new file with mode: 0644]
test/fixtures/scale.linear/grace/issue-8912.png [new file with mode: 0644]

index c5c6a98a762415aa085af671b5623e50c12329ec..2eda295df0ae036806fddfccdbe7c2d68130d7fa 100644 (file)
@@ -3,7 +3,7 @@ import {_alignPixel, _measureText, renderText, clipArea, unclipArea} from '../he
 import {callback as call, each, finiteOrDefault, isArray, isFinite, isNullOrUndef, isObject} from '../helpers/helpers.core';
 import {toDegrees, toRadians, _int16Range, _limitValue, HALF_PI} from '../helpers/helpers.math';
 import {_alignStartEnd, _toLeftRightCenter} from '../helpers/helpers.extras';
-import {toFont, toPadding} from '../helpers/helpers.options';
+import {toFont, toPadding, _addGrace} from '../helpers/helpers.options';
 
 import './core.scale.defaults';
 
@@ -204,6 +204,7 @@ export default class Scale extends Element {
     this.labelRotation = undefined;
     this.min = undefined;
     this.max = undefined;
+    this._range = undefined;
     /** @type {Tick[]} */
     this.ticks = [];
     /** @type {object[]|null} */
@@ -401,6 +402,7 @@ export default class Scale extends Element {
       me.beforeDataLimits();
       me.determineDataLimits();
       me.afterDataLimits();
+      me._range = _addGrace(me, me.options.grace);
       me._dataLimitsCached = true;
     }
 
index 3a671d269cdcf9bd92270c06ceadd45b62c49adc..9eda15c33d3e3b8d677935b24a3db0f1b8be18cd 100644 (file)
@@ -2,7 +2,6 @@ import {isNullOrUndef} from '../helpers/helpers.core';
 import {almostEquals, almostWhole, niceNum, _decimalPlaces, _setMinAndMaxByKey, sign} from '../helpers/helpers.math';
 import Scale from '../core/core.scale';
 import {formatNumber} from '../helpers/helpers.intl';
-import {_addGrace} from '../helpers/helpers.options';
 
 /**
  * Generate a set of linear ticks for an axis
@@ -232,7 +231,8 @@ export default class LinearScaleBase extends Scale {
       step: tickOpts.stepSize,
       count: tickOpts.count,
     };
-    const ticks = generateTicks(numericGeneratorOptions, _addGrace(me, opts.grace));
+    const dataRange = me._range || me;
+    const ticks = generateTicks(numericGeneratorOptions, dataRange);
 
     // At this point, we need to update our max and min given the tick values,
     // since we probably have expanded the range of the scale
diff --git a/test/fixtures/scale.linear/grace/issue-8912.js b/test/fixtures/scale.linear/grace/issue-8912.js
new file mode 100644 (file)
index 0000000..3c11da4
--- /dev/null
@@ -0,0 +1,26 @@
+module.exports = {
+  description: 'https://github.com/chartjs/Chart.js/issues/8912',
+  config: {
+    type: 'bar',
+    data: {
+      labels: ['Red', 'Blue'],
+      datasets: [{
+        data: [10, -10]
+      }]
+    },
+    options: {
+      plugins: false,
+      scales: {
+        x: {
+          display: false,
+        },
+        y: {
+          grace: '100%'
+        }
+      }
+    }
+  },
+  options: {
+    spriteText: true
+  }
+};
diff --git a/test/fixtures/scale.linear/grace/issue-8912.png b/test/fixtures/scale.linear/grace/issue-8912.png
new file mode 100644 (file)
index 0000000..0a6e768
Binary files /dev/null and b/test/fixtures/scale.linear/grace/issue-8912.png differ