Skip to content

Commit

Permalink
Linear: Respect bounds option in tick generation
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed May 29, 2021
1 parent 8c63351 commit f478100
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/scales/scale.linearbase.js
Expand Up @@ -2,6 +2,7 @@ import {isNullOrUndef} from '../helpers/helpers.core';
import {almostEquals, almostWhole, niceNum, _decimalPlaces, _setMinAndMaxByKey, sign, toRadians} from '../helpers/helpers.math';
import Scale from '../core/core.scale';
import {formatNumber} from '../helpers/helpers.intl';
import {_filterBetween} from '../helpers';

/**
* Generate a set of linear ticks for an axis
Expand Down Expand Up @@ -29,7 +30,7 @@ function generateTicks(generationOptions, dataRange) {
// for details.

const MIN_SPACING = 1e-14;
const {step, min, max, precision, count, maxTicks, maxDigits, includeBounds} = generationOptions;
const {bounds, step, min, max, precision, count, maxTicks, maxDigits, includeBounds} = generationOptions;
const unit = step || 1;
const maxSpaces = maxTicks - 1;
const {min: rmin, max: rmax} = dataRange;
Expand Down Expand Up @@ -58,8 +59,13 @@ function generateTicks(generationOptions, dataRange) {
spacing = Math.ceil(spacing * factor) / factor;
}

niceMin = Math.floor(rmin / spacing) * spacing;
niceMax = Math.ceil(rmax / spacing) * spacing;
if (bounds === 'ticks') {
niceMin = Math.floor(rmin / spacing) * spacing;
niceMax = Math.ceil(rmax / spacing) * spacing;
} else {
niceMin = rmin;
niceMax = rmax;
}

if (minDefined && maxDefined && step && almostWhole((max - min) / step, spacing / 1000)) {
// Case 1: If min, max and stepSize are set and they make an evenly spaced scale use it.
Expand Down Expand Up @@ -241,6 +247,7 @@ export default class LinearScaleBase extends Scale {

const numericGeneratorOptions = {
maxTicks,
bounds: opts.bounds,
min: opts.min,
max: opts.max,
precision: tickOpts.precision,
Expand Down
29 changes: 29 additions & 0 deletions test/fixtures/controller.line/stacking/bounds-data.js
@@ -0,0 +1,29 @@
module.exports = {
config: {
type: 'line',
data: {
labels: ['a', 'b'],
datasets: [{
borderColor: 'red',
data: [50, 75],
}, {
borderColor: 'blue',
data: [25, 50],
}]
},
options: {
scales: {
x: {
display: false
},
y: {
stacked: true,
bounds: 'data'
}
}
}
},
options: {
spriteText: true
}
};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f478100

Please sign in to comment.