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

Show correct decimal places when using count but min is not an integer #9122

Merged
merged 2 commits into from May 19, 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
2 changes: 1 addition & 1 deletion src/core/core.ticks.js
Expand Up @@ -82,7 +82,7 @@ function calculateDelta(tickValue, ticks) {
let delta = ticks.length > 3 ? ticks[2].value - ticks[1].value : ticks[1].value - ticks[0].value;

// If we have a number like 2.5 as the delta, figure out how many decimal places we need
if (Math.abs(delta) > 1 && tickValue !== Math.floor(tickValue)) {
if (Math.abs(delta) >= 1 && tickValue !== Math.floor(tickValue)) {
// not an integer
delta = tickValue - Math.floor(tickValue);
}
Expand Down
6 changes: 5 additions & 1 deletion src/scales/scale.linearbase.js
Expand Up @@ -91,7 +91,11 @@ function generateTicks(generationOptions, dataRange) {

// The spacing will have changed in cases 1, 2, and 3 so the factor cannot be computed
// until this point
factor = Math.pow(10, isNullOrUndef(precision) ? _decimalPlaces(spacing) : precision);
const decimalPlaces = Math.max(
_decimalPlaces(spacing),
_decimalPlaces(niceMin),
);
factor = Math.pow(10, isNullOrUndef(precision) ? decimalPlaces : precision);
niceMin = Math.round(niceMin * factor) / factor;
niceMax = Math.round(niceMax * factor) / factor;

Expand Down
38 changes: 38 additions & 0 deletions test/fixtures/scale.linear/tick-count-min-max-not-int.js
@@ -0,0 +1,38 @@
module.exports = {
description: 'https://github.com/chartjs/Chart.js/issues/9078',
config: {
type: 'bar',
data: {
datasets: [{
data: [
{x: 1, y: 3.5},
{x: 2, y: 4.7},
{x: 3, y: 7.3},
{x: 4, y: 6.7}
]
}]
},
options: {
scales: {
x: {
type: 'linear',
display: false,
},
y: {
min: 3.5,
max: 8.5,
ticks: {
count: 6,
}
}
}
}
},
options: {
spriteText: true,
canvas: {
width: 256,
height: 256
}
}
};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.