Skip to content

Commit 8b7a78c

Browse files
authored
added reset_every and offset options to the integrate filter (#210)
1 parent 1b2de35 commit 8b7a78c

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

readme.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,12 @@ entities:
458458
- delta # computes the delta between each two consecutive numeric y values.
459459
- derivate: h # computes rate of change per unit of time: h # ms (milisecond), s (second), m (minute), h (hour), d (day), w (week), M (month), y (year)
460460
- integrate: h # computes area under the curve per unit of time using Right hand riemann integration. Same units as the derivative
461-
- map_y_numbers: Math.sqrt(y + 10*100) # map the y coordinate of each datapoint. Same available variables as for `map_y`
461+
- integrate:
462+
unit: h # defaults to h
463+
reset_every: 1h # Defaults to 0 (never reset). Any duration unit (ms, s, m, h, d, w, M, y).
464+
offset: 30m # defaults to 0. Resets happen 30m later
462465

466+
- map_y_numbers: Math.sqrt(y + 10*100) # map the y coordinate of each datapoint. Same available variables as for `map_y`
463467
# In the filters below, missing and non numeric datapoints will be discarded
464468
- sliding_window_moving_average: # best for smoothing
465469
# default parameters:

src/filters/filters.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,32 @@ const filters = {
140140
}),
141141
};
142142
},
143-
integrate:
144-
(unit: keyof typeof timeUnits = "h") =>
145-
({ xs, ys, meta }) => {
146-
checkTimeUnits(unit);
147-
checkTimeUnits(unit);
143+
integrate: (
144+
unitOrObject:
145+
| keyof typeof timeUnits
146+
| {
147+
unit?: keyof typeof timeUnits;
148+
reset_every?: TimeDurationStr;
149+
offset?: TimeDurationStr;
150+
} = "h"
151+
) => {
152+
const param =
153+
typeof unitOrObject == "string" ? { unit: unitOrObject } : unitOrObject;
154+
console.log(`param`, param);
155+
const unit = param.unit ?? "h";
156+
const reset_every = parseTimeDuration(param.reset_every ?? "0s");
157+
const offset = parseTimeDuration(param.offset ?? "0s");
158+
checkTimeUnits(unit);
159+
const date = new Date();
160+
date.setHours(0);
161+
date.setMinutes(0);
162+
date.setSeconds(0);
163+
const t0 = +date + offset;
164+
return ({ xs, ys, meta }) => {
148165
let yAcc = 0;
149166
let last = {
150167
x: NaN,
168+
laps: 0,
151169
};
152170
return {
153171
meta: {
@@ -157,6 +175,13 @@ const filters = {
157175
xs: xs,
158176
ys: mapNumbers(ys, (y, i) => {
159177
const x = +xs[i];
178+
if (reset_every > 0) {
179+
const laps = Math.floor((x - t0) / reset_every);
180+
if (laps !== last.laps) {
181+
yAcc = 0;
182+
last.laps = laps;
183+
}
184+
}
160185
const dateDelta = (x - last.x) / timeUnits[unit];
161186
const isFirst = isNaN(last.x);
162187
last.x = x;
@@ -165,7 +190,8 @@ const filters = {
165190
return yAcc;
166191
}),
167192
};
168-
},
193+
};
194+
},
169195
sliding_window_moving_average:
170196
({ window_size = 10, extended = false, centered = true } = {}) =>
171197
(params) => {

src/plotly-graph-card.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,7 @@ export class PlotlyGraph extends HTMLElement {
329329
this.config = config;
330330
const is = this.config;
331331
this.touchController.isEnabled = !is.disable_pinch_to_zoom;
332-
if (is.hours_to_show !== was?.hours_to_show || is.offset !== was?.offset) {
333-
this.exitBrowsingMode();
334-
} else {
335-
await this.plot({ should_fetch: false });
336-
}
332+
this.exitBrowsingMode();
337333
}
338334
getCSSVars() {
339335
const styles = window.getComputedStyle(this.contentEl);

0 commit comments

Comments
 (0)