Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scrum 51 finishing touches #34

Merged
merged 5 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions src/ProfHeat.AUI/ViewModels/DataVisualizerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@
using LiveChartsCore.Defaults;
using LiveChartsCore.Drawing;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Painting;
using ProfHeat.Core.Interfaces;
using ProfHeat.Core.Models;
using SkiaSharp;

namespace ProfHeat.AUI.ViewModels;

public partial class DataVisualizerViewModel : BaseViewModel
{
#region Fields
// Instances of managers.
private readonly IResultDataManager _ResultDataManager;
private readonly IResultDataManager _resultDataManager;

// Observable properties.
[ObservableProperty, NotifyCanExecuteChangedFor(nameof(ExportResultsCommand))]
Expand Down Expand Up @@ -53,13 +55,23 @@ public string SelectedPeriod
public ObservableCollection<ISeries> ProducedHeat => GetLineSeries(result => result.ProducedHeat);
public ObservableCollection<ISeries> GasConsumption => GetLineSeries(result => result.GasConsumption);
public ObservableCollection<ISeries> ElectricityProduced => GetLineSeries(result => result.ElectricityProduced);
public static Axis[] XAxes => [new DateTimeAxis(TimeSpan.FromHours(1), date => date.ToString("yy MMM dd',' HH'h'"))];
public static DrawMarginFrame DrawMarginFrame => new() { Stroke = new SolidColorPaint(SKColors.White, 1) };
public static Axis[] XAxes =>
[new DateTimeAxis(TimeSpan.FromHours(1), date => date.ToString("yy MMM dd',' HH'h'"))
{ LabelsPaint = new SolidColorPaint(SKColors.White) }];
public static Axis[] CostsYAxis { get; } = GetYAxis("DKK / MWh(th)");
public static Axis[] CO2EmissionsYAxis { get; } = GetYAxis("kg / MWh(th)");
public static Axis[] ProducedHeatYAxis { get; } = GetYAxis("MW");
public static Axis[] GasConsumptionYAxis { get; } = GetYAxis("MWh(gas) / MWh(th)");
public static Axis[] ElectricityProducedYAxis { get; } = GetYAxis("MW");
public SolidColorPaint LegendTextPaint { get; } = new() { Color = SKColors.White };

#endregion

#region Constructor
public DataVisualizerViewModel(IResultDataManager resultDataManager, List<OptimizationResult> results)
{
_ResultDataManager = resultDataManager;
_resultDataManager = resultDataManager;
SelectedPeriod = Periods[0];
Results = results;
}
Expand All @@ -77,7 +89,7 @@ public async Task ImportResults(string filePath = null!)
if (!string.IsNullOrEmpty(filePath))
{
Results.Clear();
Results.AddRange(_ResultDataManager.LoadResultData(filePath));
Results.AddRange(_resultDataManager.LoadResultData(filePath));

OnPropertyChanged(nameof(Results));
OnPropertyChanged(nameof(Costs));
Expand Down Expand Up @@ -105,7 +117,7 @@ public async Task ExportResults(string filePath = null!)

if (!string.IsNullOrEmpty(filePath))
{
_ResultDataManager.SaveResultData(Results, filePath!);
_resultDataManager.SaveResultData(Results, filePath!);
}
}
catch (Exception exception)
Expand Down Expand Up @@ -133,5 +145,13 @@ private ObservableCollection<ISeries> GetLineSeries(Func<OptimizationResult, dou
GeometryFill = null,
Fill = null
}));

private static Axis[] GetYAxis(string metric) =>
[new Axis
{
Name = metric,
NamePaint = new SolidColorPaint(SKColors.White),
LabelsPaint = new SolidColorPaint(SKColors.White)
}];
#endregion
}
16 changes: 12 additions & 4 deletions src/ProfHeat.AUI/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace ProfHeat.AUI.ViewModels;

public class MainWindowViewModel : BaseViewModel
public partial class MainWindowViewModel : BaseViewModel
{
#region Fields
// Instances of managers.
Expand All @@ -27,12 +27,16 @@ public class MainWindowViewModel : BaseViewModel
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data", "HeatingGrid.config") // Path to HeatingGrid.config.
);
private readonly ISourceDataManager _sourceDataManager = new SourceDataManager(new CsvRepository());
private readonly IResultDataManager _ResultDataManager = new ResultDataManager(new CsvRepository());
private readonly IResultDataManager _resultDataManager = new ResultDataManager(new CsvRepository());
private readonly IOptimizer _optimizer = new Optimizer();

// List of optimization results.
private readonly List<OptimizationResult> _results = [];

// Observable properties.
[ObservableProperty]
private int _selectedTabIndex = 0; // Sets Optimizer tab as default.

// ViewModels for the tabs.
public OptimizerViewModel Optimizer { get; }
public DataVisualizerViewModel DataVisualizer { get; }
Expand All @@ -41,8 +45,12 @@ public class MainWindowViewModel : BaseViewModel
#region Constructor
public MainWindowViewModel()
{
Optimizer = new OptimizerViewModel(_assetManager, _sourceDataManager, _optimizer, _results);
DataVisualizer = new DataVisualizerViewModel(_ResultDataManager, _results);
Optimizer = new OptimizerViewModel(_assetManager, _sourceDataManager, _optimizer, _results, ChangeTab);
DataVisualizer = new DataVisualizerViewModel(_resultDataManager, _results);
}
#endregion

#region Helper Methods
private void ChangeTab(int tabIndex) => SelectedTabIndex = tabIndex;
#endregion
}
5 changes: 4 additions & 1 deletion src/ProfHeat.AUI/ViewModels/OptimizerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public partial class OptimizerViewModel : BaseViewModel
private readonly HeatingGrid _grid;
private readonly List<MarketCondition> _marketConditions = [];
private readonly List<OptimizationResult> _results;
private readonly Action<int> _changeTab;

// Observable properties.
public ObservableCollection<CheckBoxItem> CheckBoxItems { get; }
Expand All @@ -36,11 +37,12 @@ public partial class OptimizerViewModel : BaseViewModel
#endregion

#region Constructor
public OptimizerViewModel(IAssetManager assetManager, ISourceDataManager sourceDataManager, IOptimizer optimizer, List<OptimizationResult> results)
public OptimizerViewModel(IAssetManager assetManager, ISourceDataManager sourceDataManager, IOptimizer optimizer, List<OptimizationResult> results, Action<int> changeTab)
{
_sourceDataManager = sourceDataManager;
_optimizer = optimizer;
_results = results;
_changeTab = changeTab;
_grid = assetManager.LoadAssets();

// initialize CheckBoxItems for UI.
Expand Down Expand Up @@ -101,6 +103,7 @@ public void Optimize()
{
_results.Clear();
_results.AddRange(optimizationResults);
_changeTab?.Invoke(1); // Changes tab to Data Visualizer.
}
}
catch (Exception exception)
Expand Down
37 changes: 31 additions & 6 deletions src/ProfHeat.AUI/Views/DataVisualizerView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
x:Class="ProfHeat.AUI.Views.DataVisualizerView"
x:DataType="vm:DataVisualizerViewModel"
xmlns:lvc="using:LiveChartsCore.SkiaSharpView.Avalonia">

<TabControl TabStripPlacement="Left">
<!-- Tab For buttons -->
<TabItem Header="Options" FontWeight="SemiBold">
Expand Down Expand Up @@ -40,28 +40,53 @@
<!-- Tab For Charts -->
<TabItem Header="Costs">
<lvc:CartesianChart Series="{Binding Costs}"
TooltipPosition="Top"
XAxes="{Binding XAxes}"
ZoomMode="X"/>
YAxes="{Binding CostsYAxis}"
LegendPosition="Bottom"
LegendTextPaint="{Binding LegendTextPaint}"
ZoomMode="ZoomX"
DrawMarginFrame="{Binding DrawMarginFrame}"/>
</TabItem>
<TabItem Header="CO2Emissions">
<lvc:CartesianChart Series="{Binding CO2Emissions}"
TooltipPosition="Top"
XAxes="{Binding XAxes}"
ZoomMode="X"/>
YAxes="{Binding CO2EmissionsYAxis}"
LegendPosition="Bottom"
LegendTextPaint="{Binding LegendTextPaint}"
ZoomMode="ZoomX"
DrawMarginFrame="{Binding DrawMarginFrame}"/>
</TabItem>
<TabItem Header="ProducedHeat">
<lvc:CartesianChart Series="{Binding ProducedHeat}"
TooltipPosition="Top"
XAxes="{Binding XAxes}"
ZoomMode="X"/>
YAxes="{Binding ProducedHeatYAxis}"
LegendPosition="Bottom"
LegendTextPaint="{Binding LegendTextPaint}"
ZoomMode="ZoomX"
DrawMarginFrame="{Binding DrawMarginFrame}"/>
</TabItem>
<TabItem Header="GasConsumption">
<lvc:CartesianChart Series="{Binding GasConsumption}"
TooltipPosition="Top"
XAxes="{Binding XAxes}"
ZoomMode="X"/>
YAxes="{Binding GasConsumptionYAxis}"
LegendPosition="Bottom"
LegendTextPaint="{Binding LegendTextPaint}"
ZoomMode="ZoomX"
DrawMarginFrame="{Binding DrawMarginFrame}"/>
</TabItem>
<TabItem Header="ElectricityProduced">
<lvc:CartesianChart Series="{Binding ElectricityProduced}"
TooltipPosition="Top"
XAxes="{Binding XAxes}"
ZoomMode="X"/>
YAxes="{Binding ElectricityProducedYAxis}"
LegendPosition="Bottom"
LegendTextPaint="{Binding LegendTextPaint}"
ZoomMode="ZoomX"
DrawMarginFrame="{Binding DrawMarginFrame}"/>
</TabItem>
</TabControl>
</UserControl>
3 changes: 2 additions & 1 deletion src/ProfHeat.AUI/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
IsAddTabButtonVisible="False"
CanDragTabs="False"
CanReorderTabs="False"
AllowDropTabs="False">
AllowDropTabs="False"
SelectedIndex="{Binding SelectedTabIndex}">
<!-- OptimizerView -->
<ui:TabViewItem Header="Optimizer"
IsClosable="False">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,20 @@ public class OptimizerViewModelTests
private readonly Mock<IAssetManager> _mockAssetManager = new();
private readonly Mock<ISourceDataManager> _mockSourceDataManager = new();
private readonly Mock<IOptimizer> _mockOptimizer = new();
private readonly Mock<Action<int>> _mockChangeTab = new();
private readonly List<MarketCondition> _marketConditions = [new(
new DateTime(2023, 02, 08, 0, 0, 0, DateTimeKind.Unspecified),
new DateTime(2023, 02, 08, 1, 0, 0, DateTimeKind.Unspecified),
10.111, 10.111)];
private readonly HeatingGrid _grid = new ("test", "/test.svg", 1000, [new ("Gas Boiler", "/Assets/Images/GasBoiler.svg", 5, 500, 215, 1.01, 0)]);
private readonly HeatingGrid _grid = new("test", "/test.svg", 1000, [new("Gas Boiler", "/Assets/Images/GasBoiler.svg", 5, 500, 215, 1.01, 0)]);
private readonly List<OptimizationResult> _results = [];
private readonly OptimizerViewModel _viewModel;
private const string _filePath = "testFile.csv";

public OptimizerViewModelTests()
{
_ = _mockAssetManager.Setup(x => x.LoadAssets()).Returns(_grid);
_viewModel = new OptimizerViewModel(_mockAssetManager.Object, _mockSourceDataManager.Object, _mockOptimizer.Object, _results);
_viewModel = new OptimizerViewModel(_mockAssetManager.Object, _mockSourceDataManager.Object, _mockOptimizer.Object, _results, _mockChangeTab.Object);
}

[Fact]
Expand Down Expand Up @@ -83,5 +84,6 @@ public void OptimizeCommand_Executes()
// Assert
_mockOptimizer.Verify(sourceDataManager => sourceDataManager.Optimize(selectedUnits, It.IsAny<List<MarketCondition>>()), Times.Once());
Assert.NotEmpty(_results);
_mockChangeTab.Verify(changeTab => changeTab.Invoke(1), Times.Once());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void Save_Test()
repository.Save(data, _filePath);

// Assert
Assert.Equal(_testFile.Replace(" ",""), File.ReadAllText(_filePath).Replace(" ", ""));
Assert.Equal(_testFile.Replace(" ", ""), File.ReadAllText(_filePath).Replace(" ", ""));
}

[Fact]
Expand Down
Loading