Advertisement
peachyontop

gewrgerg

Apr 19th, 2025
7
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. using OxyPlot;
  2. using OxyPlot.Series;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Windows.Input;
  9. using Microsoft.Win32;
  10.  
  11. public class MainViewModel : INotifyPropertyChanged
  12. {
  13. private ObservableCollection<LoadData> _loadData = new ObservableCollection<LoadData>();
  14. private string _filePath;
  15. private double _averageLoad;
  16. private double _maxLoad;
  17. private double _minLoad;
  18. private PlotModel _plotModel;
  19.  
  20. public ObservableCollection<LoadData> LoadData
  21. {
  22. get => _loadData;
  23. set { _loadData = value; OnPropertyChanged(nameof(LoadData)); }
  24. }
  25.  
  26. public string FilePath { /* Implement similar to above */ }
  27. public double AverageLoad { /* ... */ }
  28. public double MaxLoad { /* ... */ }
  29. public double MinLoad { /* ... */ }
  30. public PlotModel PlotModel { /* ... */ }
  31.  
  32. public ICommand LoadCsvCommand => new RelayCommand(LoadCsv);
  33.  
  34. private void LoadCsv()
  35. {
  36. var openFileDialog = new OpenFileDialog
  37. {
  38. Filter = "CSV files (*.csv)|*.csv",
  39. Title = "Select Load Profile CSV"
  40. };
  41.  
  42. if (openFileDialog.ShowDialog() == true)
  43. {
  44. FilePath = openFileDialog.FileName;
  45. ProcessCsvFile();
  46. UpdateStatistics();
  47. UpdatePlot();
  48. }
  49. }
  50.  
  51. private void ProcessCsvFile()
  52. {
  53. LoadData.Clear();
  54. var lines = File.ReadAllLines(FilePath).Skip(1); // Skip header
  55.  
  56. foreach (var line in lines)
  57. {
  58. var values = line.Split(',');
  59. if (DateTime.TryParse(values[0], out DateTime date) &&
  60. double.TryParse(values[1], NumberStyles.Any, CultureInfo.InvariantCulture, out double load))
  61. {
  62. LoadData.Add(new LoadData { DateTime = date, Load = load });
  63. }
  64. }
  65. }
  66.  
  67. private void UpdateStatistics()
  68. {
  69. if (LoadData.Any())
  70. {
  71. AverageLoad = LoadData.Average(d => d.Load);
  72. MaxLoad = LoadData.Max(d => d.Load);
  73. MinLoad = LoadData.Min(d => d.Load);
  74. }
  75. }
  76.  
  77. private void UpdatePlot()
  78. {
  79. var plot = new PlotModel { Title = "Load Profile" };
  80. var series = new LineSeries();
  81.  
  82. foreach (var dataPoint in LoadData)
  83. {
  84. series.Points.Add(new DataPoint(
  85. DateTimeAxis.ToDouble(dataPoint.DateTime),
  86. dataPoint.Load));
  87. }
  88.  
  89. plot.Series.Add(series);
  90. plot.Axes.Add(new DateTimeAxis { Position = AxisPosition.Bottom });
  91. plot.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Title = "Load [kW]" });
  92. PlotModel = plot;
  93. }
  94.  
  95. public event PropertyChangedEventHandler PropertyChanged;
  96. protected virtual void OnPropertyChanged(string propertyName) =>
  97. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  98. }
  99.  
  100. public class RelayCommand : ICommand
  101. {
  102. private readonly Action _execute;
  103. public RelayCommand(Action execute) => _execute = execute;
  104. public bool CanExecute(object parameter) => true;
  105. public void Execute(object parameter) => _execute();
  106. public event EventHandler CanExecuteChanged;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement