From: Jukka Kurkela Date: Mon, 4 Oct 2021 21:43:04 +0000 (+0300) Subject: Linear: determine grace amount from range (#9719) X-Git-Tag: v3.6.0~18 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=10292c691299247b66626c2861f84a5a74570873;p=thirdparty%2FChart.js.git Linear: determine grace amount from range (#9719) --- diff --git a/src/core/core.scale.js b/src/core/core.scale.js index 68b3c1b1c..4549cadb9 100644 --- a/src/core/core.scale.js +++ b/src/core/core.scale.js @@ -387,7 +387,7 @@ export default class Scale extends Element { * - thickness of scales or legends in another orientation */ update(maxWidth, maxHeight, margins) { - const tickOpts = this.options.ticks; + const {beginAtZero, grace, ticks: tickOpts} = this.options; const sampleSize = tickOpts.sampleSize; // Update Lifecycle - Probably don't want to ever extend or overwrite this function ;) @@ -422,7 +422,7 @@ export default class Scale extends Element { this.beforeDataLimits(); this.determineDataLimits(); this.afterDataLimits(); - this._range = _addGrace(this, this.options.grace); + this._range = _addGrace(this, grace, beginAtZero); this._dataLimitsCached = true; } diff --git a/src/helpers/helpers.options.js b/src/helpers/helpers.options.js index 7453a54bc..d42b3503b 100644 --- a/src/helpers/helpers.options.js +++ b/src/helpers/helpers.options.js @@ -172,12 +172,15 @@ export function resolve(inputs, context, index, info) { /** * @param {{min: number, max: number}} minmax * @param {number|string} grace + * @param {boolean} beginAtZero * @private */ -export function _addGrace(minmax, grace) { +export function _addGrace(minmax, grace, beginAtZero) { const {min, max} = minmax; + const change = toDimension(grace, (max - min) / 2); + const keepZero = (value, add) => beginAtZero && value === 0 ? 0 : value + add; return { - min: min - Math.abs(toDimension(grace, min)), - max: max + toDimension(grace, max) + min: keepZero(min, -Math.abs(change)), + max: keepZero(max, change) }; } diff --git a/test/fixtures/scale.linear/grace/grace-10%.js b/test/fixtures/scale.linear/grace/grace-10%.js new file mode 100644 index 000000000..3001fecca --- /dev/null +++ b/test/fixtures/scale.linear/grace/grace-10%.js @@ -0,0 +1,29 @@ +module.exports = { + config: { + type: 'bar', + data: { + labels: ['a', 'b'], + datasets: [{ + data: [90, -10], + }], + }, + options: { + indexAxis: 'y', + scales: { + y: { + display: false + }, + x: { + grace: '10%' + } + } + } + }, + options: { + spriteText: true, + canvas: { + width: 512, + height: 128 + } + } +}; diff --git a/test/fixtures/scale.linear/grace/grace-10%.png b/test/fixtures/scale.linear/grace/grace-10%.png new file mode 100644 index 000000000..ae3db6866 Binary files /dev/null and b/test/fixtures/scale.linear/grace/grace-10%.png differ diff --git a/test/fixtures/scale.linear/grace/grace-beginAtZero.js b/test/fixtures/scale.linear/grace/grace-beginAtZero.js new file mode 100644 index 000000000..77965d6c3 --- /dev/null +++ b/test/fixtures/scale.linear/grace/grace-beginAtZero.js @@ -0,0 +1,42 @@ +module.exports = { + config: { + type: 'bar', + data: { + labels: ['a', 'b'], + datasets: [{ + data: [100, 0], + backgroundColor: 'blue' + }, { + xAxisID: 'x2', + data: [0, 100], + backgroundColor: 'red' + }], + }, + options: { + indexAxis: 'y', + scales: { + y: { + display: false + }, + x: { + position: 'top', + beginAtZero: true, + grace: '10%', + }, + x2: { + position: 'bottom', + type: 'linear', + beginAtZero: false, + grace: '10%', + } + } + } + }, + options: { + spriteText: true, + canvas: { + width: 512, + height: 128 + } + } +}; diff --git a/test/fixtures/scale.linear/grace/grace-beginAtZero.png b/test/fixtures/scale.linear/grace/grace-beginAtZero.png new file mode 100644 index 000000000..4f1883fd3 Binary files /dev/null and b/test/fixtures/scale.linear/grace/grace-beginAtZero.png differ diff --git a/test/fixtures/scale.linear/grace/grace-neg.js b/test/fixtures/scale.linear/grace/grace-neg.js index f606202ae..90bdf0eb5 100644 --- a/test/fixtures/scale.linear/grace/grace-neg.js +++ b/test/fixtures/scale.linear/grace/grace-neg.js @@ -1,5 +1,4 @@ module.exports = { - description: 'https://github.com/chartjs/Chart.js/issues/7734', config: { type: 'bar', data: { diff --git a/test/fixtures/scale.linear/grace/grace-pos.js b/test/fixtures/scale.linear/grace/grace-pos.js index 76d6c693d..72df79a86 100644 --- a/test/fixtures/scale.linear/grace/grace-pos.js +++ b/test/fixtures/scale.linear/grace/grace-pos.js @@ -1,5 +1,4 @@ module.exports = { - description: 'https://github.com/chartjs/Chart.js/issues/7734', config: { type: 'bar', data: { diff --git a/test/fixtures/scale.linear/grace/grace.js b/test/fixtures/scale.linear/grace/grace.js index 8f3f09f96..a2950e5b1 100644 --- a/test/fixtures/scale.linear/grace/grace.js +++ b/test/fixtures/scale.linear/grace/grace.js @@ -1,5 +1,4 @@ module.exports = { - description: 'https://github.com/chartjs/Chart.js/issues/7734', config: { type: 'bar', data: {