using Avalonia; using Avalonia.Controls; using Avalonia.Media; using FinalToken.Core; namespace FinalToken.App; public sealed class BurnUpChart(LimitChart chart) : Decorator { private Rect plot; private double? crosshair; public BurnUpChart() : this(new("", [])) { } protected override void OnInitialized() { base.OnInitialized(); Height = 240; ToolTip.SetShowDelay(this, 0); if (chart.Window is { } snapshot && Display.ForecastWaiting(snapshot, chart.Now) is { } waiting) Child = new TextBlock { Text = waiting, FontSize = 12, Foreground = Ui.Colour("Muted", Ui.Dark(this)), HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Center, VerticalAlignment = Avalonia.Layout.VerticalAlignment.Top, Margin = new Thickness(42, 40, 16, 0) }; PointerMoved += (_, e) => { if (chart.Analysis is not { } analysis || !plot.Contains(e.GetPosition(this))) return; crosshair = Math.Clamp(e.GetPosition(this).X, plot.Left, plot.Right); var time = analysis.Current.Start.AddTicks((long)((analysis.Current.Reset - analysis.Current.Start).Ticks * (crosshair.Value - plot.Left) / plot.Width)); var current = analysis.Current; var before = current.Points.LastOrDefault(p => p.Time <= time); var after = current.Points.FirstOrDefault(p => p.Time >= time); double? used = before?.Used; var source = before?.Origin == UseOrigin.Estimated ? "Estimated from tokens" : "Measured"; if (time > chart.Now) { if (current.ReachedOrigin == UseOrigin.Estimated) { used = 100; source = "Limit reached (estimated)"; } else if (chart.Window is { } window && LimitAnalysis.AverageUseAtReset(window.Window, chart.Now, window.Pace) is { } forecast) { used = window.Window.UsedPercent + (forecast - window.Window.UsedPercent) * (time - chart.Now).TotalSeconds / (current.Reset - chart.Now).TotalSeconds; source = "Average forecast"; } else used = null; } else if (before is not null && after is not null && after.Time > before.Time) { if (before.Time == current.Start && current.Scale is null && after.Time > current.Start) used = null; else used = before.Used + (after.Used - before.Used) * (time - before.Time).TotalSeconds / (after.Time - before.Time).TotalSeconds; if (before.Origin == UseOrigin.Estimated || after.Origin == UseOrigin.Estimated) source = "Estimated from tokens"; } var expected = 100 * (time - current.Start).TotalSeconds / (current.Reset - current.Start).TotalSeconds; ToolTip.SetTip(this, new DataTooltip(Display.Projection(time), new("Used", used is { } use ? $"{use:0.##} %" : "Collecting history", Ui.ProviderColour(chart.Provider, Ui.Dark(this))), new("Expected", $"{expected:0.##} %"), new("Delta", used is { } value ? $"{value - expected:+0.##;-0.##;0} points" : "Unknown"), new("Source", used is null ? "No baseline" : source))); InvalidateVisual(); }; PointerExited += (_, _) => { crosshair = null; InvalidateVisual(); }; } public override void Render(DrawingContext context) { base.Render(context); context.DrawRectangle(Brushes.Transparent, null, new Rect(Bounds.Size)); if (chart.Analysis is not { } data || chart.Window is not { } snapshot) return; var instance = data.Current; var dark = Ui.Dark(this); var muted = Ui.Colour("Muted", dark); var colour = Ui.ProviderColour(chart.Provider, dark); plot = new Rect(42, 22, Math.Max(1, Bounds.Width - 58), Math.Max(1, Bounds.Height - 60)); var duration = (instance.Reset - instance.Start).TotalSeconds; double X(DateTimeOffset time) => plot.Left + (time - instance.Start).TotalSeconds / duration * plot.Width; double Y(double use) => plot.Bottom - use / 100 * plot.Height; foreach (var tick in new[] { 0, 25, 50, 75, 100 }) { context.DrawLine(new Pen(Ui.Colour("Grid", dark)), new(plot.Left, Y(tick)), new(plot.Right, Y(tick))); ChartText.Draw(context, tick + "%", new(0, Y(tick) - 7), muted); } context.DrawLine(new Pen(muted, 1), new(plot.Left, plot.Bottom), new(plot.Right, plot.Top)); using (context.PushClip(plot)) { if (data.Previous is { } previous) using (context.PushOpacity(.2)) DrawSeries(previous, true); DrawSeries(instance, false); if (instance.ReachedLimit is null && LimitAnalysis.AverageUseAtReset(snapshot.Window, chart.Now, snapshot.Pace) is { } average && instance.Reset > chart.Now) context.DrawLine(new Pen(colour, 2, new DashStyle([1, 3], 0)), new(X(chart.Now), Y(snapshot.Window.UsedPercent)), new(plot.Right, Y(average))); var nowX = X(chart.Now); if (nowX >= plot.Left && nowX <= plot.Right) context.DrawLine(new Pen(muted, 1, new DashStyle([3, 3], 0)), new(nowX, plot.Top), new(nowX, plot.Bottom)); if (crosshair is { } x) context.DrawLine(new Pen(Ui.Colour("Secondary", dark), 1), new(x, plot.Top), new(x, plot.Bottom)); } if (instance.ReachedLimit is null && snapshot.Pace.Projection is { } crossing && crossing <= instance.Reset && crossing >= chart.Now) { var x = X(crossing); var mark = snapshot.Pace.State == PaceState.Ahead ? Brush.Parse("#d03b3b") : colour; context.DrawLine(new Pen(mark, 2), new(x, plot.Top - 5), new(x, plot.Top + 5)); ChartText.Draw(context, "100 % " + Display.Projection(crossing), new(Math.Clamp(x - 80, plot.Left, Math.Max(plot.Left, plot.Right - 175)), 0), Ui.Colour("Secondary", dark)); } if (instance.ReachedLimit is { } reached && instance.ReachedOrigin == UseOrigin.Estimated) { var labelX = Math.Clamp(X(reached) - 80, plot.Left, Math.Max(plot.Left, plot.Right - 260)); context.DrawLine(new Pen(colour, 2), new(X(reached), plot.Top - 5), new(X(reached), plot.Top + 5)); ChartText.Draw(context, "Limit reached (estimated) " + Display.Projection(reached), new(labelX, 0), Ui.Colour("Secondary", dark)); } ChartText.Draw(context, Display.Projection(instance.Start), new(plot.Left, plot.Bottom + 12), muted); ChartText.Draw(context, Display.Projection(instance.Reset), new(Math.Max(plot.Left, plot.Right - 120), plot.Bottom + 12), muted); var tickX = X(chart.Now); if (tickX > plot.Left + 160 && tickX < plot.Right - 160) ChartText.Draw(context, "now", new(tickX - 10, plot.Bottom + 12), Ui.Colour("Secondary", dark)); void DrawSeries(LimitInstance series, bool previous) { for (var i = 1; i < series.Points.Count; i++) { var a = series.Points[i - 1]; var b = series.Points[i]; if (i == 1 && a.Time == series.Start && b.Origin == UseOrigin.Measured && series.Scale is null && b.Time > series.Start) continue; var offset = previous ? instance.Start - series.Start : TimeSpan.Zero; var estimated = a.Origin == UseOrigin.Estimated || b.Origin == UseOrigin.Estimated; using (context.PushOpacity(estimated ? .55 : 1)) context.DrawLine(new Pen(colour, 2, estimated ? new DashStyle([4, 3], 0) : null, lineCap: PenLineCap.Round), new(X(a.Time + offset), Y(a.Used)), new(X(b.Time + offset), Y(b.Used))); } } } } public sealed class InstancePeaks(IReadOnlyList instances, Provider provider) : Control { private readonly List<(Rect Bounds, LimitInstance Instance)> hits = []; protected override void OnInitialized() { base.OnInitialized(); Height = 96; ToolTip.SetShowDelay(this, 0); PointerMoved += (_, e) => { var instance = hits.FirstOrDefault(h => h.Bounds.Contains(e.GetPosition(this))).Instance; ToolTip.SetTip(this, instance is null ? null : new DataTooltip(Display.Projection(instance.Start) + " — " + Display.Projection(instance.End), new("Peak", $"{instance.Peak:0.##} %", Ui.ProviderColour(provider, Ui.Dark(this))), new(instance.ReachedOrigin == UseOrigin.Estimated ? "Limit reached (estimated)" : "100 % reached", instance.ReachedLimit is { } time ? "⚠ " + Display.Projection(time) : "Not reached"), new("Source", instance.Points.Any(p => p.Origin == UseOrigin.Estimated) ? "Includes token estimates" : "Measured"))); }; } public override void Render(DrawingContext context) { base.Render(context); context.DrawRectangle(Brushes.Transparent, null, new Rect(Bounds.Size)); hits.Clear(); if (instances.Count == 0) return; var colour = Ui.ProviderColour(provider, Ui.Dark(this)); var width = Bounds.Width / instances.Count; for (var i = 0; i < instances.Count; i++) { var instance = instances[instances.Count - 1 - i]; var x = i * width + width / 2; context.DrawLine(new Pen(colour, 5, lineCap: PenLineCap.Round), new(x, 44), new(x, 44 - Math.Clamp(instance.Peak ?? 0, 0, 100) / 100 * 26)); var session = instance.Reset - instance.Start <= TimeSpan.FromDays(1); var label = session ? instance.Start.LocalDateTime.ToString("HH:mm") + "\u2013" + instance.End.LocalDateTime.ToString("HH:mm") : instance.Start.LocalDateTime.ToString("dd.MM") + "\u2013" + instance.End.LocalDateTime.ToString("dd.MM"); ChartText.Draw(context, label, new(Math.Max(i * width, x - 26), 52), Ui.Colour("Muted", Ui.Dark(this)), 9); var caption = instance.ReachedLimit is { } reached ? "\u26a0 " + reached.LocalDateTime.ToString("dd.MM HH:mm") : session ? instance.Start.LocalDateTime.ToString("dd.MM") : ""; ChartText.Draw(context, caption, new(Math.Max(i * width, x - 30), 68), Ui.Colour("Secondary", Ui.Dark(this)), 9); ChartText.Draw(context, $"{instance.Peak:0.#}%", new(x - 16, 0), Ui.Colour("Secondary", Ui.Dark(this))); hits.Add((new Rect(i * width, 0, width, Bounds.Height), instance)); } } }