11import { isFinite as isFiniteNumber } from './helpers.core' ;
22
3+ /**
4+ * @alias Chart.helpers.math
5+ * @namespace
6+ */
7+
38export const PI = Math . PI ;
49export const TAU = 2 * PI ;
510export const PITAU = TAU + PI ;
@@ -9,10 +14,19 @@ export const HALF_PI = PI / 2;
914export const QUARTER_PI = PI / 4 ;
1015export 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-
5054export 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-
9082export function toRadians ( degrees ) {
9183 return degrees * ( PI / 180 ) ;
9284}
0 commit comments