@@ -13,6 +13,21 @@ const allowedMultipliers = [
1313 1 , 1.2 , 1.4 , 1.5 , 1.6 , 1.8 , 2 , 2.5 , 3 , 3.5 , 4 , 5 , 6 , 7 , 7.5 , 8 , 9 , 10 ,
1414] ;
1515
16+ export function roundDownNice ( x : number ) {
17+ if ( x <= 0 ) {
18+ return x ;
19+ }
20+ const exponent = Math . floor ( Math . log10 ( x ) ) ;
21+ const base = Math . pow ( 10 , exponent ) ;
22+ const fraction = x / base ;
23+ for ( const m of allowedMultipliers . slice ( ) . reverse ( ) ) {
24+ if ( m <= fraction ) {
25+ return Math . floor ( m * base ) ;
26+ }
27+ }
28+ return Math . floor ( 10 * base ) ;
29+ }
30+
1631export function roundUpNice ( x : number ) {
1732 if ( x <= 0 ) {
1833 return x ;
@@ -22,10 +37,10 @@ export function roundUpNice(x: number) {
2237 const fraction = x / base ;
2338 for ( const m of allowedMultipliers ) {
2439 if ( m >= fraction ) {
25- return Math . round ( m * base ) ;
40+ return Math . ceil ( m * base ) ;
2641 }
2742 }
28- return Math . round ( 10 * base ) ;
43+ return Math . ceil ( 10 * base ) ;
2944}
3045
3146export function roundNearestNice ( x : number ) {
@@ -51,11 +66,14 @@ export function spacedLogValues(min: number, max: number, steps: number) {
5166 if ( steps < 2 ) {
5267 return [ ] ;
5368 }
69+ if ( steps > max - min ) {
70+ steps = max - min + 1 ;
71+ }
5472
5573 if ( min === 0 ) {
5674 const nonzeroCount = steps - 1 ;
57- const exponent = Math . floor ( Math . log10 ( max ) ) - ( nonzeroCount - 1 ) ;
58- const lowerNonZero = roundNearestNice ( Math . pow ( 10 , exponent ) ) ;
75+ const exponent = Math . log10 ( max ) / ( nonzeroCount - 1 ) ;
76+ const lowerNonZero = roundDownNice ( Math . pow ( 10 , exponent ) ) ;
5977 const upperTick = roundUpNice ( max ) ;
6078 const r = Math . pow ( upperTick / lowerNonZero , 1 / ( nonzeroCount - 1 ) ) ;
6179 const ticks = [ 0 ] ;
@@ -65,7 +83,7 @@ export function spacedLogValues(min: number, max: number, steps: number) {
6583 }
6684 return ticks ;
6785 } else {
68- const lowerTick = roundUpNice ( min ) ;
86+ const lowerTick = roundNearestNice ( min ) ;
6987 const upperTick = roundUpNice ( max ) ;
7088 const r = Math . pow ( upperTick / lowerTick , 1 / ( steps - 1 ) ) ;
7189 const ticks = [ ] ;
0 commit comments