PageRenderTime 26ms CodeModel.GetById 13ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

/Main/src/Samples/v0.2/Animation/MainWindow.xaml.cs

#
C# | 83 lines | 67 code | 10 blank | 6 comment | 3 complexity | dca6e62ae54c822a78acdae3dc2df8d8 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 Microsoft.Research.DynamicDataDisplay;
17using System.Globalization;
18
19namespace Animation
20{
21    /// <summary>Sample that show rendering of data changing in time</summary>
22    public partial class AnimatedSampleWindow : Window
23    {
24        double phase = 0;
25        readonly double[] animatedX = new double[1000];
26        readonly double[] animatedY = new double[1000];
27        EnumerableDataSource<double> animatedDataSource = null;
28
29        /// <summary>Programmatically created header</summary>
30        Header chartHeader = new Header();
31
32        /// <summary>Text contents of header</summary>
33        TextBlock headerContents = new TextBlock();
34
35        /// <summary>Timer to animate data</summary>
36        readonly DispatcherTimer timer = new DispatcherTimer();  
37
38        public AnimatedSampleWindow()
39        {
40            InitializeComponent();
41            headerContents.FontSize = 24;
42            headerContents.Text = "Phase = 0.00";
43            headerContents.HorizontalAlignment = HorizontalAlignment.Center;
44            chartHeader.Content = headerContents;
45            plotter.Children.Add(chartHeader);
46        }
47
48        private void AnimatedPlot_Timer(object sender, EventArgs e)
49        {
50            phase += 0.01;
51            if (phase > 2 * Math.PI)
52                phase -= 2 * Math.PI;
53            for (int i = 0; i < animatedX.Length; i++)
54                animatedY[i] = Math.Sin(animatedX[i] + phase);
55
56            // Here it is - signal that data is updated
57            animatedDataSource.RaiseDataChanged();
58            headerContents.Text = String.Format(CultureInfo.InvariantCulture, "Phase = {0:N2}", phase);
59        }
60
61        private void Window_Loaded(object sender, RoutedEventArgs e)
62        {
63            for (int i = 0; i < animatedX.Length; i++)
64            {
65                animatedX[i] = 2 * Math.PI * i / animatedX.Length;
66                animatedY[i] = Math.Sin(animatedX[i]);
67            }
68            EnumerableDataSource<double> xSrc = new EnumerableDataSource<double>(animatedX);
69            xSrc.SetXMapping(x => x);
70            animatedDataSource = new EnumerableDataSource<double>(animatedY);
71            animatedDataSource.SetYMapping(y => y);
72
73            // Adding graph to plotter
74            var line = plotter.AddLineGraph(new CompositeDataSource(xSrc, animatedDataSource),
75                new Pen(Brushes.Magenta, 3),                
76                new PenDescription("Sin(x + phase)"));
77
78			timer.Interval = TimeSpan.FromMilliseconds(10);
79            timer.Tick += AnimatedPlot_Timer;
80            timer.Start();
81        }
82    }
83}