Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using OxyPlot;
- using OxyPlot.Series;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Windows.Input;
- using Microsoft.Win32;
- public class MainViewModel : INotifyPropertyChanged
- {
- private ObservableCollection<LoadData> _loadData = new ObservableCollection<LoadData>();
- private string _filePath;
- private double _averageLoad;
- private double _maxLoad;
- private double _minLoad;
- private PlotModel _plotModel;
- public ObservableCollection<LoadData> LoadData
- {
- get => _loadData;
- set { _loadData = value; OnPropertyChanged(nameof(LoadData)); }
- }
- public string FilePath { /* Implement similar to above */ }
- public double AverageLoad { /* ... */ }
- public double MaxLoad { /* ... */ }
- public double MinLoad { /* ... */ }
- public PlotModel PlotModel { /* ... */ }
- public ICommand LoadCsvCommand => new RelayCommand(LoadCsv);
- private void LoadCsv()
- {
- var openFileDialog = new OpenFileDialog
- {
- Filter = "CSV files (*.csv)|*.csv",
- Title = "Select Load Profile CSV"
- };
- if (openFileDialog.ShowDialog() == true)
- {
- FilePath = openFileDialog.FileName;
- ProcessCsvFile();
- UpdateStatistics();
- UpdatePlot();
- }
- }
- private void ProcessCsvFile()
- {
- LoadData.Clear();
- var lines = File.ReadAllLines(FilePath).Skip(1); // Skip header
- foreach (var line in lines)
- {
- var values = line.Split(',');
- if (DateTime.TryParse(values[0], out DateTime date) &&
- double.TryParse(values[1], NumberStyles.Any, CultureInfo.InvariantCulture, out double load))
- {
- LoadData.Add(new LoadData { DateTime = date, Load = load });
- }
- }
- }
- private void UpdateStatistics()
- {
- if (LoadData.Any())
- {
- AverageLoad = LoadData.Average(d => d.Load);
- MaxLoad = LoadData.Max(d => d.Load);
- MinLoad = LoadData.Min(d => d.Load);
- }
- }
- private void UpdatePlot()
- {
- var plot = new PlotModel { Title = "Load Profile" };
- var series = new LineSeries();
- foreach (var dataPoint in LoadData)
- {
- series.Points.Add(new DataPoint(
- DateTimeAxis.ToDouble(dataPoint.DateTime),
- dataPoint.Load));
- }
- plot.Series.Add(series);
- plot.Axes.Add(new DateTimeAxis { Position = AxisPosition.Bottom });
- plot.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Title = "Load [kW]" });
- PlotModel = plot;
- }
- public event PropertyChangedEventHandler PropertyChanged;
- protected virtual void OnPropertyChanged(string propertyName) =>
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- public class RelayCommand : ICommand
- {
- private readonly Action _execute;
- public RelayCommand(Action execute) => _execute = execute;
- public bool CanExecute(object parameter) => true;
- public void Execute(object parameter) => _execute();
- public event EventHandler CanExecuteChanged;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement