Skip to content

Commit

Permalink
Fix: don't generate ticks > max if max is specified (#11116)
Browse files Browse the repository at this point in the history
* Fix: don't generate ticks > max if max is specified (#11083)

* Add test "Should not generate any ticks > max if max is specified" (#11083)
  • Loading branch information
CodingMarco authored Feb 10, 2023
1 parent 2481547 commit e417c60
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/scales/scale.linearbase.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ function generateTicks(generationOptions, dataRange) {
}

for (; j < numSpaces; ++j) {
ticks.push({value: Math.round((niceMin + j * spacing) * factor) / factor});
const tickValue = Math.round((niceMin + j * spacing) * factor) / factor;
if (maxDefined && tickValue > max) {
break;
}
ticks.push({value: tickValue});
}

if (maxDefined && includeBounds && niceMax !== max) {
Expand Down
22 changes: 22 additions & 0 deletions test/specs/scale.linear.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,28 @@ describe('Linear Scale', function() {
expect(getLabels(chart.scales.y)).toEqual(['1', '3', '5', '7', '9', '11']);
});

it('Should not generate any ticks > max if max is specified', function() {
var chart = window.acquireChart({
type: 'line',
options: {
scales: {
x: {
type: 'linear',
min: 2.404e-8,
max: 2.4143e-8,
ticks: {
includeBounds: false,
},
},
},
},
});

expect(chart.scales.x.min).toBe(2.404e-8);
expect(chart.scales.x.max).toBe(2.4143e-8);
expect(chart.scales.x.ticks[chart.scales.x.ticks.length - 1].value).toBeLessThanOrEqual(2.4143e-8);
});

it('Should not generate insane amounts of ticks with small stepSize and large range', function() {
var chart = window.acquireChart({
type: 'bar',
Expand Down

0 comments on commit e417c60

Please sign in to comment.