PageRenderTime 35ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
C# | 134 lines | 106 code | 20 blank | 8 comment | 9 complexity | 1329d9d75f31aa0a54fdfc91ff19dd82 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.Shapes;
  13. using Microsoft.Research.DynamicDataDisplay;
  14. using Microsoft.Research.DynamicDataDisplay.DataSources;
  15. using Microsoft.Research.DynamicDataDisplay.PointMarkers;
  16. using Microsoft.Research.DynamicDataDisplay.Charts;
  17. using Microsoft.Research.DynamicDataDisplay.ViewportConstraints;
  18. namespace Microsoft.Research.DynamicDataDisplay.Demo
  19. {
  20. /// <summary>
  21. /// Interaction logic for MainWindow.xaml
  22. /// </summary>
  23. public partial class MainWindow : Window
  24. {
  25. public MainWindow()
  26. {
  27. InitializeComponent();
  28. Loaded += new RoutedEventHandler(MainWindow_Loaded);
  29. }
  30. LineGraph line;
  31. private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  32. {
  33. // Prepare data in arrays
  34. const int N = 100;
  35. double[] x = new double[N];
  36. double[] y = new double[N];
  37. for (int i = 0; i < N; i++)
  38. {
  39. x[i] = i * 0.1;
  40. y[i] = Math.Cos(x[i]);
  41. }
  42. // Add data sources:
  43. var yDataSource = new EnumerableDataSource<double>(y);
  44. yDataSource.SetYMapping(Y => Y);
  45. yDataSource.AddMapping(ShapeElementPointMarker.ToolTipTextProperty,
  46. Y => string.Format("Value is {0}", Y));
  47. var xDataSource = new EnumerableDataSource<double>(x);
  48. xDataSource.SetXMapping(X => X);
  49. CompositeDataSource compositeDataSource = new CompositeDataSource(xDataSource, yDataSource);
  50. Matrix m = Matrix.Identity;
  51. m.RotateAt(45, 10, 10);
  52. line = new LineGraph
  53. {
  54. Stroke = Brushes.Green,
  55. StrokeThickness = 2,
  56. DataTransform =
  57. //new MatrixDataTransform(m)
  58. new RotateDataTransform(45.0.DegreesToRadians())
  59. };
  60. line.DataSource = compositeDataSource;
  61. line.AddToPlotter(plotter);
  62. plotter.Viewport.Constraints.Add(new PhysicalProportionsConstraint { ProportionRatio = 1 });
  63. // adding graph to plotter
  64. plotter.AddLineGraph(compositeDataSource,
  65. new Pen(Brushes.Goldenrod, 3),
  66. new SampleMarker(),
  67. new PenDescription("Cosine"));
  68. //plotter.Viewport.FitToViewRestrictions.Add(new FollowDataWidthRestriction { Width = 1 });
  69. plotter.PreviewKeyDown += plotter_KeyDown;
  70. }
  71. void plotter_KeyDown(object sender, KeyEventArgs e)
  72. {
  73. if ((e.Key == Key.OemPlus || e.Key == Key.OemMinus) &&
  74. (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
  75. {
  76. double sign = e.Key == Key.OemPlus ? +1 : -1;
  77. RotateDataTransform transform = (RotateDataTransform)line.DataTransform;
  78. line.DataTransform = new RotateDataTransform(transform.Angle + sign * 0.01);
  79. e.Handled = true;
  80. }
  81. }
  82. }
  83. public class SampleMarker : ShapeElementPointMarker
  84. {
  85. public override UIElement CreateMarker()
  86. {
  87. Canvas result = new Canvas()
  88. {
  89. Width = 10,
  90. Height = Size
  91. };
  92. SetMarkerProperties(result);
  93. return result;
  94. }
  95. public override void SetMarkerProperties(UIElement marker)
  96. {
  97. Canvas result = (Canvas)marker;
  98. result.Width = Size;
  99. result.Height = Size;
  100. result.Background = Brush;
  101. if (ToolTipText != String.Empty)
  102. {
  103. ToolTip tt = new ToolTip();
  104. tt.Content = ToolTipText;
  105. result.ToolTip = tt;
  106. }
  107. }
  108. public override void SetPosition(UIElement marker, Point screenPoint)
  109. {
  110. Canvas.SetLeft(marker, screenPoint.X - Size / 2);
  111. Canvas.SetTop(marker, screenPoint.Y - Size / 2);
  112. }
  113. }
  114. }