PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Main/src/Samples/v0.4/PointsOnMapSampleApp/Window1.xaml.cs

#
C# | 205 lines | 163 code | 38 blank | 4 comment | 16 complexity | 4ff7fa8438a6452c06735571e2abd284 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 Microsoft.Research.DynamicDataDisplay;
  15. using Microsoft.Research.DynamicDataDisplay.PointMarkers;
  16. using Microsoft.Research.DynamicDataDisplay.DataSources;
  17. using System.IO;
  18. using System.Globalization;
  19. using Microsoft.Research.DynamicDataDisplay.Charts;
  20. using System.Diagnostics;
  21. namespace PointSetOnMap
  22. {
  23. /// <summary>
  24. /// Interaction logic for Window1.xaml
  25. /// </summary>
  26. public partial class Window1 : Window
  27. {
  28. public Window1()
  29. {
  30. InitializeComponent();
  31. dataTypeCombobox.ItemsSource = new List<DataType>
  32. {
  33. DataType.Temp, DataType.Rainfall, DataType.SoilDepth
  34. };
  35. Loaded += Window1_Loaded;
  36. }
  37. void Window1_Loaded(object sender, RoutedEventArgs e)
  38. {
  39. LoadData();
  40. dataTypeCombobox.SelectedIndex = 0;
  41. bool res = dataTypeCombobox.Focus();
  42. }
  43. private EnumerableDataSource<DataPoint> CreateDataSource(IEnumerable<DataPoint> data)
  44. {
  45. EnumerableDataSource<DataPoint> ds = new EnumerableDataSource<DataPoint>(data);
  46. MercatorTransform transform = new MercatorTransform();
  47. ds.SetXMapping(p => p.X);
  48. ds.SetYMapping(p => transform.DataToViewport(new Point(0, p.Y)).Y);
  49. ds.AddMapping(CirclePointMarker.FillProperty, dp =>
  50. {
  51. double alpha = (dp.Data - currentRange.Min) / (currentRange.Max - currentRange.Min);
  52. Debug.Assert(0 <= alpha && alpha <= 1);
  53. const double hueWidth = 100;
  54. double hue = hueWidth * (alpha - 0.5) + hueSlider.Value;
  55. if (hue > 360) hue -= 360;
  56. else if (hue < 0) hue += 360;
  57. Debug.Assert(0 <= hue && hue <= 360);
  58. Color mainColor = new HsbColor(hue, 1, 0 + 1 * alpha, 0.3 + 0.7 * alpha).ToArgbColor();
  59. const int colorCount = 5;
  60. GradientStopCollection colors = new GradientStopCollection(colorCount);
  61. double step = 1.0 / (colorCount - 1);
  62. for (int i = 0; i < colorCount; i++)
  63. {
  64. Color color = mainColor;
  65. double x = attSlider.Value * step * i;
  66. color.A = (byte)(255 * Math.Exp(-x * x));
  67. colors.Add(new GradientStop(color, step * i));
  68. }
  69. return new RadialGradientBrush(colors);
  70. });
  71. return ds;
  72. }
  73. private List<SampleDataPoint> loadedData = new List<SampleDataPoint>();
  74. private void LoadData()
  75. {
  76. string[] strings = File.ReadAllLines("example_for_visualization.csv");
  77. // skipping 1st line, parsing all other lines
  78. for (int i = 1; i < strings.Length; i++)
  79. {
  80. SampleDataPoint point = ParseDataPoint(strings[i]);
  81. loadedData.Add(point);
  82. }
  83. tempRange.Min = loadedData.Min(p => p.Temp);
  84. tempRange.Max = loadedData.Max(p => p.Temp);
  85. rainfallRange.Min = loadedData.Min(p => p.RainFall);
  86. rainfallRange.Max = loadedData.Max(p => p.RainFall);
  87. soildepthRange.Min = loadedData.Min(p => p.SoilDepth);
  88. soildepthRange.Max = loadedData.Max(p => p.SoilDepth);
  89. }
  90. MinMax tempRange = new MinMax();
  91. MinMax rainfallRange = new MinMax();
  92. MinMax soildepthRange = new MinMax();
  93. MinMax currentRange;
  94. private SampleDataPoint ParseDataPoint(string str)
  95. {
  96. var pieces = str.Split(',');
  97. SampleDataPoint res = new SampleDataPoint();
  98. res.Lat = Double.Parse(pieces[0], CultureInfo.InvariantCulture);
  99. res.Lon = Double.Parse(pieces[1], CultureInfo.InvariantCulture);
  100. res.Temp = Double.Parse(pieces[2], CultureInfo.InvariantCulture);
  101. res.RainFall = Double.Parse(pieces[3], CultureInfo.InvariantCulture);
  102. res.SoilDepth = Double.Parse(pieces[4], CultureInfo.InvariantCulture);
  103. return res;
  104. }
  105. private IEnumerable<DataPoint> GetSampleData(DataType dataType)
  106. {
  107. switch (dataType)
  108. {
  109. case DataType.Temp:
  110. currentRange = tempRange;
  111. return loadedData.Select(dp => new DataPoint { X = dp.Lon, Y = dp.Lat, Data = dp.Temp }).ToList();
  112. case DataType.Rainfall:
  113. currentRange = rainfallRange;
  114. return loadedData.Select(dp => new DataPoint { X = dp.Lon, Y = dp.Lat, Data = dp.RainFall }).ToList();
  115. case DataType.SoilDepth:
  116. currentRange = soildepthRange;
  117. return loadedData.Select(dp => new DataPoint { X = dp.Lon, Y = dp.Lat, Data = dp.SoilDepth }).ToList();
  118. default:
  119. throw new InvalidOperationException();
  120. }
  121. }
  122. private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  123. {
  124. if (plotter == null)
  125. return;
  126. var graph = plotter.Children.OfType<MarkerPointsGraph>().FirstOrDefault();
  127. if (graph != null)
  128. graph.InvalidateVisual();
  129. }
  130. private void dataTypeCombobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  131. {
  132. if (graph == null || plotter == null)
  133. return;
  134. graph.DataSource = CreateDataSource(GetSampleData((DataType)dataTypeCombobox.SelectedValue));
  135. }
  136. private void Hyperlink_Click(object sender, RoutedEventArgs e)
  137. {
  138. Hyperlink link = (Hyperlink)sender;
  139. Process.Start(link.NavigateUri.ToString());
  140. }
  141. }
  142. class MinMax
  143. {
  144. public double Min { get; set; }
  145. public double Max { get; set; }
  146. }
  147. class SampleDataPoint
  148. {
  149. public double Lat { get; set; }
  150. public double Lon { get; set; }
  151. public double Temp { get; set; }
  152. public double RainFall { get; set; }
  153. public double SoilDepth { get; set; }
  154. }
  155. class DataPoint
  156. {
  157. public double X { get; set; }
  158. public double Y { get; set; }
  159. public double Data { get; set; }
  160. }
  161. enum DataType
  162. {
  163. Temp,
  164. Rainfall,
  165. SoilDepth
  166. }
  167. }