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