Skip to content

Commit

Permalink
Show correct decimal places when using count but min is not an inte…
Browse files Browse the repository at this point in the history
…ger (#9122)

* Show correct decimal places when using count but `min` is not an integer
* Fix lint issues
  • Loading branch information
etimberg committed May 19, 2021
1 parent 6f7d5fb commit 7800939
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
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.

0 comments on commit 7800939

Please sign in to comment.