Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linear: determine grace amount from range #9719

Merged
merged 1 commit into from Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/core/core.scale.js
Expand Up @@ -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 ;)
Expand Down Expand Up @@ -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;
}

Expand Down
9 changes: 6 additions & 3 deletions src/helpers/helpers.options.js
Expand Up @@ -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)
};
}
29 changes: 29 additions & 0 deletions 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
}
}
};
Binary file added test/fixtures/scale.linear/grace/grace-10%.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions 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
}
}
};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion 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: {
Expand Down
1 change: 0 additions & 1 deletion 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: {
Expand Down
1 change: 0 additions & 1 deletion 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: {
Expand Down