View difference between Paste ID: 8hb7EzZH and MNezy8wj
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();
89+
            RenderSelectedValue(e);
90
            e.Handled = true;
91
        }
92
93
        private void MouseMoved(object sender, OxyMouseEventArgs e)
94
        {
95
            if (_mousedown)
96
            {
97-
		RenderSelectedValue();
97+
                RenderSelectedValue(e);
98
                e.Handled = true;
99
            }
100
        }
101
102
        private ObservableCollection<Annotation> _selectedFrequencylines;
103
        private static bool RenderingLines;
104-
        private void RenderSelectedValue()
104+
105
        private void RenderSelectedValue(OxyMouseEventArgs e)
106
        {
107
            if (!RenderingLines)
108
            {
109
                RenderingLines = true;
110-
                if (_selectedFrequencylines == null) _selectedFrequencylines = new ObservableCollection<Annotation>();
110+
111
                Axis xaxis, yaxis;
112-
                _selectedFrequencylines.Add(new LineAnnotation
112+
                FFTplotter.Model.GetAxesFromPoint(e.Position, out xaxis, out yaxis);
113
                if (xaxis == null) return;
114
                double x = xaxis.InverseTransform(e.Position.X);
115-
                    X = 500,
115+
                var a = new LineAnnotation
116
                {
117
                    Type = LineAnnotationType.Vertical,
118-
                });
118+
                    X = x,
119
                    Color = OxyColors.Gray,
120-
                // And plot all the added lines!
120+
121-
                foreach (var a in _selectedFrequencylines)
121+
                };
122
123-
                    // TODO: here silverlight crashes if you quicly click (with lmb) at two different locations in the plot.
123+
                // Add the new line
124-
                    FFTplotter.Model.Annotations.Add(a);
124+
                FFTplotter.Model.Annotations.Add(a);
125-
                }
125+
                FFTplotter.Model.Title = FFTplotter.Model.Annotations.Count + " annotations added";
126
127
                FFTplotter.Model.InvalidatePlot(false);
128
129
                RenderingLines = false;
130
            }
131
            else
132
            {
133
            }
134
        }
135
136
        #region INotifyPropertyChanged Members
137
138
        public event PropertyChangedEventHandler PropertyChanged;
139
140
        /// <summary>
141
        ///   Generic NotifyPropertyChanged function
142
        /// </summary>
143
        /// <param name="propertyname"> Name of the property that has changed </param>
144
        private void NotifyPropertyChanged(string propertyname)
145
        {
146
            if (PropertyChanged != null)
147
            {
148
                PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
149
            }
150
        }
151
        #endregion
152
    }
153
}