From: Evert Timberg Date: Wed, 19 May 2021 22:31:15 +0000 (-0400) Subject: Show correct decimal places when using count but `min` is not an integer (#9122) X-Git-Tag: v3.3.0~5 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=78009390ad9a473cbc1847999de8097898fd7be4;p=thirdparty%2FChart.js.git Show correct decimal places when using count but `min` is not an integer (#9122) * Show correct decimal places when using count but `min` is not an integer * Fix lint issues --- diff --git a/src/core/core.ticks.js b/src/core/core.ticks.js index 186be7c0f..f054ef66a 100644 --- a/src/core/core.ticks.js +++ b/src/core/core.ticks.js @@ -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); } diff --git a/src/scales/scale.linearbase.js b/src/scales/scale.linearbase.js index f8248668e..cb299cf43 100644 --- a/src/scales/scale.linearbase.js +++ b/src/scales/scale.linearbase.js @@ -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; diff --git a/test/fixtures/scale.linear/tick-count-min-max-not-int.js b/test/fixtures/scale.linear/tick-count-min-max-not-int.js new file mode 100644 index 000000000..a9f6e02f0 --- /dev/null +++ b/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 + } + } +}; diff --git a/test/fixtures/scale.linear/tick-count-min-max-not-int.png b/test/fixtures/scale.linear/tick-count-min-max-not-int.png new file mode 100644 index 000000000..0b94172fd Binary files /dev/null and b/test/fixtures/scale.linear/tick-count-min-max-not-int.png differ