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 helpers.math._factorize #6360

Merged
merged 4 commits into from
Jul 2, 2019
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
34 changes: 34 additions & 0 deletions src/helpers/helpers.math.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

/**
* @alias Chart.helpers.math
* @namespace
*/
var exports = {
/**
* Returns an array of factors sorted from 1 to sqrt(value)
* @private
*/
_factorize: function(value) {
var result = [];
var sqrt = Math.sqrt(value);
var i;

for (i = 1; i < sqrt; i++) {
if (value % i === 0) {
result.push(i);
result.push(value / i);
}
}
if (sqrt === (sqrt | 0)) { // if value is a square number
result.push(sqrt);
}

result.sort(function(a, b) {
return a - b;
}).pop();
return result;
}
};

module.exports = exports;
1 change: 1 addition & 0 deletions src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ module.exports = require('./helpers.core');
module.exports.easing = require('./helpers.easing');
module.exports.canvas = require('./helpers.canvas');
module.exports.options = require('./helpers.options');
module.exports.math = require('./helpers.math');
17 changes: 9 additions & 8 deletions src/scales/scale.time.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var helpers = require('../helpers/index');
var Scale = require('../core/core.scale');

var valueOrDefault = helpers.valueOrDefault;
var factorize = helpers.math._factorize;

// Integer constants are from the ES6 spec.
var MIN_INTEGER = Number.MIN_SAFE_INTEGER || -9007199254740991;
Expand All @@ -15,42 +16,42 @@ var INTERVALS = {
millisecond: {
common: true,
size: 1,
steps: [1, 2, 5, 10, 20, 50, 100, 250, 500]
steps: factorize(1000)
},
second: {
common: true,
size: 1000,
steps: [1, 2, 5, 10, 15, 30]
steps: factorize(60)
},
minute: {
common: true,
size: 60000,
steps: [1, 2, 5, 10, 15, 30]
steps: factorize(60)
},
hour: {
common: true,
size: 3600000,
steps: [1, 2, 3, 6, 12]
steps: factorize(24)
},
day: {
common: true,
size: 86400000,
steps: [1, 2, 5]
steps: factorize(10)
},
week: {
common: false,
size: 604800000,
steps: [1, 2, 3, 4]
steps: factorize(4)
},
month: {
common: true,
size: 2.628e9,
steps: [1, 2, 3]
steps: factorize(12)
},
quarter: {
common: false,
size: 7.884e9,
steps: [1, 2, 3, 4]
steps: factorize(4)
},
year: {
common: true,
Expand Down
16 changes: 16 additions & 0 deletions test/specs/helpers.math.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

describe('Chart.helpers.math', function() {
var factorize = Chart.helpers.math._factorize;

it('should factorize', function() {
expect(factorize(1000)).toEqual([1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 125, 200, 250, 500]);
expect(factorize(60)).toEqual([1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30]);
expect(factorize(30)).toEqual([1, 2, 3, 5, 6, 10, 15]);
expect(factorize(24)).toEqual([1, 2, 3, 4, 6, 8, 12]);
expect(factorize(12)).toEqual([1, 2, 3, 4, 6]);
expect(factorize(4)).toEqual([1, 2]);
expect(factorize(-1)).toEqual([]);
expect(factorize(2.76)).toEqual([]);
});
});