@@ -6,12 +6,13 @@ const getAxisMinMax = (value) => {
66
77 const range = max - min
88
9- const targetTicks = 6
9+ const targetTicks = 6 // assume 6 ticks on y axis (5 divisions)
1010 const roughStep = range / ( targetTicks - 1 )
1111
12- const step = niceNumber ( roughStep )
13- // number of decimal places necessary to represent step cleanly
14- const decimals = Math . max ( 0 , - Math . floor ( Math . log10 ( step ) ) )
12+ // round the step size to a nice number and determine how many decimal places
13+ // are needed to show it
14+ const [ step , decimals ] = niceNumber ( roughStep )
15+
1516 // round min down and max up using multiples of step
1617 const axisMin = Number ( ( Math . floor ( min / step ) * step ) . toFixed ( decimals ) )
1718 const axisMax = Number ( ( Math . ceil ( max / step ) * step ) . toFixed ( decimals ) )
@@ -23,15 +24,24 @@ const getAxisMinMax = (value) => {
2324}
2425
2526// 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
2628const 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
2731 const exp = Math . floor ( Math . log10 ( x ) )
28- const f = x / Math . pow ( 10 , exp ) // fraction in [1, 10)
32+ const f = x / Math . pow ( 10 , exp )
33+
34+ // round f to 1, 2 5 or 10
2935 let niceFraction
3036 if ( f < 1.5 ) niceFraction = 1
3137 else if ( f < 3 ) niceFraction = 2
3238 else if ( f < 7 ) niceFraction = 5
3339 else niceFraction = 10
34- return niceFraction * Math . pow ( 10 , exp )
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 ]
3545}
3646
3747const getAxisMin = ( value ) => {
0 commit comments