@@ -13,6 +13,21 @@ const allowedMultipliers = [
13
13
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 ,
14
14
] ;
15
15
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
+
16
31
export function roundUpNice ( x : number ) {
17
32
if ( x <= 0 ) {
18
33
return x ;
@@ -22,10 +37,10 @@ export function roundUpNice(x: number) {
22
37
const fraction = x / base ;
23
38
for ( const m of allowedMultipliers ) {
24
39
if ( m >= fraction ) {
25
- return Math . round ( m * base ) ;
40
+ return Math . ceil ( m * base ) ;
26
41
}
27
42
}
28
- return Math . round ( 10 * base ) ;
43
+ return Math . ceil ( 10 * base ) ;
29
44
}
30
45
31
46
export function roundNearestNice ( x : number ) {
@@ -51,11 +66,14 @@ export function spacedLogValues(min: number, max: number, steps: number) {
51
66
if ( steps < 2 ) {
52
67
return [ ] ;
53
68
}
69
+ if ( steps > max - min ) {
70
+ steps = max - min + 1 ;
71
+ }
54
72
55
73
if ( min === 0 ) {
56
74
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 ) ) ;
59
77
const upperTick = roundUpNice ( max ) ;
60
78
const r = Math . pow ( upperTick / lowerNonZero , 1 / ( nonzeroCount - 1 ) ) ;
61
79
const ticks = [ 0 ] ;
@@ -65,7 +83,7 @@ export function spacedLogValues(min: number, max: number, steps: number) {
65
83
}
66
84
return ticks ;
67
85
} else {
68
- const lowerTick = roundUpNice ( min ) ;
86
+ const lowerTick = roundNearestNice ( min ) ;
69
87
const upperTick = roundUpNice ( max ) ;
70
88
const r = Math . pow ( upperTick / lowerTick , 1 / ( steps - 1 ) ) ;
71
89
const ticks = [ ] ;
0 commit comments