PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 0ms 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
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 System.Globalization;
  17. namespace Microsoft.Research.DynamicDataDisplay.Xbap.Samples.Demos.v02
  18. {
  19. /// <summary>
  20. /// Interaction logic for AnimationSample.xaml
  21. /// </summary>
  22. public partial class AnimationSample : Page
  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. /// <summary>Programmatically created header</summary>
  29. Header chartHeader = new Header();
  30. /// <summary>Text contents of header</summary>
  31. TextBlock headerContents = new TextBlock();
  32. /// <summary>Timer to animate data</summary>
  33. readonly DispatcherTimer timer = new DispatcherTimer();
  34. public AnimationSample()
  35. {
  36. InitializeComponent();
  37. headerContents.FontSize = 24;
  38. headerContents.Text = "Phase = 0.00";
  39. headerContents.HorizontalAlignment = HorizontalAlignment.Center;
  40. chartHeader.Content = headerContents;
  41. plotter.Children.Add(chartHeader);
  42. }
  43. private void AnimatedPlot_Timer(object sender, EventArgs e)
  44. {
  45. phase += 0.01;
  46. if (phase > 2 * Math.PI)
  47. phase -= 2 * Math.PI;
  48. for (int i = 0; i < animatedX.Length; i++)
  49. animatedY[i] = Math.Sin(animatedX[i] + phase);
  50. // Here it is - signal that data is updated
  51. animatedDataSource.RaiseDataChanged();
  52. headerContents.Text = String.Format(CultureInfo.InvariantCulture, "Phase = {0:N2}", phase);
  53. }
  54. private void Window_Loaded(object sender, RoutedEventArgs e)
  55. {
  56. for (int i = 0; i < animatedX.Length; i++)
  57. {
  58. animatedX[i] = 2 * Math.PI * i / animatedX.Length;
  59. animatedY[i] = Math.Sin(animatedX[i]);
  60. }
  61. EnumerableDataSource<double> xSrc = new EnumerableDataSource<double>(animatedX);
  62. xSrc.SetXMapping(x => x);
  63. animatedDataSource = new EnumerableDataSource<double>(animatedY);
  64. animatedDataSource.SetYMapping(y => y);
  65. // Adding graph to plotter
  66. // todo
  67. //plotter.AddLineGraph(new CompositeDataSource(xSrc, animatedDataSource),
  68. // new Pen(Brushes.Magenta, 3),
  69. // new PenDescription("Sin(x + phase)"));
  70. timer.Interval = TimeSpan.FromMilliseconds(10);
  71. timer.Tick += AnimatedPlot_Timer;
  72. timer.IsEnabled = true;
  73. // Force evertyhing plotted to be visible
  74. plotter.FitToView();
  75. }
  76. private void Page_Unloaded(object sender, RoutedEventArgs e)
  77. {
  78. timer.Stop();
  79. }
  80. }
  81. }