Skip to content

Commit a32be6e

Browse files
Add example to Show/Hide Legend (#1470) (#1977)
Co-authored-by: VisualMelon <VisualMelon@users.noreply.github.com>
1 parent c025d31 commit a32be6e

3 files changed

Lines changed: 65 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
44
## Unreleased
55

66
### Added
7+
- Example to Show/Hide Legend (#1470)
78
- Example of BarSeries stacked and with labels (#1979)
89
- Example of issue with AreaSeries tracker (#1982)
910

CONTRIBUTORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Federico Coppola <fede@silentman.it>
5555
Francois Botha <igitur@gmail.com>
5656
Frank Tore Sæther <frank.sather@gmail.com>
5757
Garrett
58+
GaryDev <112358055+garydev10@users.noreply.github.com>
5859
Geert van Horrik <geert@catenalogic.com>
5960
Gimly
6061
Herman Eldering <herman@eldering.net>

Source/Examples/ExampleLibrary/Examples/PlotControllerExamples.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)