SHOW:
|
|
- or go back to the newest paste.
| 1 | ===== MainPage.xaml ===== | |
| 2 | ||
| 3 | <UserControl x:Class="SilverlightApplication3.MainPage" | |
| 4 | xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
| 5 | xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
| 6 | xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | |
| 7 | xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | |
| 8 | xmlns:oxy="clr-namespace:OxyPlot.Silverlight;assembly=OxyPlot.Silverlight" | |
| 9 | mc:Ignorable="d" | |
| 10 | d:DesignHeight="300" d:DesignWidth="400"> | |
| 11 | ||
| 12 | <Grid x:Name="LayoutRoot" Background="White"> | |
| 13 | <oxy:Plot x:Name="FFTplotter"/> | |
| 14 | </Grid> | |
| 15 | </UserControl> | |
| 16 | ||
| 17 | ===== MainPage.xaml.cs ===== | |
| 18 | ||
| 19 | using System; | |
| 20 | using OxyPlot; | |
| 21 | using System.Collections.ObjectModel; | |
| 22 | using System.Collections.Specialized; | |
| 23 | using System.ComponentModel; | |
| 24 | using System.Windows.Controls; | |
| 25 | using System.Collections.Generic; | |
| 26 | ||
| 27 | namespace SilverlightApplication3 | |
| 28 | {
| |
| 29 | public partial class MainPage : UserControl, INotifyPropertyChanged | |
| 30 | {
| |
| 31 | public MainPage() | |
| 32 | {
| |
| 33 | InitializeComponent(); | |
| 34 | FFTplotter.Model = new PlotModel(); | |
| 35 | SetPlotData(); | |
| 36 | ||
| 37 | SelectedValues = new ObservableCollection<int>(); | |
| 38 | ||
| 39 | FFTplotter.Model.MouseDown += MouseDown; | |
| 40 | FFTplotter.Model.MouseUp += MouseUp; | |
| 41 | FFTplotter.Model.MouseMove += MouseMoved; | |
| 42 | } | |
| 43 | ||
| 44 | private void SetPlotData() | |
| 45 | {
| |
| 46 | // Set the type of lineseries and the kind of visualization | |
| 47 | var ls = new LineSeries(); | |
| 48 | ||
| 49 | var ps = new List<DataPoint>(); | |
| 50 | Random random = new Random(); | |
| 51 | var min = 0; | |
| 52 | var max = 100; | |
| 53 | ||
| 54 | for (int x = 0; x < 1000; x++) | |
| 55 | {
| |
| 56 | var dataPoint = 1; | |
| 57 | ps.Add(new DataPoint(x, random.Next(min, max))); | |
| 58 | } | |
| 59 | ||
| 60 | ls.ItemsSource = ps; | |
| 61 | ||
| 62 | FFTplotter.Model.Series.Clear(); | |
| 63 | FFTplotter.Model.Series.Add(ls); | |
| 64 | FFTplotter.Model.InvalidatePlot(true); | |
| 65 | } | |
| 66 | ||
| 67 | private ObservableCollection<int> _selectedFrequencies; | |
| 68 | public ObservableCollection<int> SelectedValues | |
| 69 | {
| |
| 70 | get { return _selectedFrequencies; }
| |
| 71 | set | |
| 72 | {
| |
| 73 | if (value == null) return; | |
| 74 | _selectedFrequencies = value; | |
| 75 | NotifyPropertyChanged("SelectedFrequencies");
| |
| 76 | } | |
| 77 | } | |
| 78 | ||
| 79 | private bool _mousedown; | |
| 80 | private void MouseDown(object sender, OxyMouseEventArgs e) | |
| 81 | {
| |
| 82 | _mousedown = true; | |
| 83 | e.Handled = true; | |
| 84 | } | |
| 85 | ||
| 86 | private void MouseUp(object sender, OxyMouseEventArgs e) | |
| 87 | {
| |
| 88 | _mousedown = false; | |
| 89 | RenderSelectedValue(); | |
| 90 | e.Handled = true; | |
| 91 | } | |
| 92 | ||
| 93 | private void MouseMoved(object sender, OxyMouseEventArgs e) | |
| 94 | {
| |
| 95 | if (_mousedown) | |
| 96 | {
| |
| 97 | RenderSelectedValue(); | |
| 98 | e.Handled = true; | |
| 99 | } | |
| 100 | } | |
| 101 | ||
| 102 | private ObservableCollection<Annotation> _selectedFrequencylines; | |
| 103 | private static bool RenderingLines; | |
| 104 | private void RenderSelectedValue() | |
| 105 | {
| |
| 106 | if (!RenderingLines) | |
| 107 | {
| |
| 108 | RenderingLines = true; | |
| 109 | ||
| 110 | if (_selectedFrequencylines == null) _selectedFrequencylines = new ObservableCollection<Annotation>(); | |
| 111 | ||
| 112 | _selectedFrequencylines.Add(new LineAnnotation | |
| 113 | {
| |
| 114 | Type = LineAnnotationType.Vertical, | |
| 115 | X = 500, | |
| 116 | Color = OxyColors.Gray, | |
| 117 | LineStyle = LineStyle.Solid, | |
| 118 | }); | |
| 119 | ||
| 120 | // And plot all the added lines! | |
| 121 | foreach (var a in _selectedFrequencylines) | |
| 122 | {
| |
| 123 | // TODO: here silverlight crashes if you quicly click (with lmb) at two different locations in the plot. | |
| 124 | FFTplotter.Model.Annotations.Add(a); | |
| 125 | } | |
| 126 | FFTplotter.Model.InvalidatePlot(false); | |
| 127 | ||
| 128 | RenderingLines = false; | |
| 129 | } | |
| 130 | else | |
| 131 | {
| |
| 132 | } | |
| 133 | } | |
| 134 | ||
| 135 | #region INotifyPropertyChanged Members | |
| 136 | ||
| 137 | public event PropertyChangedEventHandler PropertyChanged; | |
| 138 | ||
| 139 | /// <summary> | |
| 140 | /// Generic NotifyPropertyChanged function | |
| 141 | /// </summary> | |
| 142 | /// <param name="propertyname"> Name of the property that has changed </param> | |
| 143 | private void NotifyPropertyChanged(string propertyname) | |
| 144 | {
| |
| 145 | if (PropertyChanged != null) | |
| 146 | {
| |
| 147 | PropertyChanged(this, new PropertyChangedEventArgs(propertyname)); | |
| 148 | } | |
| 149 | } | |
| 150 | #endregion | |
| 151 | } | |
| 152 | } |