1
- import { FunctionComponent , useMemo } from "react" ;
1
+ import { FunctionComponent , useMemo , useState } from "react" ;
2
2
import Plot from "react-plotly.js" ;
3
3
4
4
import { TimeseriesPlotProps as Props } from "./types" ;
@@ -25,6 +25,9 @@ const TimeseriesPlot: FunctionComponent<Partial<Props>> = ({
25
25
height = 300 ,
26
26
zoomInRequired = false ,
27
27
} ) => {
28
+ // State for legend visibility
29
+ const [ showLegend , setShowLegend ] = useState < boolean > ( true ) ;
30
+
28
31
// Memoize the transposed channel data
29
32
const channelData = useMemo ( ( ) => {
30
33
if ( ! data ) return [ ] ;
@@ -111,9 +114,9 @@ const TimeseriesPlot: FunctionComponent<Partial<Props>> = ({
111
114
showticklabels : true ,
112
115
showgrid : true ,
113
116
} ,
114
- showlegend : true ,
117
+ showlegend : showLegend ,
115
118
} ) ,
116
- [ channelSeparation , height , visibleEndTime , visibleStartTime , width ] ,
119
+ [ channelSeparation , height , visibleEndTime , visibleStartTime , width , showLegend ] ,
117
120
) ;
118
121
119
122
const config = useMemo (
@@ -152,7 +155,26 @@ const TimeseriesPlot: FunctionComponent<Partial<Props>> = ({
152
155
return < div > Loading...</ div > ;
153
156
}
154
157
155
- return < Plot data = { plotData } layout = { layout } config = { config } /> ;
158
+ return (
159
+ < div >
160
+ < div style = { { marginBottom : '10px' , display : 'flex' , justifyContent : 'flex-end' } } >
161
+ < button
162
+ onClick = { ( ) => setShowLegend ( ! showLegend ) }
163
+ style = { {
164
+ padding : '5px 10px' ,
165
+ backgroundColor : '#f8f9fa' ,
166
+ border : '1px solid #dee2e6' ,
167
+ borderRadius : '4px' ,
168
+ cursor : 'pointer' ,
169
+ fontSize : '12px'
170
+ } }
171
+ >
172
+ { showLegend ? 'Hide Legend' : 'Show Legend' }
173
+ </ button >
174
+ </ div >
175
+ < Plot data = { plotData } layout = { layout } config = { config } />
176
+ </ div >
177
+ ) ;
156
178
} ;
157
179
158
180
export default TimeseriesPlot ;
0 commit comments