@@ -349,8 +349,9 @@ export class PlotlyGraph extends HTMLElement {
349349 const entities = this . config . entities ;
350350
351351 const units = this . getAllUnitsOfMeasurement ( ) ;
352-
353- return entities . flatMap ( ( trace , traceIdx ) => {
352+ const show_value_traces : Plotly . Data [ ] = [ ] ;
353+ const real_traces : Plotly . Data [ ] = [ ] ;
354+ entities . forEach ( ( trace , traceIdx ) => {
354355 const entity_id = trace . entity ;
355356 const history = this . cache . getHistory ( trace ) ;
356357 const attributes = this . hass ?. states [ entity_id ] ?. attributes || { } ;
@@ -391,10 +392,10 @@ export class PlotlyGraph extends HTMLElement {
391392 } ,
392393 trace
393394 ) ;
394- const traces : Plotly . Data [ ] = [ mergedTrace ] ;
395+ real_traces . push ( mergedTrace ) ;
395396 if ( mergedTrace . show_value ) {
396397 mergedTrace . legendgroup ??= "group" + traceIdx ;
397- traces . push ( {
398+ show_value_traces . push ( {
398399 // @ts -expect-error (texttemplate missing in plotly typings)
399400 texttemplate : `%{y}%{customdata.unit_of_measurement}` , // here so it can be overwritten
400401 ...mergedTrace ,
@@ -418,7 +419,7 @@ export class PlotlyGraph extends HTMLElement {
418419 const timeMargin =
419420 mergedTrace . show_value . right_margin *
420421 ( ( this . config . hours_to_show * 1000 * 60 * 60 ) / 100 ) ;
421- traces . push ( {
422+ show_value_traces . push ( {
422423 ...mergedTrace ,
423424 marker : {
424425 color : "transparent" ,
@@ -430,8 +431,10 @@ export class PlotlyGraph extends HTMLElement {
430431 } ) ;
431432 }
432433 }
433- return traces ;
434434 } ) ;
435+ // Preserving the original sequence of real_traces is important for `fill: tonexty`
436+ // https://github.com/dbuezas/lovelace-plotly-graph-card/issues/87
437+ return [ ...real_traces , ...show_value_traces ] ;
435438 }
436439
437440 getLayout ( ) : Plotly . Layout {
0 commit comments