]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fix CategoryScale.getValueForPixel with autoSkip (#8101)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Wed, 2 Dec 2020 04:51:33 +0000 (06:51 +0200)
committerGitHub <noreply@github.com>
Wed, 2 Dec 2020 04:51:33 +0000 (06:51 +0200)
src/scales/scale.category.js
test/specs/scale.category.tests.js

index 4824dbe9ca836d426b6c8931f4f7614e1ba10438..6c06719e8479c31b98f3ab488fd2b8fe99938591 100644 (file)
@@ -111,8 +111,7 @@ export default class CategoryScale extends Scale {
 
        getValueForPixel(pixel) {
                const me = this;
-               const value = Math.round(me._startValue + me.getDecimalForPixel(pixel) * me._valueRange);
-               return Math.min(Math.max(value, 0), me.ticks.length - 1);
+               return Math.round(me._startValue + me.getDecimalForPixel(pixel) * me._valueRange);
        }
 
        getBasePixel() {
index fec12cac61adb14b4ba46cfda475873cf87218db..884627af6ab7c3c7676d140093fed34b2abfc13e 100644 (file)
@@ -476,6 +476,35 @@ describe('Category scale tests', function() {
                expect(yScale.getPixelForValue(4)).toBeCloseToPixel(538);
        });
 
+       it('Should be consistent on pixels and values with autoSkipped ticks', function() {
+               var labels = [];
+               for (let i = 0; i < 50; i++) {
+                       labels.push('very long label ' + i);
+               }
+               var chart = window.acquireChart({
+                       type: 'bar',
+                       data: {
+                               labels,
+                               datasets: [{
+                                       data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
+                               }]
+                       }
+               });
+
+               var scale = chart.scales.x;
+               expect(scale.ticks.length).toBeLessThan(50);
+
+               let x = 0;
+               for (let i = 0; i < 50; i++) {
+                       var x2 = scale.getPixelForValue(labels[i]);
+                       var x3 = scale.getPixelForValue(i);
+                       expect(x2).toEqual(x3);
+                       expect(x2).toBeGreaterThan(x);
+                       expect(scale.getValueForPixel(x2)).toBe(i);
+                       x = x2;
+               }
+       });
+
        it('Should bound to ticks/data', function() {
                var chart = window.acquireChart({
                        type: 'line',