Skip to content

Commit

Permalink
increase spacing until there is a range
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed Jan 1, 2019
1 parent aae05a0 commit 38dc932
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/scales/scale.linearbase.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function generateTicks(generationOptions, dataRange) {
var max = generationOptions.max;
var precision = generationOptions.precision;
var spacing, factor, niceMin, niceMax, numSpaces;
var i = 0;

// spacing is set to a nice number of the dataRange divided by maxNumSpaces.
// stepSize is used as a minimum unit if it is specified.
Expand All @@ -40,9 +41,12 @@ function generateTicks(generationOptions, dataRange) {
factor = Math.pow(10, precision);
spacing = Math.ceil(spacing * factor) / factor;
}

niceMin = Math.floor(dataRange.min / spacing) * spacing;
niceMax = Math.ceil(dataRange.max / spacing) * spacing;
do {
niceMin = Math.floor(dataRange.min / spacing) * spacing;
niceMax = Math.ceil(dataRange.max / spacing) * spacing;
spacing *= 2;
} while (niceMin === niceMax && ++i < 8);
spacing /= 2;

// If min, max and stepSize is set and they make an evenly spaced scale use it.
if (stepSize) {
Expand Down
25 changes: 25 additions & 0 deletions test/specs/scale.linear.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1170,4 +1170,29 @@ describe('Linear Scale', function() {
expect(data[0]._model.base + minBarLength).toEqual(data[0]._model.x);
expect(data[1]._model.base - minBarLength).toEqual(data[1]._model.x);
});

it('Should ensure that the scale has a max and min that are not equal when data contains values that are very close to each other', function() {
var chart = window.acquireChart({
type: 'scatter',
data: {
datasets: [{
data: [
{x: 1, y: 1.8548483304974972},
{x: 2, y: 1.8548483304974974},
]
}],
},
options: {
scales: {
yAxes: [{
id: 'yScale0',
type: 'linear',
}]
}
}
});

expect(chart.scales.yScale0).not.toEqual(undefined); // must construct
expect(chart.scales.yScale0.max).toBeGreaterThan(chart.scales.yScale0.min);
});
});

0 comments on commit 38dc932

Please sign in to comment.