Skip to content

Commit b30d05f

Browse files
authored
Merge pull request #222 from labmlai/smooth
Smoothing to a single point
2 parents 72906d5 + 4985313 commit b30d05f

File tree

4 files changed

+41
-17
lines changed

4 files changed

+41
-17
lines changed

app/ui/src/components/charts/lines/chart.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ import {Indicator} from "../../../models/run"
55
import {
66
getExtent,
77
getLogScale,
8-
getScale,
9-
getSmoothWindow,
10-
smoothSeries,
11-
trimSteps
8+
getScale
129
} from "../utils"
1310
import {LineFill, LinePlot} from "./plot"
1411
import {BottomAxis, RightAxis} from "../axis"

app/ui/src/components/charts/lines/plot.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import d3 from "../../../d3"
22
import {WeyaElement, WeyaElementFunction} from '../../../../../lib/weya/weya'
33
import {FillOptions, PlotOptions} from '../types'
44
import {PointValue} from "../../../models/run"
5-
import {getSelectedIdx, mapRange, smoothSeries} from "../utils"
5+
import {getSelectedIdx} from "../utils"
66

77
export interface LinePlotOptions extends PlotOptions {
88
xScale: d3.ScaleLinear<number, number>
@@ -88,6 +88,10 @@ export class LinePlot {
8888
})
8989
})
9090
})
91+
92+
if (this.series.length == 1) { // allways render circle for single point
93+
this.renderCircle(0)
94+
}
9195
}
9296

9397
renderIndicators(cursorStep: number | null) {

app/ui/src/components/charts/spark_lines/chart.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {WeyaElementFunction} from '../../../../../lib/weya/weya'
22
import {ChartOptions} from '../types'
3-
import {getExtent, getSmoothWindow, smoothSeries} from "../utils"
3+
import {getExtent} from "../utils"
44
import {SparkLine} from "./spark_line"
55
import ChartColors from "../chart_colors"
66
import {DefaultLineGradient} from "../chart_gradients"
@@ -56,7 +56,7 @@ export class SparkLines {
5656
const margin = Math.floor(opt.width / 64)
5757
this.rowWidth = Math.min(450, opt.width - Math.max(3 * margin, 60))
5858

59-
this.stepExtent = getExtent(this.currentSeries.concat(this.baseSeries).map(s => s.series), d => d.step)
59+
this.stepExtent = getExtent(this.currentSeries.concat(this.baseSeries).map(s => s.trimmedSeries), d => d.step)
6060

6161
this.chartColors = new ChartColors({nColors: this.uniqueItems.size, secondNColors: this.uniqueItems.size, isDivergent: opt.isDivergent})
6262
}
@@ -82,7 +82,7 @@ export class SparkLines {
8282
}
8383
let sparkLine = new SparkLine({
8484
name: s.name,
85-
series: s.series,
85+
series: s.trimmedSeries,
8686
selected: this.currentPlotIdx[i],
8787
stepExtent: this.stepExtent,
8888
width: this.rowWidth,
@@ -105,7 +105,7 @@ export class SparkLines {
105105
}
106106
let sparkLine = new SparkLine({
107107
name: s.name,
108-
series: s.series,
108+
series: s.trimmedSeries,
109109
selected: this.basePlotIdx[i],
110110
stepExtent: this.stepExtent,
111111
width: this.rowWidth,

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

+31-8
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,15 @@ export function toDate(time: number) {
9393
return new Date(time * 1000)
9494
}
9595

96-
export function smoothSeries(series: PointValue[], windowSize: number): PointValue[] {
96+
function smoothSeries(series: PointValue[], windowSize: number): PointValue[] {
9797
let result: PointValue[] = []
9898
windowSize = ~~windowSize
99+
if (series.length < windowSize) {
100+
windowSize = series.length
101+
}
99102
let extraWindow = windowSize / 2
100103
extraWindow = ~~extraWindow
101-
if (series.length <= windowSize) {
102-
return series
103-
}
104+
104105

105106
let count = 0
106107
let total = 0
@@ -194,15 +195,15 @@ export function smoothAndTrimAllCharts(series: Indicator[], baseSeries: Indicato
194195
baseSeries ?? [], smoothValue)
195196

196197
if (series != null) {
197-
series = series.map((s, i) => {
198+
series.map((s, i) => {
198199
s.series = smoothSeries(s.series, smoothWindow[0][i])
199200
return s
200201
})
201202
trimSteps(series, stepRange[0], stepRange[1], smoothRange)
202203
}
203204

204205
if (baseSeries != null) {
205-
baseSeries = baseSeries.map((s, i) => {
206+
baseSeries.map((s, i) => {
206207
s.series = smoothSeries(s.series, smoothWindow[1][i])
207208
return s
208209
})
@@ -218,7 +219,7 @@ export function trimSteps(series: Indicator[], min: number, max: number, smoothR
218219
localSmoothRange = 0
219220
} else {
220221
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
222223
localSmoothRange = 0
223224
}
224225
}
@@ -236,6 +237,9 @@ export function trimSteps(series: Indicator[], min: number, max: number, smoothR
236237
}
237238
localMax = Math.min(localMax, s.series[s.series.length - 1].step - localSmoothRange)
238239

240+
localMin = Math.floor(localMin)
241+
localMax = Math.ceil(localMax)
242+
239243
let minIndex = s.series.length - 1
240244
let maxIndex = 0
241245

@@ -249,6 +253,25 @@ export function trimSteps(series: Indicator[], min: number, max: number, smoothR
249253

250254
s.lowTrimIndex = minIndex
251255
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+
}
252275
})
253276
}
254277

@@ -285,7 +308,7 @@ export function getSmoothWindow(currentSeries: Indicator[], baseSeries: Indicato
285308
return [stepRange, 0]
286309
}
287310

288-
let smoothRange = mapRange(smoothValue, 1, 100, 1, Math.max(maxRange/10, 1))
311+
let smoothRange = mapRange(smoothValue, 1, 100, 1, maxRange)
289312

290313
let stepRange = [[],[]]
291314
for (let s of currentSeries) {

0 commit comments

Comments
 (0)