PageRenderTime 10ms CodeModel.GetById 2ms app.highlight 4ms RepoModel.GetById 2ms app.codeStats 0ms

/Main/src/Xbap/DynamicDataDisplay.Xbap.Samples/Demos/v02/CurrencyExchange/CurrencyExchangeSample.xaml.cs

#
C# | 87 lines | 68 code | 11 blank | 8 comment | 1 complexity | aa817a4c0d002d5eb7677da4cb8ed548 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.Navigation;
13using System.Windows.Shapes;
14using Microsoft.Research.DynamicDataDisplay.DataSources;
15using System.IO;
16using System.Globalization;
17using System.Reflection;
18
19namespace Microsoft.Research.DynamicDataDisplay.Samples.Demos.v02.CurrencyExchange
20{
21	/// <summary>
22	/// Interaction logic for CurrencyExchangeSample.xaml
23	/// </summary>
24	public partial class CurrencyExchangeSample : Page
25	{
26		public CurrencyExchangeSample()
27		{
28			InitializeComponent();
29
30			Loaded += new RoutedEventHandler(Window1_Loaded);
31		}
32
33		List<CurrencyInfo> usd;
34		List<CurrencyInfo> eur;
35		List<CurrencyInfo> gbp;
36		List<CurrencyInfo> jpy;
37		private void Window1_Loaded(object sender, RoutedEventArgs e)
38		{
39			usd = LoadCurrencyRates("usd.txt");
40			eur = LoadCurrencyRates("eur.txt");
41			gbp = LoadCurrencyRates("gbp.txt");
42			jpy = LoadCurrencyRates("jpy.txt");
43
44			Color[] colors = ColorHelper.CreateRandomColors(4);
45
46			// todo
47			//plotter.AddLineGraph(CreateCurrencyDataSource(usd), colors[0], 1, "RUB / USD");
48			//plotter.AddLineGraph(CreateCurrencyDataSource(eur), colors[1], 1, "RUB / EURO");
49			//plotter.AddLineGraph(CreateCurrencyDataSource(gbp), colors[2], 1, "RUB / GBP");
50			//plotter.AddLineGraph(CreateCurrencyDataSource(jpy), colors[3], 1, "RUB / JPY");
51		}
52
53		private EnumerableDataSource<CurrencyInfo> CreateCurrencyDataSource(List<CurrencyInfo> rates)
54		{
55			EnumerableDataSource<CurrencyInfo> ds = new EnumerableDataSource<CurrencyInfo>(rates);
56			ds.SetXMapping(ci => dateAxis.ConvertToDouble(ci.Date));
57			ds.SetYMapping(ci => ci.Rate);
58			return ds;
59		}
60
61		private static List<CurrencyInfo> LoadCurrencyRates(string fileName)
62		{
63			var assembly = Assembly.GetExecutingAssembly();
64			using (Stream resourceStream = assembly.GetManifestResourceStream("Microsoft.Research.DynamicDataDisplay.Xbap.Samples.Demos.v02.CurrencyExchange." + fileName))
65			{
66				using (StreamReader reader = new StreamReader(resourceStream))
67				{
68					var strings = reader.ReadToEnd().Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
69
70					var res = new List<CurrencyInfo>(strings.Length - 1);
71					for (int i = 1; i < strings.Length; i++)
72					{
73						string line = strings[i];
74						string[] subLines = line.Split('\t');
75
76						DateTime date = DateTime.Parse(subLines[1]);
77						double rate = Double.Parse(subLines[5], CultureInfo.InvariantCulture);
78
79						res.Add(new CurrencyInfo { Date = date, Rate = rate });
80					}
81
82					return res;
83				}
84			}
85		}
86	}
87}