diff --git a/Elwig/Windows/ChartWindow.xaml.cs b/Elwig/Windows/ChartWindow.xaml.cs index b470523..9bd7601 100644 --- a/Elwig/Windows/ChartWindow.xaml.cs +++ b/Elwig/Windows/ChartWindow.xaml.cs @@ -26,12 +26,12 @@ namespace Elwig.Windows { private bool HasChanged = false; private bool Updating = false; - private Scatter DataPlot; + private Scatter? DataPlot; private Scatter? GebundenPlot; - private Marker HighlightedPointPlot; - private Marker PrimaryMarkedPointPlot; - private Marker SecondaryMarkedPointPlot; - private Text TooltipPlot; + private Marker? HighlightedPointPlot; + private Marker? PrimaryMarkedPointPlot; + private Marker? SecondaryMarkedPointPlot; + private Text? TooltipPlot; private static readonly LegendItem UngebundenLegend = new() { @@ -77,7 +77,7 @@ namespace Elwig.Windows { PriceInput.Unit = $"{CurrencySymbol}/kg"; GebundenFlatBonus.Unit = $"{CurrencySymbol}/kg"; PaymentVar = ctx.PaymentVariants.Find(year, avnr) ?? throw new ArgumentException("PaymentVar not found"); - Title = $"{PaymentVar?.Name} - Lese {year} - Elwig"; + Title = $"{PaymentVar.Name} - Lese {year} - Elwig"; LockContext = true; } @@ -215,10 +215,12 @@ namespace Elwig.Windows { GebundenPlot.MarkerStyle = new MarkerStyle(MarkerShape.FilledCircle, 9, ColorGebunden); } - DataPlot = OechslePricePlot.Plot.Add.Scatter(SelectedGraphEntry!.DataGraph.DataX, SelectedGraphEntry!.DataGraph.DataY); - DataPlot.LineStyle.Color = ColorUngebunden; - DataPlot.Color = ColorUngebunden; - DataPlot.MarkerStyle = new MarkerStyle(MarkerShape.FilledCircle, 9, ColorUngebunden); + if (SelectedGraphEntry != null) { + DataPlot = OechslePricePlot.Plot.Add.Scatter(SelectedGraphEntry.DataGraph.DataX, SelectedGraphEntry.DataGraph.DataY); + DataPlot.LineStyle.Color = ColorUngebunden; + DataPlot.Color = ColorUngebunden; + DataPlot.MarkerStyle = new MarkerStyle(MarkerShape.FilledCircle, 9, ColorUngebunden); + } OechslePricePlot.Plot.Axes.SetLimits(Math.Min(GraphEntry.MinX, GraphEntry.MinXGeb) - 1, GraphEntry.MaxX + 1, -0.1, 1.5); @@ -244,7 +246,10 @@ namespace Elwig.Windows { SecondaryMarkedPoint = -1; ChangeActiveGraph(null); HideGradationLines(); - OechslePricePlot.Plot.Remove(DataPlot); + if (DataPlot != null) { + OechslePricePlot.Plot.Remove(DataPlot); + DataPlot = null; + } if (GebundenPlot != null) { OechslePricePlot.Plot.Remove(GebundenPlot); GebundenPlot = null; @@ -254,9 +259,9 @@ namespace Elwig.Windows { OechslePricePlot.Refresh(); } - private void ChangeMarker(Marker point, bool visible, double x = 0, double y = 0) { - point.Location = new Coordinates(x, y); - point.IsVisible = visible; + private static void ChangeMarker(Marker? point, bool visible, double x = 0, double y = 0) { + point?.Location = new Coordinates(x, y); + point?.IsVisible = visible; } private void EnableActionButtons() { @@ -416,7 +421,7 @@ namespace Elwig.Windows { var res = Validator.CheckDecimal(PriceInput, true, 2, Season.Precision); if (res.IsValid && double.TryParse(PriceInput.Text, out double price)) { ActiveGraph.SetPriceAt(PrimaryMarkedPoint, price); - PrimaryMarkedPointPlot.Location = new Coordinates(PrimaryMarkedPointPlot.Location.X, price); + PrimaryMarkedPointPlot?.Location = new Coordinates(PrimaryMarkedPointPlot.Location.X, price); SetHasChanged(); OechslePricePlot.Refresh(); CheckGebundenTypeFixed(); @@ -492,17 +497,19 @@ namespace Elwig.Windows { ChangeMarker(PrimaryMarkedPointPlot, true, ActiveGraph.GetOechsleAt(PrimaryMarkedPoint), ActiveGraph.GetPriceAt(PrimaryMarkedPoint)); - Updating = true; - OechsleInput.Text = Highlighted.Graph!.GetOechsleAt(Highlighted.Index).ToString(); - GebInput.IsChecked = Highlighted.Graph == SelectedGraphEntry?.GebundenGraph; - PriceInput.Text = Math.Round(Highlighted.Graph.GetPriceAt(Highlighted.Index), Season.Precision).ToString(); - Updating = false; + if (Highlighted.Graph != null) { + Updating = true; + OechsleInput.Text = Highlighted.Graph.GetOechsleAt(Highlighted.Index).ToString(); + GebInput.IsChecked = Highlighted.Graph == SelectedGraphEntry?.GebundenGraph; + PriceInput.Text = Math.Round(Highlighted.Graph.GetPriceAt(Highlighted.Index), Season.Precision).ToString(); + Updating = false; + } EnableActionButtons(); } else { PrimaryMarkedPoint = -1; SecondaryMarkedPoint = -1; - if (SelectedGraphEntry!.GebundenGraph != null) { + if (SelectedGraphEntry?.GebundenGraph != null) { ChangeActiveGraph(null); } ChangeMarker(PrimaryMarkedPointPlot, false); @@ -548,21 +555,26 @@ namespace Elwig.Windows { if (mouseOnData != null) { ChangeMarker(HighlightedPointPlot, true, mouseOnData.Value.X, mouseOnData.Value.Y); - HighlightedPointPlot.IsVisible = true; + HighlightedPointPlot?.IsVisible = true; HoverChanged = true ^ HoverActive; HoverActive = true; - HandleTooltip(mouseOnData.Value.X, mouseOnData.Value.Y, mouseOnData.Value.Index, SelectedGraphEntry!.DataGraph, e.GetPosition(OechslePricePlot), e is MouseWheelEventArgs); + if (SelectedGraphEntry?.DataGraph is Graph g) + HandleTooltip(mouseOnData.Value.X, mouseOnData.Value.Y, mouseOnData.Value.Index, g, e.GetPosition(OechslePricePlot), e is MouseWheelEventArgs); } else if (mouseOnGebunden != null) { ChangeMarker(HighlightedPointPlot, true, mouseOnGebunden.Value.X, mouseOnGebunden.Value.Y); - HighlightedPointPlot.IsVisible = true; + HighlightedPointPlot?.IsVisible = true; HoverChanged = true ^ HoverActive; HoverActive = true; - HandleTooltip(mouseOnGebunden.Value.X, mouseOnGebunden.Value.Y, mouseOnGebunden.Value.Index, SelectedGraphEntry!.GebundenGraph!, e.GetPosition(OechslePricePlot), e is MouseWheelEventArgs); + if (SelectedGraphEntry?.GebundenGraph is Graph g) + HandleTooltip(mouseOnGebunden.Value.X, mouseOnGebunden.Value.Y, mouseOnGebunden.Value.Index, g, e.GetPosition(OechslePricePlot), e is MouseWheelEventArgs); } else { ChangeMarker(HighlightedPointPlot, false); HoverChanged = false ^ HoverActive; HoverActive = false; - OechslePricePlot.Plot.PlottableList.Remove(TooltipPlot); + if (TooltipPlot != null) { + OechslePricePlot.Plot.PlottableList.Remove(TooltipPlot); + TooltipPlot = null; + } OechslePricePlot.Refresh(); } } @@ -577,7 +589,10 @@ namespace Elwig.Windows { private void HandleTooltip(double pointX, double pointY, int pointIndex, Graph g, Point p, bool force) { if (force || LastHighlighted != Highlighted || HoverChanged) { - OechslePricePlot.Plot.PlottableList.Remove(TooltipPlot); + if (TooltipPlot != null) { + OechslePricePlot.Plot.PlottableList.Remove(TooltipPlot); + TooltipPlot = null; + } if (TooltipInput.IsChecked == true) { Coordinates mouseLocation = GetMouseCoordinates(new(p.X, p.Y - 30)); TooltipPlot = OechslePricePlot.Plot.Add.Text($"Oechsle: {pointX:N2}, Preis: {Math.Round(pointY, Season.Precision)} {CurrencySymbol}/kg", mouseLocation.X, mouseLocation.Y); @@ -676,7 +691,7 @@ namespace Elwig.Windows { } } - private void DisableTextBox(TextBox u) { + private static void DisableTextBox(TextBox u) { u.IsEnabled = false; u.IsReadOnly = true; } @@ -707,10 +722,8 @@ namespace Elwig.Windows { ActiveGraph = g; } - private void ChangeLineWidth(Scatter? p, double lineWidth) { - if (p != null) { - p.LineWidth = (float)lineWidth; - } + private static void ChangeLineWidth(Scatter? p, double lineWidth) { + p?.LineWidth = (float)lineWidth; } private void GraphList_SelectionChanged(object sender, SelectionChangedEventArgs e) { @@ -752,7 +765,7 @@ namespace Elwig.Windows { } } - SelectedGraphEntry!.Vaributes = VaributeInput.SelectedItems.Cast().ToList(); + SelectedGraphEntry?.Vaributes = [.. VaributeInput.SelectedItems.Cast()]; SetHasChanged(); GraphList.Items.Refresh(); VaributeInput.Items.Refresh();