PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

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

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