From 4f481a06c37f2ca03fd1e9f301e272a049c14367 Mon Sep 17 00:00:00 2001 From: SuperDooper <37051355+superdooper86@users.noreply.github.com> Date: Fri, 8 May 2026 12:12:55 +0200 Subject: [PATCH] feat(windows): add SparklineControl.cs --- windows/Controls/SparklineControl.cs | 61 ++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 windows/Controls/SparklineControl.cs diff --git a/windows/Controls/SparklineControl.cs b/windows/Controls/SparklineControl.cs new file mode 100644 index 0000000..dadc71b --- /dev/null +++ b/windows/Controls/SparklineControl.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Windows; +using System.Windows.Media; + +namespace ClaudeCheckerWindows.Controls; + +public class SparklineControl : FrameworkElement +{ + public static readonly DependencyProperty DataProperty = + DependencyProperty.Register(nameof(Data), typeof(IList), typeof(SparklineControl), + new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender)); + + public static readonly DependencyProperty LineColorProperty = + DependencyProperty.Register(nameof(LineColor), typeof(Color), typeof(SparklineControl), + new FrameworkPropertyMetadata(Colors.Orange, FrameworkPropertyMetadataOptions.AffectsRender)); + + public IList? Data { get => (IList?)GetValue(DataProperty); set => SetValue(DataProperty, value); } + public Color LineColor { get => (Color)GetValue(LineColorProperty); set => SetValue(LineColorProperty, value); } + + protected override void OnRender(DrawingContext dc) + { + var data = Data; + if (data == null || data.Count < 1) return; + + var plot = data.Count == 1 ? new[] { data[0], data[0] } : [.. data]; + + var w = RenderSize.Width; + var h = RenderSize.Height; + var minV = double.MaxValue; + var maxV = double.MinValue; + + foreach (var v in plot) { minV = Math.Min(minV, v); maxV = Math.Max(maxV, v); } + var range = Math.Max(maxV - minV, 1); + + Point Pt(int i) => new( + i / (double)(plot.Count - 1) * w, + h - (plot[i] - minV) / range * (h - 6) - 3); + + // Fill + var fill = new StreamGeometry(); + using (var ctx = fill.Open()) + { + ctx.BeginFigure(new Point(0, h), true, true); + for (var i = 0; i < plot.Count; i++) ctx.LineTo(Pt(i), true, false); + ctx.LineTo(new Point(w, h), true, false); + } + fill.Freeze(); + dc.DrawGeometry(new SolidColorBrush(Color.FromArgb(26, LineColor.R, LineColor.G, LineColor.B)), null, fill); + + // Line + var line = new StreamGeometry(); + using (var ctx = line.Open()) + { + ctx.BeginFigure(Pt(0), false, false); + for (var i = 1; i < plot.Count; i++) ctx.LineTo(Pt(i), true, false); + } + line.Freeze(); + dc.DrawGeometry(null, new Pen(new SolidColorBrush(LineColor), 1.5) { LineJoin = PenLineJoin.Round }, line); + } +}