Skip to content

Commit 9add6b9

Browse files
committed
refactor smoothing
1 parent 4ceae9e commit 9add6b9

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed

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

+14-23
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ function getSmoothWindow(currentSeries: Indicator[], baseSeries: Indicator[], sm
205205
maxRange = Math.max(maxRange, s.series[s.series.length - 1].step - s.series[0].step)
206206
}
207207
}
208-
if (maxRange == Number.MIN_SAFE_INTEGER) {
208+
if (maxRange == Number.MIN_SAFE_INTEGER) { // all single points. -> can't smooth
209209
let stepRange = [[],[]]
210210
for (let s of currentSeries) {
211211
stepRange[0].push(1)
@@ -216,28 +216,26 @@ function getSmoothWindow(currentSeries: Indicator[], baseSeries: Indicator[], sm
216216
return [stepRange, 0]
217217
}
218218

219-
let smoothRange = mapRange(smoothValue, 1, 100, 1, maxRange)
219+
let smoothRange = mapRange(smoothValue, 1, 100, 1, 2*maxRange)
220220

221221
let stepRange = [[],[]]
222+
222223
for (let s of currentSeries) {
223-
if (smoothValue == 100) { // hardcode to max range in case not range due to step inconsistencies
224-
stepRange[0].push(s.series.length)
225-
} else if (s.series.length >= 2 && !s.is_summary) {
224+
if (s.series.length >= 2 && !s.is_summary) {
226225
let stepGap = s.series[1].step - s.series[0].step
227-
let numSteps = Math.max(1, Math.floor(smoothRange / stepGap))
226+
let numSteps = Math.max(1, Math.ceil(smoothRange / stepGap))
228227
stepRange[0].push(numSteps)
229-
} else {
228+
} else { // can't smooth - just a single point
230229
stepRange[0].push(1)
231230
}
232231
}
232+
233233
for (let s of baseSeries) {
234-
if (smoothValue == 100) {
235-
stepRange[1].push(s.series.length)
236-
} else if (s.series.length >= 2 && !s.is_summary) {
234+
if (s.series.length >= 2 && !s.is_summary) {
237235
let stepGap = s.series[1].step - s.series[0].step
238-
let numSteps = Math.max(1, Math.floor(smoothRange / stepGap))
236+
let numSteps = Math.max(1, Math.ceil(smoothRange / stepGap))
239237
stepRange[1].push(numSteps)
240-
} else {
238+
} else { // can't smooth - just a single point
241239
stepRange[1].push(1)
242240
}
243241
}
@@ -248,9 +246,6 @@ function getSmoothWindow(currentSeries: Indicator[], baseSeries: Indicator[], sm
248246
function smoothSeries(series: PointValue[], windowSize: number): PointValue[] {
249247
let result: PointValue[] = []
250248
windowSize = ~~windowSize
251-
if (series.length < windowSize) {
252-
windowSize = series.length
253-
}
254249
let extraWindow = windowSize / 2
255250
extraWindow = ~~extraWindow
256251

@@ -278,18 +273,14 @@ function smoothSeries(series: PointValue[], windowSize: number): PointValue[] {
278273

279274
function trimSteps(series: Indicator[], min: number, max: number, smoothWindow: number[], trimSmoothEnds: boolean = true) {
280275
series.forEach((s, i) => {
281-
let localSmoothWindow = smoothWindow[i] / 2 // remove half from each end
282-
localSmoothWindow = Math.floor(localSmoothWindow)
283-
if (localSmoothWindow < 0) {
284-
localSmoothWindow = 0
285-
}
276+
let localSmoothWindow = Math.floor(smoothWindow[i] / 2) // remove half from each end
277+
286278
if (s.series.length <= 1) {
287279
localSmoothWindow = 0
288280
} else if (smoothWindow[i] >= s.series.length) {
289281
localSmoothWindow = Math.floor(s.series.length/2)
290282
}
291283

292-
293284
let localMin = min
294285
let localMax = max
295286

@@ -308,8 +299,8 @@ function trimSteps(series: Indicator[], min: number, max: number, smoothWindow:
308299
(s.series.length%2 == 0 && localSmoothWindow != 0 ? 1 : 0)].step) // get the mid value for even length series
309300
}
310301

311-
localMin = Math.floor(localMin-1)
312-
localMax = Math.ceil(localMax+1)
302+
localMin = Math.floor(localMin) - 0.5
303+
localMax = Math.ceil(localMax) + 0.5
313304

314305
let minIndex = s.series.length - 1
315306
let maxIndex = 0

0 commit comments

Comments
 (0)