Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: tick spacing when min=0 | niceMin or max=0 #8811

Merged
merged 1 commit into from Apr 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/scales/scale.linearbase.js
Expand Up @@ -98,12 +98,12 @@ function generateTicks(generationOptions, dataRange) {
let j = 0;
if (minDefined) {
ticks.push({value: min});
// If the niceMin is smaller than min, skip it
if (niceMin < min) {
// If the niceMin is smaller or equal to min, skip it
if (niceMin <= min) {
j++;
}
// If the next nice tick is close to min, skip that too
if (almostWhole(Math.round((niceMin + j * spacing) * factor) / factor / min, spacing / 1000)) {
if (almostEquals(Math.round((niceMin + j * spacing) * factor) / factor, min, spacing / 10)) {
j++;
}
}
Expand All @@ -114,7 +114,7 @@ function generateTicks(generationOptions, dataRange) {

if (maxDefined) {
// If the previous tick is close to max, replace it with max, else add max
if (almostWhole(ticks[ticks.length - 1].value / max, spacing / 1000)) {
if (almostEquals(ticks[ticks.length - 1].value, max, spacing / 10)) {
ticks[ticks.length - 1].value = max;
} else {
ticks.push({value: max});
Expand Down
26 changes: 26 additions & 0 deletions test/fixtures/scale.linear/issue-8806.js
@@ -0,0 +1,26 @@
module.exports = {
description: 'https://github.com/chartjs/Chart.js/issues/8806',
config: {
type: 'bar',
data: {
labels: ['0', '1', '2', '3', '4', '5', '6'],
datasets: [{
label: '# of Votes',
data: [32, 46, 28, 21, 20, 13, 27]
}]
},
options: {
scales: {
x: {display: false},
y: {ticks: {maxTicksLimit: 4}, min: 0}
}
}
},
options: {
spriteText: true,
canvas: {
width: 256,
height: 256
}
}
};
Binary file added test/fixtures/scale.linear/issue-8806.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.