diff --git a/Darling/Darling.Tests/ChartDisplayScaleTests.cs b/Darling/Darling.Tests/ChartDisplayScaleTests.cs new file mode 100644 index 0000000000..a29b53dd2d --- /dev/null +++ b/Darling/Darling.Tests/ChartDisplayScaleTests.cs @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2026 Erik Darling, Darling Data LLC + * + * This file is part of the SQL Server Performance Monitor. + * + * Licensed under the MIT License. See LICENSE file in the project root for full license information. + */ + +using System; +using System.Globalization; +using System.Linq; +using PerformanceMonitor.Ui; +using Xunit; + +namespace Darling.Tests; + +/// +/// #6678: chart text drew at 40% of its size at 250% Windows scaling because nothing set the plot's scale from the window's DPI. +/// is that rule. Each fact drives a real chart: the plot's scale follows the DPI scale it is given +/// (also after a DPI change, and after Reset() swaps in a fresh plot), the plot then lays out in scaled units so a 13 point +/// label is 13 units of a figure 1/2.5 the pixel size, and a mouse pixel handed to the hover still finds the point under it. +/// +public sealed class ChartDisplayScaleTests +{ + [Fact] + public void The_plot_draws_at_the_DPI_scale_and_follows_a_DPI_change() => + StaTestThread.Run(() => + { + var chart = new ScottPlot.WPF.WpfPlot(); + + Assert.True(ChartDisplayScale.Apply(chart, 2.5)); + Assert.Equal(2.5, chart.Plot.ScaleFactor, 3); + Assert.Equal(2.5f, chart.DisplayScale); + + Assert.False(ChartDisplayScale.Apply(chart, 2.5)); // nothing changed, nothing to refresh + + Assert.True(ChartDisplayScale.Apply(chart, 1.0)); // moved to a 100% monitor + Assert.Equal(1.0, chart.Plot.ScaleFactor, 3); + Assert.Equal(1.0f, chart.DisplayScale); + }); + + [Theory] + [InlineData(0.0)] + [InlineData(-1.0)] + [InlineData(double.NaN)] + public void An_unknown_DPI_draws_at_100_percent(double dpiScale) => + Assert.Equal(1f, ChartDisplayScale.ScaleFor(dpiScale)); + + [Fact] + public void A_reset_plot_is_scaled_again_by_Apply() => + StaTestThread.Run(() => + { + var chart = new ScottPlot.WPF.WpfPlot(); + ChartDisplayScale.Apply(chart, 2.5); + + chart.Reset(); // ClearChart does this on every refresh: a new plot is at scale 1 + Assert.Equal(1.0, chart.Plot.ScaleFactor, 3); + + Assert.True(ChartDisplayScale.Apply(chart, 2.5)); + Assert.Equal(2.5, chart.Plot.ScaleFactor, 3); + }); + + [Fact] + public void At_250_percent_the_plot_lays_out_in_units_2_point_5_times_smaller_than_its_pixels() => + StaTestThread.Run(() => + { + var chart = new ScottPlot.WPF.WpfPlot(); + chart.Plot.Add.Scatter(new double[] { 0, 1, 2 }, new double[] { 0, 1, 2 }); + + var rect = default(ScottPlot.PixelRect); + chart.Plot.RenderManager.RenderStarting += (_, rp) => rect = rp.ScaledFigureRect; + + using (chart.Plot.GetImage(1000, 500)) { } + Assert.Equal(1000f, rect.Width, 1); + + ChartDisplayScale.Apply(chart, 2.5); + using (chart.Plot.GetImage(1000, 500)) { } + Assert.Equal(400f, rect.Width, 1); + Assert.Equal(200f, rect.Height, 1); // ChartAxisTitle fits the title to this height, so it fits at every scaling + }); + + [Theory] + [InlineData(1.0)] + [InlineData(2.5)] + public void The_hover_finds_the_point_under_a_device_pixel_at_every_scaling(double dpiScale) => + StaTestThread.Run(() => + { + var chart = new ScottPlot.WPF.WpfPlot(); + var hover = new ChartHoverHelper(chart, "ms"); + var xs = Enumerable.Range(0, 10).Select(i => new DateTime(2026, 10, 1).AddMinutes(i * 10).ToOADate()).ToArray(); + var ys = xs.Select((_, i) => 1000.0 + i * 111).ToArray(); + hover.Add(chart.Plot.Add.Scatter(xs, ys), "Series"); + ChartDisplayScale.Apply(chart, dpiScale); + chart.Plot.Axes.AutoScale(); + using (chart.Plot.GetImage(1000, 500)) { } + + for (var i = 0; i < xs.Length; i++) + { + var text = hover.HoverTextAt(chart.Plot.GetPixel(new ScottPlot.Coordinates(xs[i], ys[i]))); + Assert.NotNull(text); + Assert.Contains((1000 + i * 111).ToString("N0", CultureInfo.CurrentCulture), text); + } + }); +} diff --git a/Darling/PerformanceMonitor.Darling.Viewer/App.xaml.cs b/Darling/PerformanceMonitor.Darling.Viewer/App.xaml.cs index 5c779b5ab1..d91f965617 100644 --- a/Darling/PerformanceMonitor.Darling.Viewer/App.xaml.cs +++ b/Darling/PerformanceMonitor.Darling.Viewer/App.xaml.cs @@ -150,6 +150,9 @@ older one closes it and takes over (Velopack upgrade). The viewer holds no exclu /* F17: tabs and column headers made of panels get an accessible name equal to their visible text. */ PerformanceMonitor.Ui.AccessibleNames.Register(); + /* #6678: chart text is drawn at the window's DPI scale, not at 100% of device pixels. */ + PerformanceMonitor.Ui.ChartDisplayScale.Register(); + base.OnStartup(e); // Right-click selects the DataGrid row under the cursor app-wide, so context-menu actions diff --git a/Darling/PerformanceMonitor.Darling.Viewer/CorrelatedTimelineLanesControl.xaml.cs b/Darling/PerformanceMonitor.Darling.Viewer/CorrelatedTimelineLanesControl.xaml.cs index 4aba558caa..6e306f6535 100644 --- a/Darling/PerformanceMonitor.Darling.Viewer/CorrelatedTimelineLanesControl.xaml.cs +++ b/Darling/PerformanceMonitor.Darling.Viewer/CorrelatedTimelineLanesControl.xaml.cs @@ -626,6 +626,7 @@ any axis floor with it. */ private static void ClearChart(ScottPlot.WPF.WpfPlot chart) { chart.Reset(); + PerformanceMonitor.Ui.ChartDisplayScale.Apply(chart); // #6678: Reset swaps in a plot at scale 1 chart.Plot.Clear(); } diff --git a/Lite.Tests/ChartDisplayScaleTests.cs b/Lite.Tests/ChartDisplayScaleTests.cs new file mode 100644 index 0000000000..7007fc7472 --- /dev/null +++ b/Lite.Tests/ChartDisplayScaleTests.cs @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2026 Erik Darling, Darling Data LLC + * + * This file is part of the SQL Server Performance Monitor. + * + * Licensed under the MIT License. See LICENSE file in the project root for full license information. + */ + +using System; +using System.Globalization; +using System.Linq; +using PerformanceMonitor.Ui; +using Darling.Tests; +using Xunit; + +namespace PerformanceMonitorLite.Tests; + +/// +/// #6678: chart text drew at 40% of its size at 250% Windows scaling because nothing set the plot's scale from the window's DPI. +/// is that rule. Each fact drives a real chart: the plot's scale follows the DPI scale it is given +/// (also after a DPI change, and after Reset() swaps in a fresh plot), the plot then lays out in scaled units so a 13 point +/// label is 13 units of a figure 1/2.5 the pixel size, and a mouse pixel handed to the hover still finds the point under it. +/// +public sealed class ChartDisplayScaleTests +{ + [Fact] + public void The_plot_draws_at_the_DPI_scale_and_follows_a_DPI_change() => + StaTestThread.Run(() => + { + var chart = new ScottPlot.WPF.WpfPlot(); + + Assert.True(ChartDisplayScale.Apply(chart, 2.5)); + Assert.Equal(2.5, chart.Plot.ScaleFactor, 3); + Assert.Equal(2.5f, chart.DisplayScale); + + Assert.False(ChartDisplayScale.Apply(chart, 2.5)); // nothing changed, nothing to refresh + + Assert.True(ChartDisplayScale.Apply(chart, 1.0)); // moved to a 100% monitor + Assert.Equal(1.0, chart.Plot.ScaleFactor, 3); + Assert.Equal(1.0f, chart.DisplayScale); + }); + + [Theory] + [InlineData(0.0)] + [InlineData(-1.0)] + [InlineData(double.NaN)] + public void An_unknown_DPI_draws_at_100_percent(double dpiScale) => + Assert.Equal(1f, ChartDisplayScale.ScaleFor(dpiScale)); + + [Fact] + public void A_reset_plot_is_scaled_again_by_Apply() => + StaTestThread.Run(() => + { + var chart = new ScottPlot.WPF.WpfPlot(); + ChartDisplayScale.Apply(chart, 2.5); + + chart.Reset(); // ClearChart does this on every refresh: a new plot is at scale 1 + Assert.Equal(1.0, chart.Plot.ScaleFactor, 3); + + Assert.True(ChartDisplayScale.Apply(chart, 2.5)); + Assert.Equal(2.5, chart.Plot.ScaleFactor, 3); + }); + + [Fact] + public void At_250_percent_the_plot_lays_out_in_units_2_point_5_times_smaller_than_its_pixels() => + StaTestThread.Run(() => + { + var chart = new ScottPlot.WPF.WpfPlot(); + chart.Plot.Add.Scatter(new double[] { 0, 1, 2 }, new double[] { 0, 1, 2 }); + + var rect = default(ScottPlot.PixelRect); + chart.Plot.RenderManager.RenderStarting += (_, rp) => rect = rp.ScaledFigureRect; + + using (chart.Plot.GetImage(1000, 500)) { } + Assert.Equal(1000f, rect.Width, 1); + + ChartDisplayScale.Apply(chart, 2.5); + using (chart.Plot.GetImage(1000, 500)) { } + Assert.Equal(400f, rect.Width, 1); + Assert.Equal(200f, rect.Height, 1); // ChartAxisTitle fits the title to this height, so it fits at every scaling + }); + + [Theory] + [InlineData(1.0)] + [InlineData(2.5)] + public void The_hover_finds_the_point_under_a_device_pixel_at_every_scaling(double dpiScale) => + StaTestThread.Run(() => + { + var chart = new ScottPlot.WPF.WpfPlot(); + var hover = new ChartHoverHelper(chart, "ms"); + var xs = Enumerable.Range(0, 10).Select(i => new DateTime(2026, 10, 1).AddMinutes(i * 10).ToOADate()).ToArray(); + var ys = xs.Select((_, i) => 1000.0 + i * 111).ToArray(); + hover.Add(chart.Plot.Add.Scatter(xs, ys), "Series"); + ChartDisplayScale.Apply(chart, dpiScale); + chart.Plot.Axes.AutoScale(); + using (chart.Plot.GetImage(1000, 500)) { } + + for (var i = 0; i < xs.Length; i++) + { + var text = hover.HoverTextAt(chart.Plot.GetPixel(new ScottPlot.Coordinates(xs[i], ys[i]))); + Assert.NotNull(text); + Assert.Contains((1000 + i * 111).ToString("N0", CultureInfo.CurrentCulture), text); + } + }); +} diff --git a/Lite/App.xaml.cs b/Lite/App.xaml.cs index 0854dc776c..f715e14e05 100644 --- a/Lite/App.xaml.cs +++ b/Lite/App.xaml.cs @@ -604,6 +604,9 @@ an older-but-elevated one raises an actionable error. */ /* F17: tabs and column headers made of panels get an accessible name equal to their visible text. */ PerformanceMonitor.Ui.AccessibleNames.Register(); + /* #6678: chart text is drawn at the window's DPI scale, not at 100% of device pixels. */ + PerformanceMonitor.Ui.ChartDisplayScale.Register(); + base.OnStartup(e); /* #5320 N1: the statement filter's judge takes 250-900 ms to build, and the first connection or AG alert diff --git a/Lite/Controls/CorrelatedTimelineLanesControl.xaml.cs b/Lite/Controls/CorrelatedTimelineLanesControl.xaml.cs index 3e2e5699ed..10d264d2e9 100644 --- a/Lite/Controls/CorrelatedTimelineLanesControl.xaml.cs +++ b/Lite/Controls/CorrelatedTimelineLanesControl.xaml.cs @@ -764,6 +764,7 @@ internal static string ComparisonLabel(int days) private static void ClearChart(ScottPlot.WPF.WpfPlot chart) { chart.Reset(); + PerformanceMonitor.Ui.ChartDisplayScale.Apply(chart); // #6678: Reset swaps in a plot at scale 1 chart.Plot.Clear(); } diff --git a/PerformanceMonitor.Ui/ChartAxisTitle.cs b/PerformanceMonitor.Ui/ChartAxisTitle.cs index bc7f146a52..e41d5e3f0b 100644 --- a/PerformanceMonitor.Ui/ChartAxisTitle.cs +++ b/PerformanceMonitor.Ui/ChartAxisTitle.cs @@ -24,8 +24,8 @@ public static class ChartAxisTitle public const double BaseFontSize = 12; public const double MinFontSize = 9; - /// Pixels of the figure height the bottom tick labels, bottom title and padding take. - public const double ReservedHeight = 60; + /// Scaled units (DIPs) of the figure height the bottom tick labels (two lines for a date), legend and padding take; measured at 250% scaling once the text drew at its real size (#6678). Over-reserving only trims a title earlier. + public const double ReservedHeight = 112; /// Average glyph width as a fraction of the font size (sans-serif, mixed case and digits). private const double GlyphWidth = 0.6; @@ -91,7 +91,7 @@ public static void Attach(WpfPlot chart) public static class ChartLayout { /// Smallest height a chart row may shrink to; below it the tab scrolls instead (a shorter plot cannot draw its ticks). - public const double MinPlotHeight = 150; + public const double MinPlotHeight = 200; /// /// A chart inside a vertical keeps the mouse wheel for its own diff --git a/PerformanceMonitor.Ui/ChartDisplayScale.cs b/PerformanceMonitor.Ui/ChartDisplayScale.cs new file mode 100644 index 0000000000..108a20e5f4 --- /dev/null +++ b/PerformanceMonitor.Ui/ChartDisplayScale.cs @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2026 Erik Darling, Darling Data LLC + * + * This file is part of the SQL Server Performance Monitor. + * + * Licensed under the MIT License. See LICENSE file in the project root for full license information. + */ + +using System; +using System.Collections.Generic; +using System.Runtime.CompilerServices; +using System.Windows; +using System.Windows.Media; +using ScottPlot.WPF; + +namespace PerformanceMonitor.Ui +{ + /// + /// #6678: at 250% Windows scaling the chart text was about 40% of its size. ScottPlot.WPF paints into a device-pixel + /// canvas and takes every font size as device pixels, and nothing set from the + /// window's DPI. (The control's own DisplayScale is read once, in its constructor, before it has a window, and + /// ScottPlot never feeds it into the render.) is the rule: the plot's scale is the + /// control's DPI scale, so a 13 point tick label is the same size next to the window's own text at every scaling. + /// applies it to every chart in the process when the chart is first laid out, and again when its + /// window moves to a monitor with another DPI. + /// + /// + /// Hit-testing keeps working because every caller already hands Plot.GetCoordinates a DEVICE pixel (the mouse + /// position times GetDpi), and GetCoordinates divides it by ScaleFactor itself. A caller that + /// compares a pixel with the plot's LAYOUT rectangles (the legend panel) must divide by the scale first: layout is in + /// scaled units, see . + /// + public static class ChartDisplayScale + { + private static bool s_registered; + + /// The scale to draw at for a DPI scale: itself, or 1 when the DPI is unknown (zero, negative, NaN). + public static float ScaleFor(double dpiScale) => + dpiScale > 0 && double.IsFinite(dpiScale) ? (float)dpiScale : 1f; + + /// + /// Makes the chart's plot draw at . Returns true when the plot's scale changed, so the + /// caller knows to refresh. Safe to call again after WpfPlot.Reset(), which swaps in a plot at scale 1. + /// + public static bool Apply(WpfPlot chart, double dpiScale) + { + var scale = ScaleFor(dpiScale); + chart.DisplayScale = scale; + if (Math.Abs(chart.Plot.ScaleFactor - scale) < 0.0001) return false; + chart.Plot.ScaleFactor = scale; + return true; + } + + /// Applies the DPI scale of the chart's own window; for a chart that is already in the visual tree. + public static bool Apply(WpfPlot chart) => Apply(chart, VisualTreeHelper.GetDpi(chart).DpiScaleX); + + /// + /// Registers the handlers once for the process. Call it from the application's startup. SizeChanged, not Loaded, for + /// the reason gives: WPF raises Loaded straight to an instance's own handlers. + /// + public static void Register() + { + if (s_registered) return; + s_registered = true; + EventManager.RegisterClassHandler(typeof(WpfPlot), FrameworkElement.SizeChangedEvent, new RoutedEventHandler(OnSized)); + } + + /// The charts of one window, held weakly, so a DPI change reaches the ones on tabs that are not showing. + private sealed class WindowCharts + { + public readonly List> Charts = []; + } + + private static readonly ConditionalWeakTable s_windows = new(); + + private static void OnSized(object sender, RoutedEventArgs e) + { + if (sender is not WpfPlot chart) return; + if (Apply(chart)) chart.Refresh(); + Track(chart); + } + + private static void Track(WpfPlot chart) + { + var window = Window.GetWindow(chart); + if (window is null) return; + + if (!s_windows.TryGetValue(window, out var known)) + { + known = new WindowCharts(); + s_windows.Add(window, known); + window.DpiChanged += (_, args) => OnDpiChanged(known, args.NewDpi.DpiScaleX); + } + + known.Charts.RemoveAll(w => !w.TryGetTarget(out _)); + foreach (var w in known.Charts) + { + if (w.TryGetTarget(out var seen) && ReferenceEquals(seen, chart)) return; + } + + known.Charts.Add(new WeakReference(chart)); + } + + private static void OnDpiChanged(WindowCharts known, double dpiScale) + { + foreach (var w in known.Charts.ToArray()) + { + if (w.TryGetTarget(out var chart) && Apply(chart, dpiScale)) chart.Refresh(); + } + } + } +} diff --git a/PerformanceMonitor.Ui/ChartHoverHelper.cs b/PerformanceMonitor.Ui/ChartHoverHelper.cs index a3fe55df6b..5329746392 100644 --- a/PerformanceMonitor.Ui/ChartHoverHelper.cs +++ b/PerformanceMonitor.Ui/ChartHoverHelper.cs @@ -502,6 +502,9 @@ private bool TryLegendHitTest(ScottPlot.Pixel clickPixel, out ScottPlot.IPlottab var lr = _chart.Plot.LastRender; if (lr.Count == 0) return false; // not yet rendered + + /* #6678: the legend panel's layout is in the plot's scaled units, the click is in device pixels. */ + clickPixel = clickPixel.Divide((float)_chart.Plot.ScaleFactor); var layout = lr.Layout; var panel = layout.PanelSizes.Keys.OfType().FirstOrDefault(); diff --git a/PerformanceMonitor.Ui/ChartRenderHelper.cs b/PerformanceMonitor.Ui/ChartRenderHelper.cs index b80808ff76..13e4cb21d0 100644 --- a/PerformanceMonitor.Ui/ChartRenderHelper.cs +++ b/PerformanceMonitor.Ui/ChartRenderHelper.cs @@ -34,6 +34,7 @@ public void ClearChart(ScottPlot.WPF.WpfPlot chart) and DateTimeTicksBottom() replaces the axis object entirely. Resetting the plot object avoids tick generator type mismatches. */ chart.Reset(); + ChartDisplayScale.Apply(chart); // #6678: Reset swaps in a plot at scale 1 chart.Plot.Clear(); }