From 6814b79b65afeeab09d3f03151ec0458be10686f Mon Sep 17 00:00:00 2001 From: Jukka Kurkela Date: Wed, 2 Dec 2020 06:51:33 +0200 Subject: [PATCH] Fix CategoryScale.getValueForPixel with autoSkip (#8101) --- src/scales/scale.category.js | 3 +-- test/specs/scale.category.tests.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/scales/scale.category.js b/src/scales/scale.category.js index 4824dbe9c..6c06719e8 100644 --- a/src/scales/scale.category.js +++ b/src/scales/scale.category.js @@ -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() { diff --git a/test/specs/scale.category.tests.js b/test/specs/scale.category.tests.js index fec12cac6..884627af6 100644 --- a/test/specs/scale.category.tests.js +++ b/test/specs/scale.category.tests.js @@ -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', -- 2.47.2