PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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