@@ -93,14 +93,15 @@ export function toDate(time: number) {
93
93
return new Date ( time * 1000 )
94
94
}
95
95
96
- export function smoothSeries ( series : PointValue [ ] , windowSize : number ) : PointValue [ ] {
96
+ function smoothSeries ( series : PointValue [ ] , windowSize : number ) : PointValue [ ] {
97
97
let result : PointValue [ ] = [ ]
98
98
windowSize = ~ ~ windowSize
99
+ if ( series . length < windowSize ) {
100
+ windowSize = series . length
101
+ }
99
102
let extraWindow = windowSize / 2
100
103
extraWindow = ~ ~ extraWindow
101
- if ( series . length <= windowSize ) {
102
- return series
103
- }
104
+
104
105
105
106
let count = 0
106
107
let total = 0
@@ -194,15 +195,15 @@ export function smoothAndTrimAllCharts(series: Indicator[], baseSeries: Indicato
194
195
baseSeries ?? [ ] , smoothValue )
195
196
196
197
if ( series != null ) {
197
- series = series . map ( ( s , i ) => {
198
+ series . map ( ( s , i ) => {
198
199
s . series = smoothSeries ( s . series , smoothWindow [ 0 ] [ i ] )
199
200
return s
200
201
} )
201
202
trimSteps ( series , stepRange [ 0 ] , stepRange [ 1 ] , smoothRange )
202
203
}
203
204
204
205
if ( baseSeries != null ) {
205
- baseSeries = baseSeries . map ( ( s , i ) => {
206
+ baseSeries . map ( ( s , i ) => {
206
207
s . series = smoothSeries ( s . series , smoothWindow [ 1 ] [ i ] )
207
208
return s
208
209
} )
@@ -218,7 +219,7 @@ export function trimSteps(series: Indicator[], min: number, max: number, smoothR
218
219
localSmoothRange = 0
219
220
} else {
220
221
let trimStepCount = localSmoothRange / ( s . series [ 1 ] . step - s . series [ 0 ] . step )
221
- if ( trimStepCount < 1 ) {
222
+ if ( trimStepCount < 1 ) { // not going to trim anything - or else will filter out single steps
222
223
localSmoothRange = 0
223
224
}
224
225
}
@@ -236,6 +237,9 @@ export function trimSteps(series: Indicator[], min: number, max: number, smoothR
236
237
}
237
238
localMax = Math . min ( localMax , s . series [ s . series . length - 1 ] . step - localSmoothRange )
238
239
240
+ localMin = Math . floor ( localMin )
241
+ localMax = Math . ceil ( localMax )
242
+
239
243
let minIndex = s . series . length - 1
240
244
let maxIndex = 0
241
245
@@ -249,6 +253,25 @@ export function trimSteps(series: Indicator[], min: number, max: number, smoothR
249
253
250
254
s . lowTrimIndex = minIndex
251
255
s . highTrimIndex = maxIndex
256
+
257
+ if ( s . lowTrimIndex > s . highTrimIndex ) { // if trim covers all just give the middle value
258
+ localMax = max == - 1 ? s . series [ s . series . length - 1 ] . step : max
259
+ localMin = min == - 1 ? s . series [ 0 ] . step : min
260
+
261
+ minIndex = s . series . length - 1
262
+ maxIndex = 0
263
+
264
+ for ( let i = 0 ; i < s . series . length ; i ++ ) {
265
+ let p = s . series [ i ]
266
+ if ( p . step >= localMin && p . step <= localMax ) {
267
+ minIndex = Math . min ( i , minIndex )
268
+ maxIndex = Math . max ( i , maxIndex )
269
+ }
270
+ }
271
+
272
+ s . lowTrimIndex = ( minIndex + maxIndex ) / 2
273
+ s . highTrimIndex = ( minIndex + maxIndex ) / 2
274
+ }
252
275
} )
253
276
}
254
277
@@ -285,7 +308,7 @@ export function getSmoothWindow(currentSeries: Indicator[], baseSeries: Indicato
285
308
return [ stepRange , 0 ]
286
309
}
287
310
288
- let smoothRange = mapRange ( smoothValue , 1 , 100 , 1 , Math . max ( maxRange / 10 , 1 ) )
311
+ let smoothRange = mapRange ( smoothValue , 1 , 100 , 1 , maxRange )
289
312
290
313
let stepRange = [ [ ] , [ ] ]
291
314
for ( let s of currentSeries ) {
0 commit comments