Skip to content
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
104 changes: 104 additions & 0 deletions Darling/Darling.Tests/ChartDisplayScaleTests.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// #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.
/// <see cref="ChartDisplayScale"/> 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 <c>Reset()</c> 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.
/// </summary>
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);
}
});
}
3 changes: 3 additions & 0 deletions Darling/PerformanceMonitor.Darling.Viewer/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
105 changes: 105 additions & 0 deletions Lite.Tests/ChartDisplayScaleTests.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// #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.
/// <see cref="ChartDisplayScale"/> 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 <c>Reset()</c> 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.
/// </summary>
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);
}
});
}
3 changes: 3 additions & 0 deletions Lite/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Lite/Controls/CorrelatedTimelineLanesControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
6 changes: 3 additions & 3 deletions PerformanceMonitor.Ui/ChartAxisTitle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public static class ChartAxisTitle
public const double BaseFontSize = 12;
public const double MinFontSize = 9;

/// <summary>Pixels of the figure height the bottom tick labels, bottom title and padding take.</summary>
public const double ReservedHeight = 60;
/// <summary>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.</summary>
public const double ReservedHeight = 112;

/// <summary>Average glyph width as a fraction of the font size (sans-serif, mixed case and digits).</summary>
private const double GlyphWidth = 0.6;
Expand Down Expand Up @@ -91,7 +91,7 @@ public static void Attach(WpfPlot chart)
public static class ChartLayout
{
/// <summary>Smallest height a chart row may shrink to; below it the tab scrolls instead (a shorter plot cannot draw its ticks).</summary>
public const double MinPlotHeight = 150;
public const double MinPlotHeight = 200;

/// <summary>
/// A chart inside a vertical <see cref="System.Windows.Controls.ScrollViewer"/> keeps the mouse wheel for its own
Expand Down
112 changes: 112 additions & 0 deletions PerformanceMonitor.Ui/ChartDisplayScale.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// #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 <see cref="ScottPlot.Plot.ScaleFactor"/> from the
/// window's DPI. (The control's own <c>DisplayScale</c> is read once, in its constructor, before it has a window, and
/// ScottPlot never feeds it into the render.) <see cref="Apply(WpfPlot, double)"/> 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.
/// <see cref="Register"/> 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.
/// </summary>
/// <remarks>
/// Hit-testing keeps working because every caller already hands <c>Plot.GetCoordinates</c> a DEVICE pixel (the mouse
/// position times <c>GetDpi</c>), and <c>GetCoordinates</c> divides it by <c>ScaleFactor</c> 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 <see cref="ChartHoverHelper"/>.
/// </remarks>
public static class ChartDisplayScale
{
private static bool s_registered;

/// <summary>The scale to draw at for a DPI scale: itself, or 1 when the DPI is unknown (zero, negative, NaN).</summary>
public static float ScaleFor(double dpiScale) =>
dpiScale > 0 && double.IsFinite(dpiScale) ? (float)dpiScale : 1f;

/// <summary>
/// Makes the chart's plot draw at <paramref name="dpiScale"/>. Returns true when the plot's scale changed, so the
/// caller knows to refresh. Safe to call again after <c>WpfPlot.Reset()</c>, which swaps in a plot at scale 1.
/// </summary>
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;
}

/// <summary>Applies the DPI scale of the chart's own window; for a chart that is already in the visual tree.</summary>
public static bool Apply(WpfPlot chart) => Apply(chart, VisualTreeHelper.GetDpi(chart).DpiScaleX);

/// <summary>
/// Registers the handlers once for the process. Call it from the application's startup. SizeChanged, not Loaded, for
/// the reason <see cref="AccessibleNames.Register"/> gives: WPF raises Loaded straight to an instance's own handlers.
/// </summary>
public static void Register()
{
if (s_registered) return;
s_registered = true;
EventManager.RegisterClassHandler(typeof(WpfPlot), FrameworkElement.SizeChangedEvent, new RoutedEventHandler(OnSized));
}

/// <summary>The charts of one window, held weakly, so a DPI change reaches the ones on tabs that are not showing.</summary>
private sealed class WindowCharts
{
public readonly List<WeakReference<WpfPlot>> Charts = [];
}

private static readonly ConditionalWeakTable<Window, WindowCharts> 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<WpfPlot>(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();
}
}
}
}
3 changes: 3 additions & 0 deletions PerformanceMonitor.Ui/ChartHoverHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ScottPlot.Panels.LegendPanel>().FirstOrDefault();
Expand Down
1 change: 1 addition & 0 deletions PerformanceMonitor.Ui/ChartRenderHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Loading