PageRenderTime 46ms CodeModel.GetById 40ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/Main/src/Xbap/DynamicDataDisplay.Xbap.Samples/Demos/v02/AnimationSample.xaml.cs

#
C# | 93 lines | 68 code | 12 blank | 13 comment | 3 complexity | 6740e1224c84e4ba8d4c3956ded56e6e MD5 | raw file
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Text;
 5using System.Windows;
 6using System.Windows.Controls;
 7using System.Windows.Data;
 8using System.Windows.Documents;
 9using System.Windows.Input;
10using System.Windows.Media;
11using System.Windows.Media.Imaging;
12using System.Windows.Navigation;
13using System.Windows.Shapes;
14using Microsoft.Research.DynamicDataDisplay.DataSources;
15using System.Windows.Threading;
16using System.Globalization;
17
18namespace Microsoft.Research.DynamicDataDisplay.Xbap.Samples.Demos.v02
19{
20	/// <summary>
21	/// Interaction logic for AnimationSample.xaml
22	/// </summary>
23	public partial class AnimationSample : Page
24	{
25		double phase = 0;
26		readonly double[] animatedX = new double[1000];
27		readonly double[] animatedY = new double[1000];
28		EnumerableDataSource<double> animatedDataSource = null;
29
30		/// <summary>Programmatically created header</summary>
31		Header chartHeader = new Header();
32
33		/// <summary>Text contents of header</summary>
34		TextBlock headerContents = new TextBlock();
35
36		/// <summary>Timer to animate data</summary>
37		readonly DispatcherTimer timer = new DispatcherTimer();  
38
39		public AnimationSample()
40		{
41			InitializeComponent();
42			headerContents.FontSize = 24;
43			headerContents.Text = "Phase = 0.00";
44			headerContents.HorizontalAlignment = HorizontalAlignment.Center;
45			chartHeader.Content = headerContents;
46			plotter.Children.Add(chartHeader);
47		}
48
49		private void AnimatedPlot_Timer(object sender, EventArgs e)
50		{
51			phase += 0.01;
52			if (phase > 2 * Math.PI)
53				phase -= 2 * Math.PI;
54			for (int i = 0; i < animatedX.Length; i++)
55				animatedY[i] = Math.Sin(animatedX[i] + phase);
56
57			// Here it is - signal that data is updated
58			animatedDataSource.RaiseDataChanged();
59			headerContents.Text = String.Format(CultureInfo.InvariantCulture, "Phase = {0:N2}", phase);
60		}
61
62		private void Window_Loaded(object sender, RoutedEventArgs e)
63		{
64			for (int i = 0; i < animatedX.Length; i++)
65			{
66				animatedX[i] = 2 * Math.PI * i / animatedX.Length;
67				animatedY[i] = Math.Sin(animatedX[i]);
68			}
69			EnumerableDataSource<double> xSrc = new EnumerableDataSource<double>(animatedX);
70			xSrc.SetXMapping(x => x);
71			animatedDataSource = new EnumerableDataSource<double>(animatedY);
72			animatedDataSource.SetYMapping(y => y);
73
74			// Adding graph to plotter
75			// todo
76			//plotter.AddLineGraph(new CompositeDataSource(xSrc, animatedDataSource),
77			//    new Pen(Brushes.Magenta, 3),
78			//    new PenDescription("Sin(x + phase)"));
79
80			timer.Interval = TimeSpan.FromMilliseconds(10);
81			timer.Tick += AnimatedPlot_Timer;
82			timer.IsEnabled = true;
83
84			// Force evertyhing plotted to be visible
85			plotter.FitToView();
86		}
87
88		private void Page_Unloaded(object sender, RoutedEventArgs e)
89		{
90			timer.Stop();
91		}
92	}
93}