Compare commits

..
2 changed files with 26 additions and 33 deletions
+16 -23
View File
@@ -1,3 +1,6 @@
using Brush = System.Windows.Media.Brush;
using Pen = System.Windows.Media.Pen;
using Point = System.Windows.Point;
using System;
using System.Globalization;
using System.Windows;
@@ -18,7 +21,6 @@ public class GaugeControl : FrameworkElement
public double Percent { get => (double)GetValue(PercentProperty); set => SetValue(PercentProperty, value); }
public Brush Accent { get => (Brush)GetValue(AccentProperty); set => SetValue(AccentProperty, value); }
// Arc sweeps 270° starting at 135° (bottom-left to bottom-right, like macOS version)
private const double StartAngleDeg = 135;
private const double SweepDeg = 270;
private const double StrokeWidth = 6;
@@ -29,38 +31,30 @@ public class GaugeControl : FrameworkElement
var cy = RenderSize.Height / 2;
var radius = Math.Min(cx, cy) - StrokeWidth / 2 - 1;
// Track
DrawArc(dc, cx, cy, radius, StartAngleDeg, SweepDeg,
new Pen(new SolidColorBrush(Color.FromArgb(28, 255, 255, 255)), StrokeWidth));
// Fill
if (Percent > 0)
{
var sweep = SweepDeg * (Percent / 100.0);
DrawArc(dc, cx, cy, radius, StartAngleDeg, sweep, new Pen(Accent, StrokeWidth));
}
DrawArc(dc, cx, cy, radius, StartAngleDeg, SweepDeg * (Percent / 100.0),
new Pen(Accent, StrokeWidth));
var dpi = VisualTreeHelper.GetDpi(this).PixelsPerDip;
var dpi = VisualTreeHelper.GetDpi(this).PixelsPerDip;
// Percent label
var pctText = new FormattedText(
$"{(int)Math.Round(Percent)}%",
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
new Typeface(new FontFamily("Segoe UI"), FontStyles.Normal, FontWeights.Bold, FontStretches.Normal),
14, Brushes.White, dpi);
dc.DrawText(pctText, new Point(cx - pctText.Width / 2, cy - pctText.Height - 1));
// Sub-label
var sub = new FormattedText(
"Updated\nnow",
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface("Segoe UI"),
7, new SolidColorBrush(Color.FromRgb(0x88, 0x88, 0x88)), dpi);
sub.TextAlignment = TextAlignment.Center;
CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
new Typeface("Segoe UI"), 7,
new SolidColorBrush(Color.FromRgb(0x88, 0x88, 0x88)), dpi)
{
TextAlignment = TextAlignment.Center
};
dc.DrawText(sub, new Point(cx - sub.Width / 2, cy + 2));
}
@@ -68,10 +62,9 @@ public class GaugeControl : FrameworkElement
double startDeg, double sweepDeg, Pen pen)
{
if (sweepDeg <= 0) return;
var start = PointOnCircle(cx, cy, r, startDeg);
var end = PointOnCircle(cx, cy, r, startDeg + sweepDeg);
var isLarge = sweepDeg > 180;
var start = PointOnCircle(cx, cy, r, startDeg);
var end = PointOnCircle(cx, cy, r, startDeg + sweepDeg);
var isLarge = sweepDeg > 180;
var geo = new StreamGeometry();
using (var ctx = geo.Open())
+10 -10
View File
@@ -1,4 +1,5 @@
using System;
using Color = System.Windows.Media.Color;
using Point = System.Windows.Point;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
@@ -29,33 +30,32 @@ public class SparklineControl : FrameworkElement
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);
foreach (var v in plot) { if (v < minV) minV = v; if (v > maxV) maxV = v; }
var range = System.Math.Max(maxV - minV, 1);
Point Pt(int i) => new(
i / (double)(plot.Count - 1) * w,
i / (double)(plot.Length - 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);
for (var i = 0; i < plot.Length; 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);
for (var i = 1; i < plot.Length; i++) ctx.LineTo(Pt(i), true, false);
}
line.Freeze();
dc.DrawGeometry(null, new Pen(new SolidColorBrush(LineColor), 1.5) { LineJoin = PenLineJoin.Round }, line);
dc.DrawGeometry(null,
new System.Windows.Media.Pen(new SolidColorBrush(LineColor), 1.5) { LineJoin = PenLineJoin.Round },
line);
}
}