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

Add includeBounds option for cartesian ticks #9020

Merged
merged 5 commits into from May 7, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions docs/axes/cartesian/_common_ticks.md
Expand Up @@ -9,6 +9,7 @@ Namespace: `options.scales[scaleId].ticks`
| `sampleSize` | `number` | `ticks.length` | The number of ticks to examine when deciding how many labels will fit. Setting a smaller value will be faster, but may be less accurate when there is large variability in label length.
| `autoSkip` | `boolean` | `true` | If true, automatically calculates how many labels can be shown and hides labels accordingly. Labels will be rotated up to `maxRotation` before skipping any. Turn `autoSkip` off to show all labels no matter what.
| `autoSkipPadding` | `number` | `3` | Padding between the ticks on the horizontal axis when `autoSkip` is enabled.
| `includeBounds` | `boolean` | `true` | Should the defined `min` and `max` values be presented as ticks even if they are not "nice".
| `labelOffset` | `number` | `0` | Distance in pixels to offset the label from the centre point of the tick (in the x-direction for the x-axis, and the y-direction for the y-axis). *Note: this can cause labels at the edges to be cropped by the edge of the canvas*
| `maxRotation` | `number` | `50` | Maximum rotation for tick labels when rotating to condense labels. Note: Rotation doesn't occur until necessary. *Note: Only applicable to horizontal scales.*
| `minRotation` | `number` | `0` | Minimum rotation for tick labels. *Note: Only applicable to horizontal scales.*
Expand Down
27 changes: 16 additions & 11 deletions src/scales/scale.linearbase.js
Expand Up @@ -29,7 +29,7 @@ function generateTicks(generationOptions, dataRange) {
// for details.

const MIN_SPACING = 1e-14;
const {step, min, max, precision, count, maxTicks, maxDigits, horizontal} = generationOptions;
const {step, min, max, precision, count, maxTicks, maxDigits, horizontal, includeBounds} = generationOptions;
const unit = step || 1;
const maxSpaces = maxTicks - 1;
const {min: rmin, max: rmax} = dataRange;
Expand Down Expand Up @@ -97,13 +97,17 @@ function generateTicks(generationOptions, dataRange) {

let j = 0;
if (minDefined) {
ticks.push({value: 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 (almostEquals(Math.round((niceMin + j * spacing) * factor) / factor, min, minSpacing * (horizontal ? ('' + min).length : 1))) {
if (includeBounds) {
ticks.push({value: 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 (almostEquals(Math.round((niceMin + j * spacing) * factor) / factor, min, minSpacing * (horizontal ? ('' + min).length : 1))) {
j++;
}
} else if (niceMin < min) {
j++;
}
}
Expand All @@ -112,14 +116,14 @@ function generateTicks(generationOptions, dataRange) {
ticks.push({value: Math.round((niceMin + j * spacing) * factor) / factor});
}

if (maxDefined) {
if (maxDefined && includeBounds) {
// If the previous tick is close to max, replace it with max, else add max
if (almostEquals(ticks[ticks.length - 1].value, max, minSpacing * (horizontal ? ('' + max).length : 1))) {
ticks[ticks.length - 1].value = max;
} else {
ticks.push({value: max});
}
} else {
} else if (!maxDefined || niceMax === max) {
ticks.push({value: niceMax});
}

Expand Down Expand Up @@ -232,7 +236,8 @@ export default class LinearScaleBase extends Scale {
step: tickOpts.stepSize,
count: tickOpts.count,
maxDigits: me._maxDigits(),
horizontal: me.isHorizontal()
horizontal: me.isHorizontal(),
includeBounds: tickOpts.includeBounds !== false
};
const dataRange = me._range || me;
const ticks = generateTicks(numericGeneratorOptions, dataRange);
Expand Down
26 changes: 26 additions & 0 deletions test/fixtures/scale.linear/includeBounds.js
@@ -0,0 +1,26 @@
module.exports = {
config: {
type: 'scatter',
options: {
scales: {
y: {
max: 1225.2,
min: 369.5,
ticks: {
includeBounds: false
}
},
x: {
min: 20,
max: 100,
ticks: {
includeBounds: false
}
}
}
}
},
options: {
spriteText: true
}
};
Binary file added test/fixtures/scale.linear/includeBounds.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions types/index.esm.d.ts
Expand Up @@ -2831,6 +2831,12 @@ export interface CartesianScaleOptions extends CoreScaleOptions {
*/
crossAlign: 'near' | 'center' | 'far';

/**
* Should the defined `min` and `max` values be presented as ticks even if they are not "nice".
* @default: true
*/
includeBounds: boolean;

/**
* Distance in pixels to offset the label from the centre point of the tick (in the x direction for the x axis, and the y direction for the y axis). Note: this can cause labels at the edges to be cropped by the edge of the canvas
* @default 0
Expand Down