PageRenderTime 33ms CodeModel.GetById 14ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/Main/src/Xbap/DynamicDataDisplay.Xbap.Samples/Demos/v03/Isolines/Isolines.xaml.cs

#
C# | 113 lines | 94 code | 13 blank | 6 comment | 10 complexity | 5ca0f58beef1a28f1d124498505227a8 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Globalization;
  4using System.IO;
  5using System.Windows;
  6using System.Windows.Controls;
  7using System.Windows.Documents;
  8using Microsoft.Research.DynamicDataDisplay.Common.Auxiliary;
  9using Microsoft.Research.DynamicDataDisplay.DataSources.MultiDimensional;
 10using System.Reflection;
 11
 12namespace Microsoft.Research.DynamicDataDisplay.Samples.Demos.v03.Isolines
 13{
 14	/// <summary>
 15	/// Interaction logic for Isolines.xaml
 16	/// </summary>
 17	public partial class Isolines : Page
 18	{
 19		public Isolines()
 20		{
 21			InitializeComponent();
 22
 23			Loaded += new RoutedEventHandler(Window1_Loaded);
 24		}
 25
 26		private void Window1_Loaded(object sender, RoutedEventArgs e)
 27		{
 28			LoadField();
 29		}
 30
 31		private static string[] ParseDataString(string str)
 32		{
 33			str = str.TrimEnd(' ');
 34			return str.Split(' ');
 35		}
 36
 37		private static string[] ParseGridString(string str)
 38		{
 39			return str.TrimEnd(' ').
 40				Substring(0, str.Length - 3).
 41				TrimStart('{').
 42				Split(new string[] { " } {" }, StringSplitOptions.None);
 43		}
 44
 45		private void LoadField()
 46		{
 47			var assembly = Assembly.GetExecutingAssembly();
 48
 49			List<string> strings = new List<string>();
 50			using (Stream stream = assembly.GetManifestResourceStream("Microsoft.Research.DynamicDataDisplay.Xbap.Samples.Demos.v03.Isolines.SampleData.txt"))
 51			{
 52				using (StreamReader reader = new StreamReader(stream))
 53				{
 54					while (!reader.EndOfStream)
 55					{
 56						string str = reader.ReadLine();
 57						if (str == "Data:")
 58						{
 59							// do nothing
 60						}
 61						else if (str == "Grid:")
 62						{
 63							// do nothing too
 64						}
 65						else
 66						{
 67							strings.Add(str);
 68						}
 69					}
 70				}
 71			}
 72
 73			// data
 74			string[] nums = ParseDataString(strings[0]);
 75			int width = nums.Length;
 76			int height = strings.Count / 2;
 77
 78			CultureInfo culture = new CultureInfo("ru-RU");
 79
 80			double[,] data = new double[width, height];
 81			for (int row = 0; row < height; row++)
 82			{
 83				nums = ParseDataString(strings[row]);
 84				for (int column = 0; column < width; column++)
 85				{
 86					double d = Double.Parse(nums[column], culture);
 87					data[column, row] = d;
 88				}
 89			}
 90
 91			Point[,] gridData = new Point[width, height];
 92			for (int row = 0; row < height; row++)
 93			{
 94				string str = strings[row + height];
 95				nums = ParseGridString(str);
 96				for (int column = 0; column < width; column++)
 97				{
 98					string[] vecStrs = nums[column].Split(new string[] { "; " }, StringSplitOptions.None);
 99					gridData[column, row] = new Point(
100						Double.Parse(vecStrs[0], culture),
101						Double.Parse(vecStrs[1], culture));
102				}
103			}
104
105			WarpedDataSource2D<double> dataSource = new WarpedDataSource2D<double>(data, gridData);
106			isolineGraph.DataSource = dataSource;
107			trackingGraph.DataSource = dataSource;
108
109			var visible = dataSource.GetGridBounds();
110			plotter.Viewport.Visible = visible;
111		}
112	}
113}