Skip to content
Open
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
5 changes: 4 additions & 1 deletion src/scales/scale.linearbase.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ function generateTicks(generationOptions, dataRange) {
niceMax = rmax;
}

if (minDefined && maxDefined && step && almostWhole((max - min) / step, spacing / 1000)) {
const stepCount = step ? (max - min) / step : 0;
const stepCountEpsilon = Math.max(Math.abs(stepCount) * Number.EPSILON, 1e-14);

if (minDefined && maxDefined && step && almostWhole(stepCount, stepCountEpsilon)) {
// Case 1: If min, max and stepSize are set and they make an evenly spaced scale use it.
// spacing = step;
// numSpaces = (max - min) / spacing;
Expand Down
42 changes: 42 additions & 0 deletions test/specs/scale.linear.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,48 @@ describe('Linear Scale', function() {
expect(getLabels(chart.scales.y)).toEqual(['1', '3', '5', '7', '9', '11']);
});

it('Should use stepSize increments and keep max as the final shorter interval', function() {
var chart = window.acquireChart({
type: 'bar',
options: {
scales: {
y: {
type: 'linear',
min: 0,
max: 3333,
ticks: {
stepSize: 500
}
}
}
}
});

expect(chart.scales.y.ticks.map(tick => tick.value)).toEqual([
0, 500, 1000, 1500, 2000, 2500, 3000, 3333
]);
});

it('Should use stepSize for floating point ranges that are effectively divisible', function() {
var chart = window.acquireChart({
type: 'bar',
options: {
scales: {
y: {
type: 'linear',
min: 0,
max: 0.3,
ticks: {
stepSize: 0.1
}
}
}
}
});

expect(chart.scales.y.ticks.map(tick => +tick.value.toFixed(1))).toEqual([0, 0.1, 0.2, 0.3]);
});

it('Should not generate any ticks > max if max is specified', function() {
var chart = window.acquireChart({
type: 'line',
Expand Down