@@ -13,6 +13,7 @@ namespace ExampleLibrary
1313 using OxyPlot . Annotations ;
1414 using OxyPlot . Axes ;
1515 using OxyPlot . Series ;
16+ using OxyPlot . Legends ;
1617
1718 [ Examples ( "PlotController examples" ) ]
1819 public static class PlotControllerExamples
@@ -141,5 +142,67 @@ void HandleZoomByWheel(IPlotView view, OxyMouseWheelEventArgs args, double facto
141142 m . Started ( args ) ;
142143 }
143144 }
145+
146+ [ Example ( "Show/Hide Legend" ) ]
147+ public static Example ShowHideLegend ( )
148+ {
149+ var plotModel = new PlotModel { Title = "Show/Hide Legend" , Subtitle = "Click on the rectangles" } ;
150+
151+ int n = 3 ;
152+ for ( int i = 1 ; i <= n ; i ++ )
153+ {
154+ var s = new LineSeries { Title = "Series " + i } ;
155+ plotModel . Series . Add ( s ) ;
156+ for ( double x = 0 ; x < 2 * Math . PI ; x += 0.1 )
157+ {
158+ s . Points . Add ( new DataPoint ( x , ( Math . Sin ( x * i ) / i ) + i ) ) ;
159+ }
160+ }
161+ var l = new Legend ( ) ;
162+
163+ plotModel . Legends . Add ( l ) ;
164+
165+ var annotation1 = new RectangleAnnotation { Fill = OxyColors . Green , Text = "Show Legend" , MinimumX = 0.5 , MaximumX = 1.5 , MinimumY = .2 , MaximumY = 0.4 } ;
166+ plotModel . Annotations . Add ( annotation1 ) ;
167+
168+ var annotation2 = new RectangleAnnotation { Fill = OxyColors . SkyBlue , Text = "Hide Legend" , MinimumX = 0.5 , MaximumX = 1.5 , MinimumY = 0.6 , MaximumY = 0.8 } ;
169+ plotModel . Annotations . Add ( annotation2 ) ;
170+
171+ EventHandler < OxyMouseDownEventArgs > handleMouseClick = ( s , e ) =>
172+ {
173+ string annotationText = ( ( RectangleAnnotation ) s ) . Text ;
174+ if ( annotationText == "Show Legend" )
175+ {
176+ plotModel . IsLegendVisible = true ;
177+ }
178+ else if ( annotationText == "Hide Legend" )
179+ {
180+ plotModel . IsLegendVisible = false ;
181+ }
182+
183+ plotModel . Subtitle = "You clicked " + ( ( RectangleAnnotation ) s ) . Text ;
184+ plotModel . InvalidatePlot ( false ) ;
185+ } ;
186+
187+ annotation1 . MouseDown += handleMouseClick ;
188+ annotation2 . MouseDown += handleMouseClick ;
189+
190+ var controller = new PlotController ( ) ;
191+ var handleClick = new DelegatePlotCommand < OxyMouseDownEventArgs > (
192+ ( v , c , e ) =>
193+ {
194+ var args = new HitTestArguments ( e . Position , 10 ) ;
195+ var firstHit = v . ActualModel . HitTest ( args ) . FirstOrDefault ( x => x . Element is RectangleAnnotation ) ;
196+ if ( firstHit != null )
197+ {
198+ e . Handled = true ;
199+ plotModel . Subtitle = "You clicked " + ( ( RectangleAnnotation ) firstHit . Element ) . Text ;
200+ plotModel . InvalidatePlot ( false ) ;
201+ }
202+ } ) ;
203+ controller . Bind ( new OxyMouseDownGesture ( OxyMouseButton . Left ) , handleClick ) ;
204+
205+ return new Example ( plotModel , controller ) ;
206+ }
144207 }
145208}
0 commit comments