@@ -12,6 +12,7 @@ import Cache from "./Cache";
1212import getThemedLayout from "./themed-layout" ;
1313import isProduction from "./is-production" ;
1414import { sleep } from "./utils" ;
15+ import { Datum } from "plotly.js" ;
1516
1617const 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 ,
0 commit comments