Skip to content

Commit e3cdd73

Browse files
authored
Move niceNum to helpers.math, cleanup IE fallbacks (#8570)
1 parent bc8385e commit e3cdd73

File tree

2 files changed

+17
-47
lines changed

2 files changed

+17
-47
lines changed

src/helpers/helpers.math.js

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import {isFinite as isFiniteNumber} from './helpers.core';
22

3+
/**
4+
* @alias Chart.helpers.math
5+
* @namespace
6+
*/
7+
38
export const PI = Math.PI;
49
export const TAU = 2 * PI;
510
export const PITAU = TAU + PI;
@@ -9,10 +14,19 @@ export const HALF_PI = PI / 2;
914
export const QUARTER_PI = PI / 4;
1015
export const TWO_THIRDS_PI = PI * 2 / 3;
1116

17+
export const log10 = Math.log10;
18+
export const sign = Math.sign;
19+
1220
/**
13-
* @alias Chart.helpers.math
14-
* @namespace
21+
* Implementation of the nice number algorithm used in determining where axis labels will go
22+
* @return {number}
1523
*/
24+
export function niceNum(range) {
25+
const niceRange = Math.pow(10, Math.floor(log10(range)));
26+
const fraction = range / niceRange;
27+
const niceFraction = fraction <= 1 ? 1 : fraction <= 2 ? 2 : fraction <= 5 ? 5 : 10;
28+
return niceFraction * niceRange;
29+
}
1630

1731
/**
1832
* Returns an array of factors sorted from 1 to sqrt(value)
@@ -37,16 +51,6 @@ export function _factorize(value) {
3751
return result;
3852
}
3953

40-
export const log10 = Math.log10 || function(x) {
41-
const exponent = Math.log(x) * Math.LOG10E; // Math.LOG10E = 1 / Math.LN10.
42-
// Check for whole powers of 10,
43-
// which due to floating point rounding error should be corrected.
44-
const powerOf10 = Math.round(exponent);
45-
const isPowerOf10 = x === Math.pow(10, powerOf10);
46-
47-
return isPowerOf10 ? powerOf10 : exponent;
48-
};
49-
5054
export function isNumber(n) {
5155
return !isNaN(parseFloat(n)) && isFinite(n);
5256
}
@@ -75,18 +79,6 @@ export function _setMinAndMaxByKey(array, target, property) {
7579
}
7680
}
7781

78-
export const sign = Math.sign ?
79-
function(x) {
80-
return Math.sign(x);
81-
} :
82-
function(x) {
83-
x = +x; // convert to a number
84-
if (x === 0 || isNaN(x)) {
85-
return x;
86-
}
87-
return x > 0 ? 1 : -1;
88-
};
89-
9082
export function toRadians(degrees) {
9183
return degrees * (PI / 180);
9284
}

src/scales/scale.linearbase.js

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,8 @@
11
import {isNullOrUndef} from '../helpers/helpers.core';
2-
import {almostEquals, almostWhole, log10, _decimalPlaces, _setMinAndMaxByKey, sign} from '../helpers/helpers.math';
2+
import {almostEquals, almostWhole, niceNum, _decimalPlaces, _setMinAndMaxByKey, sign} from '../helpers/helpers.math';
33
import Scale from '../core/core.scale';
44
import {formatNumber} from '../core/core.intl';
55

6-
/**
7-
* Implementation of the nice number algorithm used in determining where axis labels will go
8-
* @return {number}
9-
*/
10-
function niceNum(range) {
11-
const exponent = Math.floor(log10(range));
12-
const fraction = range / Math.pow(10, exponent);
13-
let niceFraction;
14-
15-
if (fraction <= 1.0) {
16-
niceFraction = 1;
17-
} else if (fraction <= 2) {
18-
niceFraction = 2;
19-
} else if (fraction <= 5) {
20-
niceFraction = 5;
21-
} else {
22-
niceFraction = 10;
23-
}
24-
25-
return niceFraction * Math.pow(10, exponent);
26-
}
27-
286
/**
297
* Generate a set of linear ticks
308
* @param generationOptions the options used to generate the ticks

0 commit comments

Comments
 (0)