/Main/src/DevSamples/ForestDisplaySample/Window1.xaml.cs

# · C# · 114 lines · 93 code · 16 blank · 5 comment · 2 complexity · 5c6b5cbf14876b62f3f1c42e7ec3e75f MD5 · raw file

  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 DynamicDataDisplay.Markers.MarkerGenerators;
  15. using Microsoft.Research.DynamicDataDisplay;
  16. using Microsoft.Research.DynamicDataDisplay.Common.Auxiliary;
  17. using System.Collections.ObjectModel;
  18. using DynamicDataDisplay.Markers.ForestDisplay;
  19. using System.IO;
  20. using System.Globalization;
  21. namespace ForestDisplaySample
  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. }
  32. private readonly ObservableCollection<ForestItem> forest = new ObservableCollection<ForestItem>();
  33. private void Window_Loaded(object sender, RoutedEventArgs e)
  34. {
  35. LoadForest();
  36. Dictionary<string, TreeSpeciesInfo> speciesMappings = new Dictionary<string, TreeSpeciesInfo>();
  37. speciesMappings["Abie.bals"] = new TreeSpeciesInfo(Brushes.LimeGreen, TreeViews.RoundTriangle);
  38. speciesMappings["Acer.rubr"] = new TreeSpeciesInfo(Brushes.LimeGreen, TreeViews.Ellipse);
  39. speciesMappings["Acer.sacc"] = new TreeSpeciesInfo(Brushes.SeaGreen, TreeViews.Ellipse);
  40. speciesMappings["Betu.papy"] = new TreeSpeciesInfo(Brushes.DarkOrange, TreeViews.RoundTriangle);
  41. speciesMappings["Popu.gran"] = new TreeSpeciesInfo(Brushes.Teal, TreeViews.Rectangle);
  42. speciesMappings["Popu.trem"] = new TreeSpeciesInfo(Brushes.SeaGreen, TreeViews.Rectangle);
  43. speciesMappings["Quer.rubr"] = new TreeSpeciesInfo(Brushes.DarkOrange, TreeViews.Triangle);
  44. speciesMappings["Tili.amer"] = new TreeSpeciesInfo(Brushes.Teal, TreeViews.Triangle);
  45. forestDisplayControl.SpeciesMappings = speciesMappings;
  46. Loaded -= Window_Loaded;
  47. forestDisplayControl.DataContext = forest;
  48. }
  49. private void LoadForest()
  50. {
  51. var lines = File.ReadAllLines(@"..\..\Data.csv");
  52. string[] ids = new[] { "Abie.bals", "Acer.rubr", "Acer.sacc", "Betu.papy", "Popu.gran", "Popu.trem", "Quer.rubr", "Tili.amer" };
  53. int index = 0;
  54. foreach (var line in lines)
  55. {
  56. var strValues = line.Split(',');
  57. double basalRadius = Parse(strValues[0]);
  58. double crownRadius = Parse(strValues[1]);
  59. double crownHeight = Parse(strValues[2]);
  60. double treeHeight = Parse(strValues[3]);
  61. // skipping y
  62. double x = Parse(strValues[5]);
  63. // skipping id
  64. string speciesID = strValues[7];
  65. if (speciesID == "")
  66. {
  67. speciesID = ids[index % 8];
  68. }
  69. ForestItem forestItem = new ForestItem
  70. {
  71. TrunkWidth = 2 * basalRadius,
  72. TreeHeight = treeHeight,
  73. CrownHeight = crownHeight,
  74. CrownWidth = 2 * crownRadius,
  75. SpeciesID = speciesID,
  76. X = x
  77. };
  78. forest.Add(forestItem);
  79. index++;
  80. }
  81. }
  82. private static double Parse(string str)
  83. {
  84. return Double.Parse(str, CultureInfo.InvariantCulture);
  85. }
  86. private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  87. {
  88. double prevPercent = e.OldValue;
  89. double currPercent = e.NewValue;
  90. double ratio = currPercent / prevPercent;
  91. foreach (var item in forest)
  92. {
  93. item.TreeHeight *= ratio;
  94. item.CrownHeight *= ratio;
  95. }
  96. }
  97. }
  98. }