]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Enable bounds option to all cartesian axes (#8060)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Mon, 16 Nov 2020 20:01:47 +0000 (22:01 +0200)
committerGitHub <noreply@github.com>
Mon, 16 Nov 2020 20:01:47 +0000 (15:01 -0500)
src/core/core.scale.js
src/scales/scale.category.js
src/scales/scale.linearbase.js
src/scales/scale.logarithmic.js
test/specs/scale.category.tests.js
test/specs/scale.linear.tests.js
test/specs/scale.logarithmic.tests.js

index bad77d4c712a3bba4130330c8588f6124b4c6dad..ddec88a2e372fbcd09e5e3083e30234cc17d40d0 100644 (file)
@@ -17,6 +17,15 @@ defaults.set('scale', {
        reverse: false,
        beginAtZero: false,
 
+       /**
+        * Scale boundary strategy (bypassed by min/max time options)
+        * - `data`: make sure data are fully visible, ticks outside are removed
+        * - `ticks`: make sure ticks are fully visible, data outside are truncated
+        * @see https://github.com/chartjs/Chart.js/pull/4556
+        * @since 3.0.0
+        */
+       bounds: 'ticks',
+
        // grid line settings
        gridLines: {
                display: true,
index d61d4c5ff07ad963e13f879c235400deca74ee94..4824dbe9ca836d426b6c8931f4f7614e1ba10438 100644 (file)
@@ -27,10 +27,20 @@ export default class CategoryScale extends Scale {
 
        determineDataLimits() {
                const me = this;
-               const max = me.getLabels().length - 1;
+               const {minDefined, maxDefined} = me.getUserBounds();
+               let {min, max} = me.getMinMax(true);
+
+               if (me.options.bounds === 'ticks') {
+                       if (!minDefined) {
+                               min = 0;
+                       }
+                       if (!maxDefined) {
+                               max = me.getLabels().length - 1;
+                       }
+               }
 
-               me.min = Math.max(me._userMin || 0, 0);
-               me.max = Math.min(me._userMax || max, max);
+               me.min = min;
+               me.max = max;
        }
 
        buildTicks() {
index 8155240d608df0f2c0150dfbd2450b063e5e3320..1f26e00934e2146c2f90635942d9f95a6f634eaa 100644 (file)
@@ -203,9 +203,11 @@ export default class LinearScaleBase extends Scale {
                };
                const ticks = generateTicks(numericGeneratorOptions, me);
 
-               // At this point, we need to update our max and min given the tick values since we have expanded the
-               // range of the scale
-               _setMinAndMaxByKey(ticks, me, 'value');
+               // At this point, we need to update our max and min given the tick values,
+               // since we probably have expanded the range of the scale
+               if (opts.bounds === 'ticks') {
+                       _setMinAndMaxByKey(ticks, me, 'value');
+               }
 
                if (opts.reverse) {
                        ticks.reverse();
index 40983febde7819e68357283c834c85b0d7e56bc8..955496408159600798986912a538f9b79c0b0519 100644 (file)
@@ -124,9 +124,11 @@ export default class LogarithmicScale extends Scale {
                };
                const ticks = generateTicks(generationOptions, me);
 
-               // At this point, we need to update our max and min given the tick values since we have expanded the
-               // range of the scale
-               _setMinAndMaxByKey(ticks, me, 'value');
+               // At this point, we need to update our max and min given the tick values,
+               // since we probably have expanded the range of the scale
+               if (opts.bounds === 'ticks') {
+                       _setMinAndMaxByKey(ticks, me, 'value');
+               }
 
                if (opts.reverse) {
                        ticks.reverse();
index 97183a822146f3c00ce7a0db26f07d979ed695dc..fec12cac61adb14b4ba46cfda475873cf87218db 100644 (file)
@@ -475,4 +475,33 @@ describe('Category scale tests', function() {
                expect(yScale.getPixelForValue(3)).toBeCloseToPixel(426);
                expect(yScale.getPixelForValue(4)).toBeCloseToPixel(538);
        });
+
+       it('Should bound to ticks/data', function() {
+               var chart = window.acquireChart({
+                       type: 'line',
+                       data: {
+                               labels: ['a', 'b', 'c', 'd'],
+                               datasets: [{
+                                       data: {b: 1, c: 99}
+                               }]
+                       },
+                       options: {
+                               scales: {
+                                       x: {
+                                               type: 'category',
+                                               bounds: 'data'
+                                       }
+                               }
+                       }
+               });
+
+               expect(chart.scales.x.min).toEqual(1);
+               expect(chart.scales.x.max).toEqual(2);
+
+               chart.options.scales.x.bounds = 'ticks';
+               chart.update();
+
+               expect(chart.scales.x.min).toEqual(0);
+               expect(chart.scales.x.max).toEqual(3);
+       });
 });
index 36014eabf2b0a4d4a3cedaa5e9501963e6d78a6c..e7b5d055d87be40b0468e082af24cc13018a347c 100644 (file)
@@ -759,6 +759,28 @@ describe('Linear Scale', function() {
                expect(getLabels(chart.scales.y)).toEqual(['0.3', '0.8', '1.3', '1.8', '2.3', '2.8']);
        });
 
+       it('Should bound to data', function() {
+               var chart = window.acquireChart({
+                       type: 'line',
+                       data: {
+                               labels: ['a', 'b'],
+                               datasets: [{
+                                       data: [1, 99]
+                               }]
+                       },
+                       options: {
+                               scales: {
+                                       y: {
+                                               bounds: 'data'
+                                       }
+                               }
+                       }
+               });
+
+               expect(chart.scales.y.min).toEqual(1);
+               expect(chart.scales.y.max).toEqual(99);
+       });
+
        it('Should build labels using the user supplied callback', function() {
                var chart = window.acquireChart({
                        type: 'bar',
index c6a4b960b83002f7f8b80c08e7e23b9b151c210c..332860fe5dad76774d25255b8c425a9e4718e1ea 100644 (file)
@@ -1142,4 +1142,27 @@ describe('Logarithmic Scale tests', function() {
                expect(chart.scales.y.min).toBe(10);
                expect(chart.scales.y.max).toBe(100);
        });
+
+       it('Should bound to data', function() {
+               var chart = window.acquireChart({
+                       type: 'line',
+                       data: {
+                               labels: ['a', 'b'],
+                               datasets: [{
+                                       data: [1.1, 99]
+                               }]
+                       },
+                       options: {
+                               scales: {
+                                       y: {
+                                               type: 'logarithmic',
+                                               bounds: 'data'
+                                       }
+                               }
+                       }
+               });
+
+               expect(chart.scales.y.min).toEqual(1.1);
+               expect(chart.scales.y.max).toEqual(99);
+       });
 });