1+ // Round x to a nice number, using factors of 1, 2, 5 or 10
2+ // and determine how many decimal places are needed to show it
3+ const niceNumber = ( x ) => {
4+ // find x as a number between 1 and 9.9999 with power of 10 multiplier
5+ // eg, 68.7 is 6.87 * 10^1 so exp is 1 and f is 6.8
6+ const exp = Math . floor ( Math . log10 ( x ) )
7+ const f = x / Math . pow ( 10 , exp )
8+
9+ // round f to 1, 2 5 or 10
10+ let niceFraction
11+ if ( f < 1.5 ) niceFraction = 1
12+ else if ( f < 3 ) niceFraction = 2
13+ else if ( f < 7 ) niceFraction = 5
14+ else niceFraction = 10
15+ // and scale it up to the range of the input value so if x were 68.7 this returns 50
16+ const niceX = niceFraction * Math . pow ( 10 , exp )
17+ // determine the number of decimal places necessary to represent this
18+ const decimals = Math . max ( 0 , - exp )
19+ return [ niceX , decimals ]
20+ }
21+
122const getAxisMinMax = ( value ) => {
223 const min = typeof value ?. min === 'number' ? value ?. min : 0
324 let max = typeof value ?. max === 'number' ? value ?. max : 1
@@ -23,27 +44,6 @@ const getAxisMinMax = (value) => {
2344 }
2445}
2546
26- // Round x to a nice number, using factors of 1, 2, 5 or 10
27- // and determine how may decimal places are needed to show it
28- const niceNumber = ( x ) => {
29- // find x as a number between 1 and 9.9999 with power of 10 multiplier
30- // eg, 68.7 is 6.87 * 10^1 so exp is 1 and f is 6.8
31- const exp = Math . floor ( Math . log10 ( x ) )
32- const f = x / Math . pow ( 10 , exp )
33-
34- // round f to 1, 2 5 or 10
35- let niceFraction
36- if ( f < 1.5 ) niceFraction = 1
37- else if ( f < 3 ) niceFraction = 2
38- else if ( f < 7 ) niceFraction = 5
39- else niceFraction = 10
40- // and scale it up to the range of the input value so if x were 68.7 this returns 50
41- const niceX = niceFraction * Math . pow ( 10 , exp )
42- // determine the number of decimal places necessary to represent this
43- const decimals = Math . max ( 0 , - exp )
44- return [ niceX , decimals ]
45- }
46-
4747const getAxisMin = ( value ) => {
4848 return getAxisMinMax ( value ) . min
4949}
0 commit comments