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

Fix bug when calculating if steps fit into scale as a whole number th… #3685

Merged
merged 1 commit into from
Dec 14, 2016
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
4 changes: 4 additions & 0 deletions src/core/core.helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ module.exports = function(Chart) {
helpers.almostEquals = function(x, y, epsilon) {
return Math.abs(x - y) < epsilon;
};
helpers.almostWhole = function(x, epsilon) {
var rounded = Math.round(x);
return (((rounded - epsilon) < x) && ((rounded + epsilon) > x));
};
helpers.max = function(array) {
return array.reduce(function(max, value) {
if (!isNaN(value)) {
Expand Down
4 changes: 2 additions & 2 deletions src/core/core.ticks.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ module.exports = function(Chart) {

// If min, max and stepSize is set and they make an evenly spaced scale use it.
if (generationOptions.min && generationOptions.max && generationOptions.stepSize) {
var minMaxDeltaDivisibleByStepSize = ((generationOptions.max - generationOptions.min) % generationOptions.stepSize) === 0;
if (minMaxDeltaDivisibleByStepSize) {
// If very close to our whole number, use it.
if (helpers.almostWhole((generationOptions.max - generationOptions.min) / generationOptions.stepSize, spacing / 1000)) {
niceMin = generationOptions.min;
niceMax = generationOptions.max;
}
Expand Down
5 changes: 5 additions & 0 deletions test/core.helpers.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ describe('Core helper tests', function() {
expect(helpers.almostEquals(1e30, 1e30 + Number.EPSILON, 2 * Number.EPSILON)).toBe(true);
});

it('should correctly determine if a numbers are essentially whole', function() {
expect(helpers.almostWhole(0.99999, 0.0001)).toBe(true);
expect(helpers.almostWhole(0.9, 0.0001)).toBe(false);
});

it('should generate integer ids', function() {
var uid = helpers.uid();
expect(uid).toEqual(jasmine.any(Number));
Expand Down