Fix CategoryScale.getValueForPixel with autoSkip (#8101)

This commit is contained in:
Jukka Kurkela
2020-12-02 06:51:33 +02:00
committed by GitHub
parent c3fbe5efc3
commit 6814b79b65
2 changed files with 30 additions and 2 deletions

View 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() {

View 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',