11import React , { useEffect , useMemo , useRef } from 'react' ;
22import { useIntl } from 'react-intl' ;
3- import { SettingsSection } from '../framework/settings/SettingsPrimitives' ;
43import { ScrollArea } from '@/shared/ui/scroll-area' ;
54import type { PlotConfig } from './defaults' ;
65import {
76 isPlotLegendVisible ,
87 plotLegendSelectionState ,
9- setAllPlotLegendVisible ,
8+ setPlotLegendGroupVisible ,
109 setPlotLegendVisible ,
1110} from './plotLegendVisibility' ;
1211import { usePlotLegendEntries } from './plotPanelRuntimeStore' ;
1312
1413interface PlotLegendSettingsProps {
1514 panelId : string ;
15+ seriesId : string ;
1616 config : PlotConfig ;
1717 setConfig : ( next : PlotConfig | ( ( prev : PlotConfig ) => PlotConfig ) ) => void ;
1818}
1919
20- function legendInputId ( panelId : string , index : number ) : string {
21- return `plot-legend-${ panelId } -${ index } ` ;
20+ function legendKeyPrefix ( seriesId : string ) : string {
21+ return `${ seriesId } :` ;
22+ }
23+
24+ function legendInputId ( panelId : string , seriesId : string , index : number ) : string {
25+ return `plot-legend-${ panelId } -${ seriesId } -${ index } ` ;
2226}
2327
2428export function PlotLegendSettings ( {
2529 panelId,
30+ seriesId,
2631 config,
2732 setConfig,
2833} : PlotLegendSettingsProps ) : React . ReactNode {
2934 const { formatMessage } = useIntl ( ) ;
30- const entries = usePlotLegendEntries ( panelId ) ;
35+ const allEntries = usePlotLegendEntries ( panelId ) ;
36+ const prefix = legendKeyPrefix ( seriesId ) ;
37+ const entries = useMemo (
38+ ( ) => allEntries . filter ( ( entry ) => entry . key . startsWith ( prefix ) ) ,
39+ [ allEntries , prefix ] ,
40+ ) ;
3141 const hiddenKeys = config . hiddenLegendKeys ;
3242 const selectAllRef = useRef < HTMLInputElement > ( null ) ;
33- const selectAllId = `plot-legend-${ panelId } -all` ;
43+ const selectAllId = `plot-legend-${ panelId } -${ seriesId } - all` ;
3444
3545 const selection = useMemo (
3646 ( ) => plotLegendSelectionState ( entries , hiddenKeys ) ,
@@ -48,6 +58,10 @@ export function PlotLegendSettings({
4858 input . indeterminate = selection === 'partial' ;
4959 } , [ selection ] ) ;
5060
61+ if ( entries . length <= 1 ) {
62+ return null ;
63+ }
64+
5165 const setHiddenKeys = ( next : string [ ] ) => {
5266 setConfig ( ( prev ) => ( { ...prev , hiddenLegendKeys : next } ) ) ;
5367 } ;
@@ -57,21 +71,25 @@ export function PlotLegendSettings({
5771 } ;
5872
5973 const toggleAll = ( visible : boolean ) => {
60- setHiddenKeys ( setAllPlotLegendVisible ( entries . map ( ( entry ) => entry . key ) , visible ) ) ;
74+ setHiddenKeys (
75+ setPlotLegendGroupVisible (
76+ hiddenKeys ,
77+ entries . map ( ( entry ) => entry . key ) ,
78+ visible ,
79+ ) ,
80+ ) ;
6181 } ;
6282
6383 return (
64- < SettingsSection
65- title = { formatMessage ( { id : 'panels.plot.settings.section.legend' } ) }
66- description = { formatMessage ( { id : 'panels.plot.settings.legend.description' } ) }
67- >
68- { entries . length === 0 ? (
69- < p className = "px-1 text-[11px] text-muted-foreground" >
70- { formatMessage ( { id : 'panels.plot.settings.legend.empty' } ) }
71- </ p >
72- ) : (
73- < >
74- < div className = "mb-1.5 flex items-center gap-2 rounded border border-border bg-muted/30 px-2 py-1" >
84+ < div className = "space-y-1 border-t border-border pt-2" >
85+ < p className = "text-[10px] font-medium text-muted-foreground" >
86+ { formatMessage ( { id : 'panels.plot.settings.series.legend.title' } ) }
87+ </ p >
88+ < p className = "px-0.5 text-[10px] text-muted-foreground" >
89+ { formatMessage ( { id : 'panels.plot.settings.legend.description' } ) }
90+ </ p >
91+ < >
92+ < div className = "flex items-center gap-2 rounded border border-border bg-muted/30 px-2 py-1" >
7593 < input
7694 ref = { selectAllRef }
7795 id = { selectAllId }
@@ -88,10 +106,10 @@ export function PlotLegendSettings({
88106 ) }
89107 </ label >
90108 </ div >
91- < ScrollArea className = "max-h-72 rounded border border-border" >
109+ < ScrollArea className = "max-h-48 rounded border border-border" >
92110 < div className = "flex flex-col py-0.5" >
93111 { entries . map ( ( entry , index ) => {
94- const inputId = legendInputId ( panelId , index ) ;
112+ const inputId = legendInputId ( panelId , seriesId , index ) ;
95113 const visible = isPlotLegendVisible ( hiddenKeys , entry . key ) ;
96114 return (
97115 < label
@@ -111,16 +129,18 @@ export function PlotLegendSettings({
111129 style = { { backgroundColor : entry . color } }
112130 aria-hidden
113131 />
114- < span className = "min-w-0 flex-1 truncate text-[11px] leading-tight text-foreground" title = { entry . label } >
132+ < span
133+ className = "min-w-0 flex-1 truncate text-[11px] leading-tight text-foreground"
134+ title = { entry . label }
135+ >
115136 { entry . label }
116137 </ span >
117138 </ label >
118139 ) ;
119140 } ) }
120141 </ div >
121142 </ ScrollArea >
122- </ >
123- ) }
124- </ SettingsSection >
143+ </ >
144+ </ div >
125145 ) ;
126146}
0 commit comments