Windows 0.3.0 clean next-generation source delta; no private history or other platform changes
This commit is contained in:
+21
-4
@@ -6,6 +6,21 @@ namespace UCVL.VPNPlus;
|
||||
internal sealed class TrafficChart : Control
|
||||
{
|
||||
private readonly Queue<(DateTimeOffset Time, double Up, double Down)> _points = new();
|
||||
private TimeSpan _period = TimeSpan.FromMinutes(5);
|
||||
internal int PeriodMinutes => (int)_period.TotalMinutes;
|
||||
internal int PointCount => _points.Count;
|
||||
internal int VisiblePointCount => VisiblePoints().Length;
|
||||
internal void SetPeriodMinutes(int minutes)
|
||||
{
|
||||
if (minutes is not (1 or 5 or 15 or 60)) throw new ArgumentOutOfRangeException(nameof(minutes));
|
||||
_period = TimeSpan.FromMinutes(minutes);
|
||||
Invalidate();
|
||||
}
|
||||
private (DateTimeOffset Time, double Up, double Down)[] VisiblePoints()
|
||||
{
|
||||
var end = _points.Count == 0 ? DateTimeOffset.Now : _points.Last().Time;
|
||||
return _points.Where(point => end - point.Time <= _period).ToArray();
|
||||
}
|
||||
internal TrafficChart()
|
||||
{
|
||||
DoubleBuffered = true;
|
||||
@@ -19,19 +34,21 @@ internal sealed class TrafficChart : Control
|
||||
var at = time ?? DateTimeOffset.Now;
|
||||
double Clean(double n) => double.IsFinite(n) ? Math.Max(0, n) : 0;
|
||||
_points.Enqueue((at, Clean(up), Clean(down)));
|
||||
while (_points.Count > 60 || (_points.Count > 0 && at - _points.Peek().Time > TimeSpan.FromMinutes(5))) _points.Dequeue();
|
||||
// Bounded current-session samples only: never turn daily accounting
|
||||
// totals into a fictitious speed curve. Changing range preserves samples.
|
||||
while (_points.Count > 3600 || (_points.Count > 0 && at - _points.Peek().Time > TimeSpan.FromMinutes(60))) _points.Dequeue();
|
||||
Invalidate();
|
||||
}
|
||||
internal void Reset() { _points.Clear(); Invalidate(); }
|
||||
protected override void OnPaint(PaintEventArgs e)
|
||||
{
|
||||
base.OnPaint(e);
|
||||
var points = _points.ToArray();
|
||||
var points = VisiblePoints();
|
||||
var peak = points.Length == 0 ? 1024 : Math.Max(1024, points.Max(p => Math.Max(p.Up, p.Down)));
|
||||
var chart = ChartAxis.Draw(e.Graphics, ClientRectangle, Font, DeviceDpi, peak, speed: true);
|
||||
if (chart.Plot.Width < 1 || chart.Plot.Height < 1) return;
|
||||
var end = points.Length == 0 ? DateTimeOffset.Now : points[^1].Time;
|
||||
ChartAxis.TimeLabels(e.Graphics, chart.Plot, Font, [end.AddMinutes(-5).ToString("HH:mm"), end.AddMinutes(-2.5).ToString("HH:mm"), end.ToString("HH:mm")]);
|
||||
ChartAxis.TimeLabels(e.Graphics, chart.Plot, Font, [end.Subtract(_period).ToString("HH:mm"), end.Subtract(_period / 2).ToString("HH:mm"), end.ToString("HH:mm")]);
|
||||
if (points.Length == 0)
|
||||
{
|
||||
TextRenderer.DrawText(e.Graphics, "等待代理流量采样", Font, Rectangle.Round(chart.Plot), Brand.Muted,
|
||||
@@ -39,7 +56,7 @@ internal sealed class TrafficChart : Control
|
||||
return;
|
||||
}
|
||||
PointF[] Line(bool up) => points.Select(p => new PointF(
|
||||
chart.Plot.Left + (float)Math.Clamp(1 - (end - p.Time).TotalSeconds / 300, 0, 1) * chart.Plot.Width,
|
||||
chart.Plot.Left + (float)Math.Clamp(1 - (end - p.Time).TotalSeconds / _period.TotalSeconds, 0, 1) * chart.Plot.Width,
|
||||
chart.Plot.Bottom - (float)((up ? p.Up : p.Down) / chart.Maximum) * chart.Plot.Height)).ToArray();
|
||||
ChartAxis.Curve(e.Graphics, Line(true), Brand.RoyalBlue, DeviceDpi, dots: points.Length == 1);
|
||||
ChartAxis.Curve(e.Graphics, Line(false), Brand.Cyan, DeviceDpi, dots: points.Length == 1);
|
||||
|
||||
Reference in New Issue
Block a user