Files

79 lines
4.5 KiB
C#
Raw Permalink Normal View History

namespace UCVL.VPNPlus;
internal sealed class DailyTrafficChart : Control
{
private List<DailyUsage> _legacy = [];
private Dictionary<string, UsageHistorySeries>? _ranges;
private UsageHistoryPeriod _period = UsageHistoryPeriod.All[2];
private UsageHistorySeries? _series;
private readonly ToolTip _tip = new();
private RectangleF _plot;
internal event Action? HistoryChanged;
internal string StatusText => _series is null ? "后台暂未提供此范围的明细" :
_series.Points.Any(p => p.UploadBytes is null || p.DownloadBytes is null) ? "按小时入账 · 缺口为无明细" :
_series.Bucket == "hour" ? "按小时入账 · 含当前小时" : "套餐全部设备 · 按天统计";
internal string DetailText => _series is null ? "后台未提供此范围的可用明细;需要配套后台升级。" :
$"套餐全部设备 · {_series.Timezone}\n{_series.Note}\n本期总量不随筛选改变;本次流量仅指本机当前连接。";
internal string SelectedRange => _period.Key;
internal int PointCount => _series?.Points.Count ?? 0;
internal DailyTrafficChart()
{
DoubleBuffered = true;
SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.Opaque, false);
BackColor = Color.Transparent;
Font = Brand.Font(7.3f);
}
internal void SetHistory(List<DailyUsage> history, Dictionary<string, UsageHistorySeries>? ranges = null)
{ _legacy = history; _ranges = ranges; RefreshSeries(); }
internal void SetPeriod(UsageHistoryPeriod period) { _period = period; RefreshSeries(); }
private void RefreshSeries()
{
_series = UsageHistorySeries.Select(_period, _legacy, _ranges);
_tip.SetToolTip(this, DetailText);
Invalidate(); HistoryChanged?.Invoke();
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (_series is null || _series.Points.Count == 0 || _plot.Width <= 0 || !_plot.Contains(e.Location)) return;
var index = Math.Clamp((int)Math.Round((e.X - _plot.Left) * (_series.Points.Count - 1d) / _plot.Width), 0, _series.Points.Count - 1);
var p = _series.Points[index];
string Bytes(long? n) => n.HasValue ? $"{n.Value / 1024d / 1024:0.00} MB" : "无小时明细";
_tip.SetToolTip(this, $"{p.Time} ({_series.Timezone})\n代理上传 {Bytes(p.UploadBytes)} · 代理下载 {Bytes(p.DownloadBytes)}\n{_series.Note}");
}
protected override void Dispose(bool disposing) { if (disposing) _tip.Dispose(); base.Dispose(disposing); }
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var points = _series?.Points ?? [];
var peak = points.Count == 0 ? 1024 * 1024d : Math.Max(1024 * 1024d, points.Max(p => Math.Max(p.UploadBytes ?? 0, p.DownloadBytes ?? 0)));
var chart = ChartAxis.Draw(e.Graphics, ClientRectangle, Font, DeviceDpi, peak, speed: false);
_plot = chart.Plot;
if (points.Count == 0 || points.All(p => p.UploadBytes is null && p.DownloadBytes is null))
{
var message = _series is null ? "此范围需后台提供明细" : "暂无此范围代理流量明细";
TextRenderer.DrawText(e.Graphics, message, Font, Rectangle.Round(_plot), Brand.Muted,
ChartAxis.PaintFlags | TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
}
void Line(bool up)
{
var segment = new List<PointF>();
void Flush() { ChartAxis.Curve(e.Graphics, segment.ToArray(), up ? Brand.RoyalBlue : Brand.Cyan, DeviceDpi, dots: true); segment.Clear(); }
for (var i = 0; i < points.Count; i++)
{
var n = up ? points[i].UploadBytes : points[i].DownloadBytes;
if (n is null) { Flush(); continue; } // Never bridge a missing-data interval.
segment.Add(new(_plot.Left + i * _plot.Width / Math.Max(1, points.Count - 1),
_plot.Bottom - (float)(n.Value / chart.Maximum) * _plot.Height));
}
Flush();
}
Line(true); Line(false);
// At most four axis labels; all points remain available in the plot/tooltips.
var labels = points.Select((p, i) => i == 0 || i == points.Count - 1 || i == (points.Count-1)/3 || i == (points.Count-1)*2/3
? p.Timestamp?.ToString(_period.Bucket == "hour" ? "HH:mm" : "MM/dd") ?? "" : "").ToArray();
ChartAxis.TimeLabels(e.Graphics, _plot, Font, labels);
}
}