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

Linear: Skip ticks that would overlap with min/max #8569

Merged
merged 1 commit into from
Mar 5, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions src/scales/scale.linearbase.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ function generateTicks(generationOptions, dataRange) {
const unit = stepSize || 1;
const maxNumSpaces = generationOptions.maxTicks - 1;
const {min: rmin, max: rmax} = dataRange;
const minDefined = !isNullOrUndef(min);
const maxDefined = !isNullOrUndef(max);
let spacing = niceNum((rmax - rmin) / maxNumSpaces / unit) * unit;
let factor, niceMin, niceMax, numSpaces;

// Beyond MIN_SPACING floating point numbers being to lose precision
// such that we can't do the math necessary to generate ticks
if (spacing < MIN_SPACING && isNullOrUndef(min) && isNullOrUndef(max)) {
if (spacing < MIN_SPACING && !minDefined && !maxDefined) {
return [{value: rmin}, {value: rmax}];
}

Expand All @@ -70,7 +72,7 @@ function generateTicks(generationOptions, dataRange) {
niceMax = Math.ceil(rmax / spacing) * spacing;

// If min, max and stepSize is set and they make an evenly spaced scale use it.
if (stepSize && !isNullOrUndef(min) && !isNullOrUndef(max)) {
if (stepSize && minDefined && maxDefined) {
// If very close to our whole number, use it.
if (almostWhole((max - min) / stepSize, spacing / 1000)) {
niceMin = min;
Expand All @@ -88,11 +90,34 @@ function generateTicks(generationOptions, dataRange) {

niceMin = Math.round(niceMin * factor) / factor;
niceMax = Math.round(niceMax * factor) / factor;
ticks.push({value: isNullOrUndef(min) ? niceMin : min});
for (let j = 1; j < numSpaces; ++j) {

let j = 0;
if (minDefined) {
ticks.push({value: min});
// If the niceMin is smaller than 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)) {
j++;
}
}

for (; j < numSpaces; ++j) {
ticks.push({value: Math.round((niceMin + j * spacing) * factor) / factor});
}
ticks.push({value: isNullOrUndef(max) ? niceMax : max});

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)) {
ticks[ticks.length - 1].value = max;
} else {
ticks.push({value: max});
}
} else {
ticks.push({value: niceMax});
}

return ticks;
}
Expand Down
20 changes: 20 additions & 0 deletions test/fixtures/scale.linear/min-max-skip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports = {
description: 'https://github.com/chartjs/Chart.js/issues/7734',
config: {
type: 'line',
options: {
scales: {
y: {
max: 1225.2,
min: 369.5,
},
x: {
display: false
}
}
}
},
options: {
spriteText: true
}
};
Binary file added test/fixtures/scale.linear/min-max-skip.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/specs/scale.radialLinear.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ describe('Test the radial linear scale', function() {

expect(chart.scales.r.min).toBe(-1010);
expect(chart.scales.r.max).toBe(1010);
expect(getLabels(chart.scales.r)).toEqual(['-1,010', '-1,000', '-500', '0', '500', '1,000', '1,010']);
expect(getLabels(chart.scales.r)).toEqual(['-1,010', '-500', '0', '500', '1,010']);
});

it('should forcibly include 0 in the range if the beginAtZero option is used', function() {
Expand Down