[#33] ChartWindow: Fix scaling bug

This commit is contained in:
2024-08-28 10:20:21 +02:00
parent 8ce8492c74
commit 0f24f9da08
2 changed files with 52 additions and 53 deletions

View File

@ -5,13 +5,13 @@ using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Elwig.Controls;
using Elwig.Helpers;
using Elwig.Helpers.Billing;
using Elwig.Models.Entities;
using ScottPlot.Plottables;
using ScottPlot;
using ScottPlot.Control;
using Microsoft.EntityFrameworkCore;
namespace Elwig.Windows {
public partial class ChartWindow : ContextWindow {
@ -22,6 +22,7 @@ namespace Elwig.Windows {
public readonly int Year;
public readonly int AvNr;
public Season Season;
public string CurrencySymbol;
private PaymentVar PaymentVar;
private bool HasChanged = false;
@ -72,6 +73,9 @@ namespace Elwig.Windows {
AvNr = avnr;
using var ctx = new AppDbContext();
Season = ctx.Seasons.Find(year) ?? throw new ArgumentException("Season not found");
CurrencySymbol = Season.Currency.Symbol ?? Season.Currency.Code;
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";
LockContext = true;
@ -99,6 +103,9 @@ namespace Elwig.Windows {
private async Task RefreshGraphList(AppDbContext ctx) {
PaymentVar = await ctx.PaymentVariants.FindAsync(Year, AvNr) ?? throw new ArgumentException("PaymentVar not found");
Season = await ctx.Seasons.FindAsync(Year) ?? throw new ArgumentException("Season not found");
CurrencySymbol = Season.Currency.Symbol ?? Season.Currency.Code;
PriceInput.Unit = $"{CurrencySymbol}/kg";
GebundenFlatBonus.Unit = $"{CurrencySymbol}/kg";
try {
var data = EditBillingData.FromJson(PaymentVar.Data, Utils.GetVaributes(ctx, Year));
@ -156,15 +163,15 @@ namespace Elwig.Windows {
} else {
CopyButton.IsEnabled = false;
DeleteButton.IsEnabled = false;
DisableUnitTextBox(OechsleInput);
DisableTextBox(OechsleInput);
DisableOptionButtons();
}
if (!PaymentVar.TestVariant) {
AddButton.IsEnabled = false;
CopyButton.IsEnabled = false;
DeleteButton.IsEnabled = false;
DisableUnitTextBox(OechsleInput);
DisableUnitTextBox(PriceInput);
DisableTextBox(OechsleInput);
DisableTextBox(PriceInput);
GebundenTypeFixed.IsEnabled = false;
GebundenTypeGraph.IsEnabled = false;
GebundenTypeNone.IsEnabled = false;
@ -407,7 +414,7 @@ namespace Elwig.Windows {
EnableActionButtons();
OechslePricePlot.Refresh();
EnableUnitTextBox(PriceInput);
EnableTextBox(PriceInput);
return;
}
}
@ -416,9 +423,9 @@ namespace Elwig.Windows {
ChangeMarker(PrimaryMarkedPointPlot, false);
DisableActionButtons();
PriceInput.Text = "";
DisableUnitTextBox(PriceInput);
DisableTextBox(PriceInput);
OechslePricePlot.Refresh();
DisableUnitTextBox(PriceInput);
DisableTextBox(PriceInput);
}
private void PriceInput_TextChanged(object sender, TextChangedEventArgs evt) {
@ -517,7 +524,7 @@ namespace Elwig.Windows {
OechsleInput.Text = "";
PriceInput.Text = "";
DisableUnitTextBox(PriceInput);
DisableTextBox(PriceInput);
DisableActionButtons();
}
@ -531,28 +538,39 @@ namespace Elwig.Windows {
MouseChange(e);
}
private Coordinates GetMouseCoordinates(Point p) {
Pixel px = new(p.X, p.Y);
var source = PresentationSource.FromVisual(this);
if (source?.CompositionTarget != null) {
var matrix = source.CompositionTarget.TransformToDevice;
px.X *= (float)matrix.M11;
px.Y *= (float)matrix.M22;
}
return OechslePricePlot.Plot.GetCoordinates(px);
}
private void MouseChange(MouseEventArgs e) {
if (GraphList.SelectedItem == null) {
return;
}
(double x, double y, int index)? mouseOnData = MouseOnPlot(DataPlot, e.GetPosition(OechslePricePlot));
(double x, double y, int index)? mouseOnGebunden = MouseOnPlot(GebundenPlot, e.GetPosition(OechslePricePlot));
var mouseOnData = MouseOnPlot(DataPlot, GetMouseCoordinates(e.GetPosition(OechslePricePlot)));
var mouseOnGebunden = MouseOnPlot(GebundenPlot, GetMouseCoordinates(e.GetPosition(OechslePricePlot)));
Highlighted = LastHighlighted;
if (mouseOnData != null) {
ChangeMarker(HighlightedPointPlot, true, mouseOnData.Value.x, mouseOnData.Value.y);
ChangeMarker(HighlightedPointPlot, true, mouseOnData.Value.X, mouseOnData.Value.Y);
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);
HandleTooltip(mouseOnData.Value.X, mouseOnData.Value.Y, mouseOnData.Value.Index, SelectedGraphEntry!.DataGraph, e.GetPosition(OechslePricePlot), e is MouseWheelEventArgs);
} else if (mouseOnGebunden != null) {
ChangeMarker(HighlightedPointPlot, true, mouseOnGebunden.Value.x, mouseOnGebunden.Value.y);
ChangeMarker(HighlightedPointPlot, true, mouseOnGebunden.Value.X, mouseOnGebunden.Value.Y);
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);
HandleTooltip(mouseOnGebunden.Value.X, mouseOnGebunden.Value.Y, mouseOnGebunden.Value.Index, SelectedGraphEntry!.GebundenGraph!, e.GetPosition(OechslePricePlot), e is MouseWheelEventArgs);
} else {
ChangeMarker(HighlightedPointPlot, false);
HoverChanged = false ^ HoverActive;
@ -562,30 +580,20 @@ namespace Elwig.Windows {
}
}
private (double, double, int)? MouseOnPlot(Scatter? plot, Point p) {
if (plot == null) {
private (double X, double Y, int Index)? MouseOnPlot(Scatter? plot, Coordinates c) {
if (plot == null)
return null;
}
OechslePricePlot.Refresh();
Pixel mousePixel = new(p.X, p.Y);
Coordinates mouseLocation = OechslePricePlot.Plot.GetCoordinates(mousePixel);
DataPoint nearestPoint = plot.Data.GetNearest(mouseLocation, OechslePricePlot.Plot.LastRender, 3);
if (nearestPoint.IsReal) {
return (nearestPoint.X, nearestPoint.Y, nearestPoint.Index);
} else {
return null;
}
DataPoint nearestPoint = plot.Data.GetNearest(c, OechslePricePlot.Plot.LastRender, 5);
return nearestPoint.IsReal ? (nearestPoint.X, nearestPoint.Y, nearestPoint.Index) : null;
}
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 (TooltipInput.IsChecked == true) {
Pixel mousePixel = new(p.X, p.Y - 30);
Coordinates mouseLocation = OechslePricePlot.Plot.GetCoordinates(mousePixel);
TooltipPlot = OechslePricePlot.Plot.Add.Text($"Oechsle: {pointX:N2}, Preis: {Math.Round(pointY, Season.Precision)}€/kg", mouseLocation.X, mouseLocation.Y);
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);
TooltipPlot.LabelFontSize = 12;
TooltipPlot.LabelBold = true;
TooltipPlot.LabelBorderColor = Colors.Black;
@ -658,30 +666,30 @@ namespace Elwig.Windows {
SetHasChanged(false);
}
private void EnableUnitTextBox(UnitTextBox u) {
private void EnableTextBox(TextBox u) {
if (PaymentVar.TestVariant) {
u.IsEnabled = true;
u.IsReadOnly = false;
}
}
private void DisableUnitTextBox(UnitTextBox u) {
private void DisableTextBox(TextBox u) {
u.IsEnabled = false;
u.IsReadOnly = true;
}
private void ChangeActiveGraph(Graph? g) {
if (g != null && g == SelectedGraphEntry?.DataGraph) {
EnableUnitTextBox(OechsleInput);
EnableTextBox(OechsleInput);
if (SelectedGraphEntry?.GebundenGraph != null) ChangeLineWidth(DataPlot, 4);
ChangeLineWidth(GebundenPlot, 1);
} else if (g != null && g == SelectedGraphEntry?.GebundenGraph) {
EnableUnitTextBox(OechsleInput);
EnableTextBox(OechsleInput);
ChangeLineWidth(GebundenPlot, 4);
ChangeLineWidth(DataPlot, 1);
} else {
DisableUnitTextBox(OechsleInput);
DisableUnitTextBox(PriceInput);
DisableTextBox(OechsleInput);
DisableTextBox(PriceInput);
OechsleInput.Text = "";
PriceInput.Text = "";
ChangeLineWidth(DataPlot, 1);
@ -700,14 +708,6 @@ namespace Elwig.Windows {
RefreshInputs();
}
private void PriceInput_LostFocus(object sender, RoutedEventArgs e) {
}
private void OechsleInput_LostFocus(object sender, RoutedEventArgs e) {
}
private void GebundenFlatBonus_TextChanged(object sender, TextChangedEventArgs e) {
if (FillingInputs) return;
var r = Validator.CheckDecimal(GebundenFlatBonus, true, 2, Season.Precision);
@ -778,19 +778,19 @@ namespace Elwig.Windows {
private void GebundenType_Checked(object sender, RoutedEventArgs e) {
if (FillingInputs) return;
if (SelectedGraphEntry == null) {
DisableUnitTextBox(GebundenFlatBonus);
DisableTextBox(GebundenFlatBonus);
return;
}
if (GebundenTypeNone.IsChecked == true) {
SelectedGraphEntry.RemoveGebundenGraph();
DisableUnitTextBox(GebundenFlatBonus);
DisableTextBox(GebundenFlatBonus);
} else if (GebundenTypeFixed.IsChecked == true) {
SelectedGraphEntry.GebundenFlatBonus = double.TryParse(GebundenFlatBonus.Text, out var val) ? val : 0.1;
SelectedGraphEntry.AddGebundenGraph();
EnableUnitTextBox(GebundenFlatBonus);
EnableTextBox(GebundenFlatBonus);
} else if (GebundenTypeGraph.IsChecked == true) {
SelectedGraphEntry.AddGebundenGraph();
DisableUnitTextBox(GebundenFlatBonus);
DisableTextBox(GebundenFlatBonus);
}
SetHasChanged();
RefreshInputs();
@ -801,11 +801,11 @@ namespace Elwig.Windows {
if (SelectedGraphEntry?.GebundenFlatBonus is double bonus) {
GebundenTypeFixed.IsChecked = true;
GebundenFlatBonus.Text = $"{bonus}";
EnableUnitTextBox(GebundenFlatBonus);
EnableTextBox(GebundenFlatBonus);
} else if (SelectedGraphEntry?.GebundenGraph != null) {
GebundenTypeGraph.IsChecked = true;
GebundenFlatBonus.Text = "";
DisableUnitTextBox(GebundenFlatBonus);
DisableTextBox(GebundenFlatBonus);
}
FillingInputs = false;
}