]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Linear scale with min/max/stepSize set should use all 3 if the range fits (#6966)
authorEvert Timberg <evert.timberg+github@gmail.com>
Sat, 18 Jan 2020 13:35:30 +0000 (08:35 -0500)
committerGitHub <noreply@github.com>
Sat, 18 Jan 2020 13:35:30 +0000 (08:35 -0500)
When (max - min) / stepSize is an integer, we can do a very simple
tick generation method.

src/scales/scale.linearbase.js
test/specs/scale.linear.tests.js
test/specs/scale.radialLinear.tests.js

index cf412ecc7bac963a5a2c6840ab01c8fa3c82ea22..4c5d73b546560270b1e65bdf3ad8e5dd1578e4de 100644 (file)
@@ -13,22 +13,18 @@ const isNullOrUndef = helpers.isNullOrUndef;
  * @returns {number[]} array of tick values
  */
 function generateTicks(generationOptions, dataRange) {
-       var ticks = [];
+       const ticks = [];
        // To get a "nice" value for the tick spacing, we will use the appropriately named
        // "nice number" algorithm. See https://stackoverflow.com/questions/8506881/nice-label-algorithm-for-charts-with-minimum-ticks
        // for details.
 
-       var MIN_SPACING = 1e-14;
-       var stepSize = generationOptions.stepSize;
-       var unit = stepSize || 1;
-       var maxNumSpaces = generationOptions.maxTicks - 1;
-       var min = generationOptions.min;
-       var max = generationOptions.max;
-       var precision = generationOptions.precision;
-       var rmin = dataRange.min;
-       var rmax = dataRange.max;
-       var spacing = helpers.niceNum((rmax - rmin) / maxNumSpaces / unit) * unit;
-       var factor, niceMin, niceMax, numSpaces;
+       const MIN_SPACING = 1e-14;
+       const {stepSize, min, max, precision} = generationOptions;
+       const unit = stepSize || 1;
+       const maxNumSpaces = generationOptions.maxTicks - 1;
+       const {min: rmin, max: rmax} = dataRange;
+       let spacing = helpers.niceNum((rmax - rmin) / maxNumSpaces / unit) * unit;
+       let factor, niceMin, niceMax, numSpaces;
 
        // Beyond MIN_SPACING floating point numbers being to lose precision
        // such that we can't do the math necessary to generate ticks
@@ -55,12 +51,10 @@ function generateTicks(generationOptions, dataRange) {
        niceMax = Math.ceil(rmax / spacing) * spacing;
 
        // If min, max and stepSize is set and they make an evenly spaced scale use it.
-       if (stepSize) {
+       if (stepSize && !isNullOrUndef(min) && !isNullOrUndef(max)) {
                // If very close to our whole number, use it.
-               if (!isNullOrUndef(min) && almostWhole(min / spacing, spacing / 1000)) {
+               if (almostWhole((max - min) / stepSize, spacing / 1000)) {
                        niceMin = min;
-               }
-               if (!isNullOrUndef(max) && almostWhole(max / spacing, spacing / 1000)) {
                        niceMax = max;
                }
        }
@@ -163,11 +157,10 @@ class LinearScaleBase extends Scale {
        }
 
        getTickLimit() {
-               var me = this;
-               var tickOpts = me.options.ticks;
-               var stepSize = tickOpts.stepSize;
-               var maxTicksLimit = tickOpts.maxTicksLimit;
-               var maxTicks;
+               const me = this;
+               const tickOpts = me.options.ticks;
+               let {maxTicksLimit, stepSize} = tickOpts;
+               let maxTicks;
 
                if (stepSize) {
                        maxTicks = Math.ceil(me.max / stepSize) - Math.floor(me.min / stepSize) + 1;
index 37630e2145f88a32fdd05b6f386701a197a87be4..ef3f7e8d87b83b24ef25bbb53a6df3b580ad9ab6 100644 (file)
@@ -572,7 +572,7 @@ describe('Linear Scale', function() {
                expect(chart.scales.y).not.toEqual(undefined); // must construct
                expect(chart.scales.y.min).toBe(1);
                expect(chart.scales.y.max).toBe(11);
-               expect(getLabels(chart.scales.y)).toEqual(['11', '10', '8', '6', '4', '2', '1']);
+               expect(getLabels(chart.scales.y)).toEqual(['11', '9', '7', '5', '3', '1']);
        });
 
        it('Should create decimal steps if stepSize is a decimal number', function() {
@@ -788,7 +788,7 @@ describe('Linear Scale', function() {
                chart.options.scales.y.max = 2.8;
                chart.update();
 
-               expect(getLabels(chart.scales.y)).toEqual(['2.8', '2.5', '2.0', '1.5', '1.0', '0.5', '0.3']);
+               expect(getLabels(chart.scales.y)).toEqual(['2.8', '2.3', '1.8', '1.3', '0.8', '0.3']);
        });
 
        it('Should build labels using the user supplied callback', function() {
index 6c99a7079125c94f4aa95663c81530f5f553c5c0..a2580661579e5a56282705c444afae518a9c768e 100644 (file)
@@ -306,7 +306,7 @@ describe('Test the radial linear scale', function() {
                chart.options.scales.r.max = 2.8;
                chart.update();
 
-               expect(getLabels(chart.scales.r)).toEqual(['0.3', '0.5', '1.0', '1.5', '2.0', '2.5', '2.8']);
+               expect(getLabels(chart.scales.r)).toEqual(['0.3', '0.8', '1.3', '1.8', '2.3', '2.8']);
        });
 
        it('Should build labels using the user supplied callback', function() {