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