Skip to content

Commit d09149f

Browse files
committed
do smooth step trimming using window size instead of step range
1 parent ccf7f0c commit d09149f

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

app/ui/src/components/charts/utils.ts

+15-17
Original file line numberDiff line numberDiff line change
@@ -201,32 +201,29 @@ export function smoothAndTrimAllCharts(series: Indicator[], baseSeries: Indicato
201201
s.series = smoothSeries(s.series, smoothWindow[0][i])
202202
return s
203203
})
204-
trimSteps(series, stepRange[0], stepRange[1], smoothRange, trimSmoothEnds)
204+
trimSteps(series, stepRange[0], stepRange[1], smoothWindow[0], trimSmoothEnds)
205205
}
206206

207207
if (baseSeries != null) {
208208
baseSeries.map((s, i) => {
209209
s.series = smoothSeries(s.series, smoothWindow[1][i])
210210
return s
211211
})
212-
trimSteps(baseSeries, stepRange[0], stepRange[1], smoothRange, trimSmoothEnds)
212+
trimSteps(baseSeries, stepRange[0], stepRange[1], smoothWindow[1], trimSmoothEnds)
213213
}
214214
}
215215

216-
function trimSteps(series: Indicator[], min: number, max: number, smoothRange: number = 0, trimSmoothEnds: boolean = true) {
217-
series.forEach(s => {
218-
let localSmoothRange = smoothRange / 2 // remove half from each end
216+
function trimSteps(series: Indicator[], min: number, max: number, smoothWindow: number[], trimSmoothEnds: boolean = true) {
217+
series.forEach((s, i) => {
218+
let localSmoothWindow = smoothWindow[i] / 2 // remove half from each end
219+
localSmoothWindow = Math.floor(localSmoothWindow)
220+
if (localSmoothWindow < 0) {
221+
localSmoothWindow = 0
222+
}
219223
if (s.series.length <= 1) {
220-
localSmoothRange = 0
221-
} else {
222-
let trimStepCount = localSmoothRange / (s.series[1].step - s.series[0].step)
223-
if (trimStepCount < 1) { // not going to trim anything - or else will filter out single steps
224-
localSmoothRange = 0
225-
}
226-
227-
if (smoothRange >= s.series[s.series.length - 1].step - s.series[0].step) {
228-
localSmoothRange = (s.series[s.series.length - 1].step - s.series[0].step)/2
229-
}
224+
localSmoothWindow = 0
225+
} else if (smoothWindow[i] >= s.series.length) {
226+
localSmoothWindow = Math.floor(s.series.length/2)
230227
}
231228

232229

@@ -237,14 +234,15 @@ function trimSteps(series: Indicator[], min: number, max: number, smoothRange: n
237234
localMin = s.series[0].step
238235
}
239236
if (trimSmoothEnds) {
240-
localMin = Math.max(localMin, s.series[0].step + localSmoothRange)
237+
localMin = Math.max(localMin, s.series[localSmoothWindow].step)
241238
}
242239

243240
if (localMax == -1) {
244241
localMax = s.series[s.series.length - 1].step
245242
}
246243
if (trimSmoothEnds) {
247-
localMax = Math.min(localMax, s.series[s.series.length - 1].step - localSmoothRange)
244+
localMax = Math.min(localMax, s.series[s.series.length - 1 - localSmoothWindow +
245+
(s.series.length%2 == 0 && localSmoothWindow != 0 ? 1 : 0)].step) // get the mid value for even length series
248246
}
249247

250248
localMin = Math.floor(localMin-1)

0 commit comments

Comments
 (0)