@@ -39,6 +39,7 @@ export type CustomLegendOption = ECOption["legend"] & {
3939 type : "custom" ;
4040 data ?: {
4141 id ?: string ;
42+ secondaryIds ?: string [ ] ; // Other dataset IDs that should be controlled by this legend item.
4243 name : string ;
4344 itemStyle ?: Record < string , any > ;
4445 } [ ] ;
@@ -181,6 +182,10 @@ export class HaChartBase extends LitElement {
181182 return ;
182183 }
183184 let chartOptions : ECOption = { } ;
185+ if ( changedProps . has ( "options" ) ) {
186+ // Separate 'if' from below since this must updated before _getSeries()
187+ this . _updateHiddenStatsFromOptions ( this . options ) ;
188+ }
184189 if ( changedProps . has ( "data" ) || changedProps . has ( "_hiddenDatasets" ) ) {
185190 chartOptions . series = this . _getSeries ( ) ;
186191 }
@@ -451,14 +456,7 @@ export class HaChartBase extends LitElement {
451456 } ) ;
452457 }
453458
454- const legend = ensureArray ( this . options ?. legend || [ ] ) [ 0 ] as
455- | LegendComponentOption
456- | undefined ;
457- Object . entries ( legend ?. selected || { } ) . forEach ( ( [ stat , selected ] ) => {
458- if ( selected === false ) {
459- this . _hiddenDatasets . add ( stat ) ;
460- }
461- } ) ;
459+ this . _updateHiddenStatsFromOptions ( this . options ) ;
462460
463461 this . chart . setOption ( {
464462 ...this . _createOptions ( ) ,
@@ -469,6 +467,42 @@ export class HaChartBase extends LitElement {
469467 }
470468 }
471469
470+ // Return an array of all IDs associated with the legend item of the primaryId
471+ private _getAllIdsFromLegend (
472+ options : ECOption | undefined ,
473+ primaryId : string
474+ ) : string [ ] {
475+ if ( ! options ) return [ primaryId ] ;
476+ const legend = ensureArray ( this . options ?. legend || [ ] ) [ 0 ] as
477+ | LegendComponentOption
478+ | undefined ;
479+
480+ let customLegendItem ;
481+ if ( legend ?. type === "custom" ) {
482+ customLegendItem = ( legend as CustomLegendOption ) . data ?. find (
483+ ( li ) => typeof li === "object" && li . id === primaryId
484+ ) ;
485+ }
486+
487+ return [ primaryId , ...( customLegendItem ?. secondaryIds || [ ] ) ] ;
488+ }
489+
490+ // Parses the options structure and adds all ids of unselected legend items to hiddenDatasets.
491+ // No known need to remove items at this time.
492+ private _updateHiddenStatsFromOptions ( options : ECOption | undefined ) {
493+ if ( ! options ) return ;
494+ const legend = ensureArray ( this . options ?. legend || [ ] ) [ 0 ] as
495+ | LegendComponentOption
496+ | undefined ;
497+ Object . entries ( legend ?. selected || { } ) . forEach ( ( [ stat , selected ] ) => {
498+ if ( selected === false ) {
499+ this . _getAllIdsFromLegend ( options , stat ) . forEach ( ( id ) =>
500+ this . _hiddenDatasets . add ( id )
501+ ) ;
502+ }
503+ } ) ;
504+ }
505+
472506 private _getDataZoomConfig ( ) : DataZoomComponentOption | undefined {
473507 const xAxis = ( this . options ?. xAxis ?. [ 0 ] ?? this . options ?. xAxis ) as
474508 | XAXisOption
@@ -844,10 +878,14 @@ export class HaChartBase extends LitElement {
844878 }
845879 const id = ev . currentTarget ?. id ;
846880 if ( this . _hiddenDatasets . has ( id ) ) {
847- this . _hiddenDatasets . delete ( id ) ;
881+ this . _getAllIdsFromLegend ( this . options , id ) . forEach ( ( i ) =>
882+ this . _hiddenDatasets . delete ( i )
883+ ) ;
848884 fireEvent ( this , "dataset-unhidden" , { id } ) ;
849885 } else {
850- this . _hiddenDatasets . add ( id ) ;
886+ this . _getAllIdsFromLegend ( this . options , id ) . forEach ( ( i ) =>
887+ this . _hiddenDatasets . add ( i )
888+ ) ;
851889 fireEvent ( this , "dataset-hidden" , { id } ) ;
852890 }
853891 this . requestUpdate ( "_hiddenDatasets" ) ;
0 commit comments