Skip to content

Commit 6adc08e

Browse files
author
David Buezas
committed
Added option to return x and y arrays from a lambda
1 parent 3946246 commit 6adc08e

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

readme.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,18 @@ note: `ys[0]` represents the first "known" value, which is the value furthest to
232232
)
233233
```
234234

235+
#### Custom x coordinates of traces
236+
237+
```yaml
238+
- entity: climate.wintergarten_floor
239+
unit_of_measurement: °C
240+
lambda: |-
241+
(ys, xs, entity) => ({
242+
x: xs.map(x => -x),
243+
y: ys.map(y => y / 2),
244+
})
245+
```
246+
235247
## Default trace styling
236248

237249
```yaml

src/plotly-graph-card.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import Cache from "./Cache";
1212
import getThemedLayout from "./themed-layout";
1313
import isProduction from "./is-production";
1414
import { sleep } from "./utils";
15+
import { Datum } from "plotly.js";
1516

1617
const componentName = isProduction ? "plotly-graph" : "plotly-graph-dev";
1718

@@ -184,7 +185,7 @@ export class PlotlyGraph extends HTMLElement {
184185
);
185186
config.entities = config.entities.map((entity) => ({
186187
...entity,
187-
lambda: entity.lambda ? window.eval(entity.lambda) : (y: number[]) => y,
188+
lambda: entity.lambda ? window.eval(entity.lambda) : undefined,
188189
}));
189190
if (config.title) {
190191
config = {
@@ -267,10 +268,22 @@ export class PlotlyGraph extends HTMLElement {
267268
const unit = this.getUnitOfMeasurement(entity_id);
268269
const yaxis_idx = units.indexOf(unit);
269270
const name = trace.name || attribute.friendly_name || entity_id;
270-
const xs = history.map(({ last_changed }) => new Date(last_changed));
271-
const ys = history.map(({ state }) =>
272-
state === "unavailable" ? undefined : state
271+
const xsIn = history.map(({ last_changed }) => new Date(last_changed));
272+
const ysIn: Datum[] = history.map(({ state }) =>
273+
state === "unavailable" ? null : state
273274
);
275+
276+
let xs: Datum[] = xsIn;
277+
let ys = ysIn;
278+
if (trace.lambda) {
279+
const r = trace.lambda(ysIn, xsIn, history);
280+
if (Array.isArray(r)) {
281+
ys = r;
282+
} else {
283+
if (r.x) xs = r.x;
284+
if (r.y) ys = r.y;
285+
}
286+
}
274287
return merge(
275288
{
276289
entity_id,
@@ -282,7 +295,7 @@ export class PlotlyGraph extends HTMLElement {
282295
shape: "hv",
283296
},
284297
x: xs,
285-
y: trace.lambda(ys, xs, history),
298+
y: ys,
286299
yaxis: "y" + (yaxis_idx == 0 ? "" : yaxis_idx + 1),
287300
},
288301
this.config.default_trace,

src/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
import { Datum } from "plotly.js";
2+
13
export type Config = {
24
type: "custom:plotly-graph-card";
35
hours_to_show?: number;
46
refresh_interval?: number; // in seconds
57
entities: (Partial<Plotly.PlotData> & {
68
entity: string;
79
unit_of_measurement?: string;
8-
lambda: (y: any[], x: Date[], raw_entity: History) => number[];
10+
lambda?: (y: Datum[], x: Date[], raw_entity: History) => Datum[] | {x?:Datum[], y?:Datum[]};
911
})[];
1012
default_trace?: Partial<Plotly.PlotData>,
1113
layout?: Partial<Plotly.Layout>;

0 commit comments

Comments
 (0)