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

Last sec changes #35

Merged
merged 1 commit into from
May 16, 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
81 changes: 41 additions & 40 deletions src/ProfHeat.AUI/ViewModels/DataVisualizerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,37 +33,28 @@ public partial class DataVisualizerViewModel : BaseViewModel
[ObservableProperty, NotifyCanExecuteChangedFor(nameof(ExportResultsCommand))]
private List<OptimizationResult> _results;

[ObservableProperty]
private string _selectedPeriod = String.Empty;
public string SelectedPeriod
{
get => _selectedPeriod;
set
{
_selectedPeriod = value;
OnPropertyChanged(nameof(Results));
OnPropertyChanged(nameof(Costs));
OnPropertyChanged(nameof(CO2Emissions));
OnPropertyChanged(nameof(ProducedHeat));
OnPropertyChanged(nameof(GasConsumption));
OnPropertyChanged(nameof(ElectricityProduced));
OnPropertyChanged(nameof(XAxes));
}
}

public ObservableCollection<string> Periods { get; } = ["Winter", "Summer"];
public ObservableCollection<ISeries> Costs => GetLineSeries(result => result.Costs);
public ObservableCollection<ISeries> CO2Emissions => GetLineSeries(result => result.CO2Emissions);
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 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[] CostsYAxis { get; } = GetYAxis("DKK");
public static Axis[] CO2EmissionsYAxis { get; } = GetYAxis("kg");
public static Axis[] ProducedHeatYAxis { get; } = GetYAxis("MWh");
public static Axis[] GasConsumptionYAxis { get; } = GetYAxis("MWh");
public static Axis[] ElectricityProducedYAxis { get; } = GetYAxis("MW");

public SolidColorPaint LegendTextPaint { get; } = new() { Color = SKColors.White };

#endregion
Expand Down Expand Up @@ -91,13 +82,7 @@ public async Task ImportResults(string filePath = null!)
Results.Clear();
Results.AddRange(_resultDataManager.LoadResultData(filePath));

OnPropertyChanged(nameof(Results));
OnPropertyChanged(nameof(Costs));
OnPropertyChanged(nameof(CO2Emissions));
OnPropertyChanged(nameof(ProducedHeat));
OnPropertyChanged(nameof(GasConsumption));
OnPropertyChanged(nameof(ElectricityProduced));
OnPropertyChanged(nameof(XAxes));
NotifyPropertiesChanged();
ExportResultsCommand.NotifyCanExecuteChanged();
}
}
Expand Down Expand Up @@ -131,20 +116,23 @@ public async Task ExportResults(string filePath = null!)
private bool CanExport() => Results.Count > 0;

private ObservableCollection<ISeries> GetLineSeries(Func<OptimizationResult, double> selector) =>
new(Results
.GroupBy(r => r.UnitName)
.Select(group => new LineSeries<DateTimePoint>
{
Values = group
.Where(result => (SelectedPeriod == "Winter") ? result.TimeFrom.Month is >= 10 or <= 3 : result.TimeFrom.Month is >= 4 and <= 9)
.Select(result => new DateTimePoint(result.TimeFrom, selector(result))),
Name = group.Key,
DataPadding = new LvcPoint(0.5f, 0),
LineSmoothness = 0,
GeometryStroke = null,
GeometryFill = null,
Fill = null
}));
new(Results
.GroupBy(r => r.UnitName)
.Select(group => new LineSeries<DateTimePoint>
{
Values = group
.Where(result => IsInSelectedPeriod(result.TimeFrom.Month))
.Select(result => new DateTimePoint(result.TimeFrom, selector(result))),
Name = group.Key,
DataPadding = new LvcPoint(0.5f, 0),
LineSmoothness = 0,
GeometryStroke = null,
GeometryFill = null,
Fill = null
}));

private bool IsInSelectedPeriod(int month) =>
SelectedPeriod == "Winter" ? month is >= 10 or <= 3 : month is >= 4 and <= 9;

private static Axis[] GetYAxis(string metric) =>
[new Axis
Expand All @@ -153,5 +141,18 @@ [new Axis
NamePaint = new SolidColorPaint(SKColors.White),
LabelsPaint = new SolidColorPaint(SKColors.White)
}];

partial void OnSelectedPeriodChanged(string value) => NotifyPropertiesChanged();

private void NotifyPropertiesChanged()
{
OnPropertyChanged(nameof(Results));
OnPropertyChanged(nameof(Costs));
OnPropertyChanged(nameof(CO2Emissions));
OnPropertyChanged(nameof(ProducedHeat));
OnPropertyChanged(nameof(GasConsumption));
OnPropertyChanged(nameof(ElectricityProduced));
OnPropertyChanged(nameof(XAxes));
}
#endregion
}
162 changes: 82 additions & 80 deletions src/ProfHeat.AUI/Views/DataVisualizerView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,84 +9,86 @@
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">
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<Button Content="Import Results"
Command="{Binding ImportResultsCommand}"
MinWidth="200"
Margin="10"/>
<ComboBox ItemsSource="{Binding Periods}"
SelectedItem="{Binding SelectedPeriod}"
MinWidth="200"
Margin="10">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Button Grid.Column="2"
Content="Export Results"
Command="{Binding ExportResultsCommand}"
MinWidth="200"
Margin="10"/>
</StackPanel>
</TabItem>
<!-- Tab For Charts -->
<TabItem Header="Costs">
<lvc:CartesianChart Series="{Binding Costs}"
TooltipPosition="Top"
XAxes="{Binding XAxes}"
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}"
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}"
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}"
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}"
YAxes="{Binding ElectricityProducedYAxis}"
LegendPosition="Bottom"
LegendTextPaint="{Binding LegendTextPaint}"
ZoomMode="ZoomX"
DrawMarginFrame="{Binding DrawMarginFrame}"/>
</TabItem>
</TabControl>
<DockPanel>
<TabControl DockPanel.Dock="Top" TabStripPlacement="Left" MinHeight="350">
<TabItem Header="Costs">
<lvc:CartesianChart Series="{Binding Costs}"
TooltipPosition="Top"
XAxes="{Binding XAxes}"
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}"
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}"
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}"
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}"
YAxes="{Binding ElectricityProducedYAxis}"
LegendPosition="Bottom"
LegendTextPaint="{Binding LegendTextPaint}"
ZoomMode="ZoomX"
DrawMarginFrame="{Binding DrawMarginFrame}"/>
</TabItem>
</TabControl>
<Grid DockPanel.Dock="Bottom" ColumnDefinitions="Auto,Auto,Auto"
VerticalAlignment="Bottom"
HorizontalAlignment="Center">
<ComboBox Grid.Column="0"
ItemsSource="{Binding Periods}"
SelectedItem="{Binding SelectedPeriod}"
MinWidth="200"
Margin="10">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Button Grid.Column="1"
Content="Import Results"
Command="{Binding ImportResultsCommand}"
MinWidth="200"
Margin="10"/>
<Button Grid.Column="2"
Content="Export Results"
Command="{Binding ExportResultsCommand}"
MinWidth="200"
Margin="10"/>
</Grid>
</DockPanel>
</UserControl>

Loading